一尘不染

Selenium隐式等待是否总是占据整个等待时间,还是可以更快地完成?

selenium

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

该页面暗示它要等待整整10秒钟,这非常令人困惑,因为它不是javadoc所暗示的。

WebDriver.java的以下代码注释隐含了它的轮询操作可以比定义隐式超时更快的时间完成。但是, 评论中的最后一句话确实 使人 难以置信
,使我对此不太确定。如果它实际上是在轮询,那么它将如何“不利地影响测试时间”,因为它不会遍历整个隐式等待时间?

/**
 *  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);

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


阅读 282

收藏
2020-06-26

共1个答案

一尘不染

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

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

我正在介绍该execute方法的要点(您可以在此处找到完整的源代码):

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,该类具有自己的处理方式。

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

如果要指定轮询时间,则可能应该开始使用ExplicitWaits

2020-06-26