简体   繁体   English

phpunit selenium2扩展中的显式等待

[英]Explicit waits in phpunit selenium2 extension

For C# there is a way to write a statement for waiting until an element on a page appears: 对于C#,有一种方法可以编写一个语句来等待页面上的元素出现:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.Id("someDynamicElement"));
    });

But is there a way to do the same in phpunit's selenium extension? 但有没有办法在phpunit的selenium扩展中做同样的事情?

Note 1 注1

The only thing I've found is $this->timeouts()->implicitWait() , but obviously it's not what I'm looking for. 我发现的唯一的东西是$this->timeouts()->implicitWait() ,但显然它不是我想要的。

Note 2 笔记2

This question is about Selenium2 and PHPUnit_Selenium2 extension accordingly. 这个问题是关于Selenium2和PHPUnit_Selenium2相应的扩展。

The implicitWait that you found is what you can use instead of waitForCondition. 您找到的implicitWait是您可以使用而不是waitForCondition。 As the specification of WebDriver API (that you also found ;)) states: 正如WebDriver API的规范(您也发现;))指出:

implicit - Set the amount of time the driver should wait when searching for elements. 隐式 - 设置驱动程序在搜索元素时应等待的时间。 When searching for a single element, the driver should poll the page until an element is found or the timeout expires, whichever occurs first. 搜索单个元素时,驱动程序应轮询页面,直到找到元素或超时到期,以先发生者为准。

For example, this code will wait up to 30 seconds for an element to appear before clicking on it: 例如,在单击它之前,此代码将等待最多30秒才能显示元素:

public function testClick()
{
    $this->timeouts()->implicitWait(30000);
    $this->url('http://test/test.html');
    $elm = $this->clickOnElement('test');
}

The drawback is it's set for the life of the session and may slow down other tests unless it's set back to 0. 缺点是它设置了会话的生命周期,并可能减慢其他测试,除非它被设置回0。

From my experience it is very hard to debug selenium test case from phpunit, not to mention maintaining them. 根据我的经验,从phpunit调试selenium测试用例非常困难,更不用说维护它们了。 The approach I use in my projects is to use Selenium IDE, store test cases as .html files and only invoke them via phpunit. 我在项目中使用的方法是使用Selenium IDE,将测试用例存储为.html文件,并且只能通过phpunit调用它们。 If there is anything wrong, I can lunch them from IDE and debug in a much easier why. 如果有任何问题,我可以从IDE中进行午餐,然后调试就更容易了。 And Selenium IDE has waitForElementPresent, waitForTextPresent and probably some other method, which can solve your issue. Selenium IDE有waitForElementPresent,waitForTextPresent和可能的其他一些方法,可以解决你的问题。 If you want to give it a try, you can use this method in your class inheriting from Selenium Test Case class. 如果您想尝试一下,可以在继承自Selenium Test Case类的类中使用此方法。

    $this->runSelenese("/path/to/test/case.html");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM