Understanding Selenium Timeouts with Examples |. BrowserStack

Home Guide Understanding Selenium Timeouts Understanding Selenium Timeouts By Neha Vaidya, Community Contributor - April 21, 2020 Selenium is one of the most widely used tools for automation testing.

Every command in Selenium Webdriver is an asynchronous operation.What are Selenium Timeouts?Consider a situation in which web driver fails to execute the test case as the webpage takes too long to load. In such cases, it is essential to mention the wait time for the page load to avoid test case failure. This is where Timeouts play an important role. They are used to set an interval of time between actions performed on the test.Timeouts are usually performed using Selenium wait commands.The various Selenium Webdriver timeouts used while testing an application are as follows:Let’s discuss each method in detail.1. implicitly wait()This timeout is used to specify the amount of time the driver should wait while searching for an element if it is not immediately present.The syntax is as follows:implicitly wait(long time, java.util.concurrent.time unit unit);When searching for a particular single element, the driver should pause page loading until the element has been found. If it doesn’t wait, then the timeout expires before throwing a no such element exception.Read article on Commons Exceptions in Selenium Webdriver: How to handle them?When searching for multiple elements, the driver should pause the page until at least one element has been found or the timeout has expired.Example:implicitly wait(20, time unit.SECONDS);In this statement, the web driver will wait for 20 seconds before proceeding to the next action.Code Snippet:import java.util.concurrent.time unit; import org.openqa.selenium.By; import org.openqa.selenium.web driver; import org.openqa.selenium.web element; import org.openqa.selenium.chrome.chrome driver; public class timeout example { public static void main(String[] args) { System.set property("webdriver.chrome.driver", "Path of Chrome Driver"); web driver driver = new chrome driver(); driver.get("https://www.ebay.com/"); // Implicit wait timeout for 20seconds driver.manage().timeouts().implicitly wait(20, time unit.SECONDS); driver.find element(By.xpath("//input[@id='gh-ac']")).send keys("Mobile"); //xpath for search button web element search = driver.find element(By.xpath("//input[@id='gh-btn']")); search.click(); } } On executing the code above, the driver will wait for 20 seconds on the particular website even if the web element is not found.Note: If a user wants to increase the implicit wait timeout, it should be done carefully as this will affect the test run time.2. set script timeout();This is used to set the amount of time the web driver must wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.Syntax:set script timeout(long time,java.util.concurrent.time unit unit);The default timeout for set script timeout method is zero. If you do not set time, then there are chances that execute async script method may fail because the java script code may take more than the time allotted. To avoid such failures, set the set script timeout. This is mainly used for Javascript objects and executors.Example:// set script timeout for 10 seconds driver.manage().timeouts().set script timeout(10, time unit.SECONDS); ((javascript executor) driver).execute script("alert('hello world');"); ((javascript executor) driver).execute async script("window.set timeout(arguments[arguments.length - 1], 500);");In the example above, if the time is not used, then there will be an error stating: “Timed out waiting for async script result”. To avoid this error, one should use set script timeout.3. page load timeout in selenium this sets the time to wait for a page to load completely before throwing an error. If the timeout is negative, page loads can be indefinite.Syntax:page load timeout(long time,java.util.concurrent.time unit unit);This timeout is applicable only to driver.manage() and driver.navigate.to() methods.Example:public class page load test { public static void main(String[] args) { System.set property("webdriver.chrome.driver", "Path of driver"); web driver driver = new chrome driver(); // set the time of 30 seconds for page to complete the loading driver.manage().timeouts().page load timeout(30, time unit.SECONDS); driver.get("https://www.google.com/"); } }In the code above, if your page does not load within 30 seconds, web driverException will be thrown.Selenium Timeouts must be included to create effective, comprehensive and seamlessly running test cases. This article intends to help in this regard by briefly explaining how Timeouts work, and how they can be incorporated into Selenium test scripts.To know more about Automation, refer to this piece on Selenium Automation. Was this post useful? Yes, Thanks Not Really We're sorry to hear that. Please share your feedback so we can do better Thanks a lot for your feedback! Tags Automation Testing Selenium Selenium Webdriver Related Articles Effective ways to use XPath in Selenium Learn to effectively write XPath in Selenium to find elements. Read the tutorial to understand the b... Learn More How to run Selenium tests on Chrome using chrome driver Dive-in to get started with Selenium testing for Chrome browser using chrome driver. Learn More Testing on Virtual Machines (VM): Move to Real Device Testing What is Virtual Machine Testing? Virtual Machines (VMs), often emulators and simulators, are essenti... Learn More Featured Articles Effective ways to use XPath in Selenium How to run Selenium tests on Chrome using chrome driver Testing on Virtual Machines (VM): Move to Real Device Testing Automation Testing Selenium Selenium Webdriver Run Selenium Tests on 2000+ Browsers & Devices Get Started Free
RELATED ARTICLES