Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mink: wait for page to load in @BeforeStep

I want to execute some javascript on page in @BeforeStep hook that depends on jQuery. However jQuery is not defined at that time, in fact page is blank.

Here's what I am trying to achive:

/**
 * @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 figured maybe I could wait for the page to load jQuery first (commented line), but it is not true. Seems like execution is halted until that hook is processed.

What is the right place in behat/mink ecosystem to execute javascript on page?

like image 893
Dziamid Avatar asked Aug 23 '12 08:08

Dziamid


1 Answers

How about this?

$this->getSession()->wait(5000, 'typeof window.jQuery == "function"');
like image 182
Jakub Zalas Avatar answered Sep 25 '22 23:09

Jakub Zalas