简体   繁体   中英

Selenium webdriver polling time

I'm looking forward for a proper explanation about the selenium webdriver polling time in Selenium.

As I know, below wait command will wait for 40 seconds until the specific element get clickable

  public void CreateSalesOrder(){
        WebDriverWait wait = new WebDriverWait(driver, 40);
        wait.until(ExpectedConditions.elementToBeClickable(btnNewSalesOrser));
            btnNewSalesOrser.click(); 
    }

In the 2nd code snippet I've added "Polling" command.

   public void CreateSalesOrder(){
        WebDriverWait wait = new WebDriverWait(driver, 40);
        wait.pollingEvery(2, TimeUnit.SECONDS);
        wait.until(ExpectedConditions.elementToBeClickable(btnNewSalesOrser));
        btnNewSalesOrser.click();
    }

What is the use of polling time ?

If we didn't mention any polling time, selenium will take the default polling time as 500milli seconds. ie., script will check for the excepted condition for the webelement in the web page every 500 milli seconds. Your first code snippet works with this.

We use pollingEvery to override the default polling time. In the below example(your second code snippet), the script checks for the expected condition for every 2 seconds and not for 500 milliseconds.

public void CreateSalesOrder()
{
    WebDriverWait wait = new WebDriverWait(driver, 40);
    wait.pollingEvery(2, TimeUnit.SECONDS);
    wait.until(ExpectedConditions.elementToBeClickable(btnNewSalesOrser));
    btnNewSalesOrser.click();
}

This polling frequency may actually help in reducing the CPU overload. Refer this javadoc for more info pollingEvery .

Hope this helps you. Thanks.

Using WebDriverWait wait = new WebDriverWait(driver, 40); the driver will wait a maximum of 40 seconds until the condition is fulfilled.

Using wait.pollingEvery(2, TimeUnit.SECONDS); specifies that the driver will do the checks (to see if the condition is fulfilled) every 2 seconds until the condition is fulfilled.


In sum this means that your driver will do a check every 2 seconds for a period of 40 seconds .


You could also specify the polling interval as a shortcut in the Constructor :

WebDriverWait wait = new WebDriverWait(driver, 40, TimeUnit.SECONDS.toMillis(2));

For understanding the explanation, you have to understand the polling time for Explicit Wait.

WebDriverWait wait = new WebDriverWait(driver, 40);

This waits up to 40 seconds before throwing a TimeoutException unless it finds the element to return within 40 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully hence the default polling time for ExplicitWait is 500 milliseconds.

wait.pollingEvery(2, TimeUnit.SECONDS);

In this case, the polling time is 2 seconds ie the Expected condition will not be checked after every 500 milliseconds, it should be checked after 2 seconds until the specific elements get clickable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM