Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Selenium implicit wait always take the entire wait time or can it finish sooner?

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?

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

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?

like image 373
djangofan Avatar asked Mar 17 '23 10:03

djangofan


1 Answers

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.)

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

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 
}

The line:

response = executor.execute(command);

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.

like image 96
Ant's Avatar answered Apr 26 '23 08:04

Ant's