问题描述
当使用Selenium2Driver将Behat与Mink结合使用时,是否有办法可靠地等待页面加载?
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"
即仅此一次测试就需要1分钟30秒.
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.
推荐答案
您可以使用带有Javasccript条件的wait方法,例如:
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'");
}
另一种好的做法是有一个等待该元素的方法,如果找到element返回该元素对象,否则它将引发异常.
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.
对于click方法,您可以输入以下内容:
For the click method you can have something like this:
$this->waitForElement("css_selector")->click();
这篇关于Behat + Selenium 2等待页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!