Is there any method in WebDriver with Java for controlling speed of browser?

这一生的挚爱 提交于 2019-12-18 05:57:09

问题


When I use Selenium RC there is a method setSpeed as:

selenium.setSpeed("500");

What is the way to control speed of browser in Selenium WebDriver?


回答1:


You can use Thread.Sleep(500) (or equivalent) in whatever language you are using to run webdriver. This will cause the thread to pause for an exact number of milliseconds.

Alternatively you can use explicit or implicit waits described here.

  • Explicit waits allow you to define an ExpectedCondition. Webdriver will check the condition every 500 milliseconds until it returns true, (after which execution will resume immediately).

  • Implicit waits cause webdriver to keep retry attempting to locate something in the DOM. Execution will resume immediately once the element is found.

Note that neither implicit nor explicit waits will guarantee a 500 millisecond pause.




回答2:


There is no longer any way to control the speed of each "step" in Selenium WebDriver. At one time, there was a setSpeed() method on the Options interface (in the Java bindings; other bindings had similar constructs on their appropriately-named objects), but it was deprecated long, long ago. The theory behind this is that you should not need to a priori slow down every single step of your WebDriver code. If you need to wait for something to happen in the application you're automating, you should be using an implicit or explicit wait routine.




回答3:


There is no straight forward way. But there is a hack that you can use, you can override methods of webdriver and introduce a explicit sleep to slow down your tests eg. overriding findElement method

public class _WebDriver extends FirefoxDriver {

@Override
public WebElement findElement(By by) {
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return by.findElement((SearchContext) this);
}

}



回答4:


Even better might be to use the webdriver FluentWait class alongside with ExpectedCondition. Sample can be found here: http://www.summa-tech.com/blog/2011/10/10/using-page-objects-with-selenium-and-web-driver-20/



来源:https://stackoverflow.com/questions/11154982/is-there-any-method-in-webdriver-with-java-for-controlling-speed-of-browser

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