Selenium隐式等待是否总是占据整个等待时间,还是可以更快地完成? [英] Does Selenium implicit wait always take the entire wait time or can it finish sooner?

查看:222
本文介绍了Selenium隐式等待是否总是占据整个等待时间,还是可以更快地完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Selenium隐式等待是否总是占用整个等待时间,还是可以更快地完成?如果我将隐式等待时间设置为10秒,那么对.findElement的调用是否可以在几秒钟内完成,还是总是需要整整10秒?

Does Selenium implicit wait always take the entire wait time or can it finish sooner? If I set the implicit wait to 10 seconds, could a call to .findElement finish in a few seconds or would it always take the entire 10 seconds?

此页面表示它正在等待整整十秒钟,这非常令人困惑,因为它不是javadoc所暗示的.

This page implies that it waits the full 10 seconds, which is very confusing because its not what the javadoc implies.

以下来自WebDriver.java的代码注释表示,它的轮询操作可以比定义隐式超时的时间早完成.但是,评论中的最后一句话确实使人难以置信,这让我不太确定.如果它实际上是在轮询,那么它将不会对整个测试等待时间产生负面影响?

The following code comment from WebDriver.java implies that its a polling action which can finish sooner than the implicit timeout is defined at. BUT, the last sentence in the comment really throws a wrench into that belief and makes me not totally sure about it. If it is actually polling, then how would it "adversely affect test time", since it wouldn't go the entire implicit wait duration?

/**
 *  from WebDriver.java
 * Specifies the amount of time the driver should wait when searching for an element if
 * it is not immediately present.
 * <p/>
 * When searching for a single element, the driver should poll the page until the 
 * element has been found, or this timeout expires before throwing a 
 * {@link NoSuchElementException}. When searching for multiple elements, the driver 
 * should poll the page until at least one element has been found or this timeout has
 * expired.
 * <p/>
 * Increasing the implicit wait timeout should be used judiciously as it will have an 
 * adverse effect on test run time, especially when used with slower location 
 * strategies like XPath.
 * 
 * @param time The amount of time to wait.
 * @param unit The unit of measure for {@code time}.
 * @return A self reference.
 */
Timeouts implicitlyWait(long time, TimeUnit unit);

此外,是否有人可以提供有关默认轮询"发生频率的信息?

Also, if anyone can provide information on how often the default "polling" occurs?

推荐答案

一旦找到元素,它就可以完成.如果不是,它将引发错误并停止.轮询时间还是非常特定于驱动程序的实现(不是Java绑定,而是驱动程序部分,例如:FireFox扩展,Safari扩展等)

It can finish once it was able to find the element. If not it does throws the error and stops. The poll time is again very specific to the driver implementation ( not Java bindings , but the driver part, example: FireFox extension, Safari Extension etc.)

正如我提到的在这里,这些都是非常具体的驱动程序实现.所有与驱动程序相关的调用都通过execute方法进行.

As I have mentioned here, these are very specific to the driver implementation. All driver related calls goes via execute method.

我正在提出execute方法的要点(您可以找到完整的源代码

I'm putting up the gist over of the execute method (you can find the full source here):

protected Response execute(String driverCommand, Map<String, ?> parameters) {
    Command command = new Command(sessionId, driverCommand, parameters);
    Response response;

    long start = System.currentTimeMillis();
    String currentName = Thread.currentThread().getName();
    Thread.currentThread().setName(
        String.format("Forwarding %s on session %s to remote", driverCommand, sessionId));
    try {
      log(sessionId, command.getName(), command, When.BEFORE);
      response = executor.execute(command);
      log(sessionId, command.getName(), command, When.AFTER);

      if (response == null) {
        return null;
      }
      //other codes 
}

该行:

response = executor.execute(command);

讲述了整个故事. executor的类型为CommandExecutor,因此所有调用都转到特定的驱动程序类,例如ChromeCommandExecutor,SafariDriverCommandExecutor,该类具有自己的处理方式.

says the whole story. executor is of type CommandExecutor, so all calls goes to the specific driver class like ChromeCommandExecutor,SafariDriverCommandExecutor, which has their own handling.

因此,轮询取决于驱动程序的实现.

So the polling is upto the driver implementation.

如果要指定轮询时间,则可能应该开始使用显式等待.

If you want to specify the polling time, then you should probably start using Explicit Waits.

这篇关于Selenium隐式等待是否总是占据整个等待时间,还是可以更快地完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆