Best Practices of Setting Timeouts in Selenium


What is timeouts in selenium?

If you are expecting any test cases to be completed within specified time, then we need to mention that specified period of time within the timeouts attribute.

Different Timeouts in selenium:

1. implicitlyWait()

2. setScriptTimeout()

3. pageLoadTimeout()

implicitlyWait()

Implicit wait is used to set a default wait time in the automation script to wait for an element visibility on the page.

Example:

Implicitly Wait for 10 seconds

WebDriver driver= new CromeDriver();
driver.get("https://suremdm.42gears.com/");
driver.manage().timeouts().implicityWait(10,TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("abc@gmail.com");

setScriptTimeout()

Set the amount of time to wait for an asynchronous script to complete execution, but if the process is not finished before the time limit, it is throwing an exception.

Example:

setScriptTimeout for 5 seconds

JavascriptExecutor js=(JavascriptExecutor)driver;
s.executeScript("alert('Have a nice day');");
driver.manage().timeouts().setScriptTimeout(5,TimeUnit.SECONDS);
js.executeAsyncScript("window.setTimeout(arguments[arguments.length-1], 500);");

pageLoadTimeout()

Set the amount of time to wait for page load to be complete or pageLoadTimeout is waiting for an application page load completely.

Example:

set the time of 10 seconds for page to complete the loading

WebDriver driver= new CromeDriver();
driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);
driver.get.("https://suremdm.42gears.com/");

Selenium Timeouts must be included to create effective and comprehensive running test cases.

Leave A Comment

Your email address will not be published. Required fields are marked *