WebDriver 中是否有任何方法可以使用 Java 来控制浏览器的速度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11154982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any method in WebDriver with Java for controlling speed of browser?
提问by Ripon Al Wasim
When I use Selenium RC there is a method setSpeed as:
当我使用 Selenium RC 时,有一个方法 setSpeed 为:
selenium.setSpeed("500");
selenium.setSpeed("500");
What is the way to control speed of browser in Selenium WebDriver?
在 Selenium WebDriver 中控制浏览器速度的方法是什么?
采纳答案by shamp00
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.
您可以使用Thread.Sleep(500)
(或等效的)任何语言来运行 webdriver。这将导致线程暂停精确的毫秒数。
Alternatively you can use explicitor implicit waitsdescribed 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.
显式等待允许您定义一个
ExpectedCondition
. Webdriver 将每 500 毫秒检查一次条件,直到它返回 true,(之后将立即恢复执行)。隐式等待导致 webdriver 不断重试尝试在 DOM 中定位某些内容。一旦找到元素,执行将立即恢复。
Note that neither implicit nor explicit waits will guarantee a 500 millisecond pause.
请注意,隐式或显式等待都不能保证 500 毫秒的暂停。
回答by JimEvans
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 priorislow 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.
Selenium WebDriver 中不再有任何方法可以控制每个“步骤”的速度。曾经setSpeed()
,Options
接口上有一个方法(在 Java 绑定中;其他绑定在其适当命名的对象上有类似的构造),但很久很久以前它就被弃用了。这背后的理论是,您不应该先验地减慢 WebDriver 代码的每一步。如果您需要等待正在自动化的应用程序中发生某些事情,您应该使用隐式或显式等待例程。
回答by moiz virani
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
没有直接的方法。但是你可以使用一个 hack,你可以覆盖 webdriver 的方法并引入显式睡眠来减慢你的测试,例如。覆盖 findElement 方法
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);
}
}
回答by Arek
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/
更好的方法可能是将 webdriver FluentWait 类与 ExpectedCondition 一起使用。示例可以在这里找到:http: //www.summa-tech.com/blog/2011/10/10/using-page-objects-with-selenium-and-web-driver-20/