Search code examples
javaseleniumpolling

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 ?


Solution

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