【问题标题】:Mink: wait for page to load in @BeforeStepMink:在@BeforeStep 中等待页面加载
【发布时间】:2013-08-31 10:12:35
【问题描述】:

我想在依赖于 jQuery 的 @BeforeStep 钩子中的页面上执行一些 javascript。但是当时没有定义jQuery,实际上页面是空白的。

这是我想要达到的目标:

/**
 * @BeforeStep @javascript
 */
public function registerAjaxEventHandlers()
{
    $javascript = <<<JS
        window.jQuery(document).ready(function () {
            if (window.__ajaxStatus !== undefined) {
                return;
            }
            window.__ajaxStatus = 'none';

            $('body').ajaxStop(function() {
              window.__ajaxStatus = 'idle';
            });

            $('body').ajaxStart(function() {
              window.__ajaxStatus = 'in-flight';
            });
        });
JS;
        //$this->getSession()->wait(5000, 'window.jQuery !== undefined');
        $this->getSession()->executeScript($javascript);
    }

我想也许我可以等待页面首先加载 jQuery(注释行),但事实并非如此。似乎在处理该钩子之前执行已停止。

在 behat/mink 生态系统中,在页面上执行 javascript 的正确位置是什么?

【问题讨论】:

  • 我最终将上面的JS脚本添加到源代码中。

标签: selenium selenium-webdriver behat mink


【解决方案1】:

这个怎么样?

$this->getSession()->wait(5000, 'typeof window.jQuery == "function"');

【讨论】:

  • 也没有帮助。等待 5 秒,屏幕为白色并继续。
  • 我做了类似的事情,但是当一个步骤不与浏览器交互时,这会导致场景运行时间更长,例如加载先决条件数据时。像$this-&gt;getSession-&gt;getCurrentUrl() === 'about:blank' 这样进行检查会有所帮助,但也许有更优雅的方法。
【解决方案2】:

您的 javascript 在该步骤之前执行,即在页面加载之前。但是如果你加载一个页面,各种 ondomload/pageload JavaScript 也会被触发。

如果您愿意在初始页面之后运行它,您可以像这样使用@AfterStep:

/**
 * @AfterStep
 */
public function set_selenium_css_selector (Behat\Behat\Event\StepEvent $event) {
  $this->getSession()->wait(10, 'jQuery ("body").addClass ("selenium")');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2011-08-17
    相关资源
    最近更新 更多