Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 669
Ramitha Silva Avatar asked Dec 02 '22 11:12

Ramitha Silva


2 Answers

If we didn't mention any polling time, selenium will take the default polling time as 500milli seconds. i.e.., 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.

like image 63
santhosh kumar Avatar answered Dec 05 '22 08:12

santhosh kumar


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));
like image 43
JDC Avatar answered Dec 05 '22 07:12

JDC