5

我想在依赖于 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 的正确位置是什么?

4

2 回答 2

5

这个怎么样?

$this->getSession()->wait(5000, 'typeof window.jQuery == "function"');
于 2012-08-24T07:41:07.237 回答
0

您的 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")');
}
于 2013-08-31T02:12:35.660 回答