Behat + selenium 2 wait for page to load

大兔子大兔子 提交于 2020-01-09 11:39:29

问题


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.


回答1:


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();


来源:https://stackoverflow.com/questions/38200240/behat-selenium-2-wait-for-page-to-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!