Mink: wait for the page to load at @BeforeStep

I want to execute some javascript on a page in the @BeforeStep method, which depends on jQuery. However, jQuery is not defined at the time; in fact, the page is empty.

Here is what I am trying to achieve:

/**
 * @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);
    }

I thought maybe I can wait for the page to load jQuery first (comment line), but it is not. It seems that execution is paused until the handler is processed.

What is the place in the behit / mink ecosystem to execute javascript on a page?

+5
source share
2 answers

How about this?

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

javascript , . , JavaScript ondomoad/pageload .

, @AfterStep :

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

All Articles