Search code examples
seleniumselenium-webdriverbehatmink

Behat + selenium 2 wait for page to load


Is there a way to reliably wait for pages to load when using Behat with Mink using the Selenium2Driver?

I've inherited some legacy tests that wait like this:

Background:
  Given I am on "http://test.example.com"
  And I wait for "20000"
  Given I click on the element with css selector ".button1"
  And I wait for "30000"
  Given I click on the element with css selector ".button2"
  And I wait for "30000"
  Given I click on the element with css selector ".button1"
  And I wait for "10000"

i.e. just this single test takes 1minute 30seconds.

What I'd like to do is have a generic way of waiting for the previous click to result in a page load, without having to wait a large fixed amount of time each time.

All of the suggestions I can see for waiting for page load, all refer to checking that a particular page element is loaded.

However these tests run against a variety of legacy websites, where there isn't always a standard element that can be checked to be present, so I'm hoping to use a more generic method.


Solution

  • You can use a wait method with javasccript condition like:

    /**
     * @When /^wait for the page to be loaded$/
     */
    public function waitForThePageToBeLoaded()
    {
        $this->getSession()->wait(10000, "document.readyState === 'complete'");
    }
    

    Another good practice is to have a method that waits for the element, if element is found returns the element object else it will throw an exception.

    For the click method you can have something like this:

    $this->waitForElement("css_selector")->click();