Web sites frequently display widgets and ads on their pages or use AJAX calls to improve response time. Hence you don't have access to all web elements present in the page at once. Testers regularly use wait statements to wait for elements in DOM and test the behavior of the application. Below I compare the three types of waits. I'll try to make it as comprehensive as I can to give you guys a quick understanding at a glance.
Explicit Wait | Fluent Wait | Implicit Wait |
---|---|---|
Explicit wait only waits for the required element till the specified time runs out. | Fluent wait will wait for the required element until it finds it or the time runs out. | The implicit wait will wait for the specified amount of time for every element in the web page. |
Explicit wait requires knowledge of prebuilt ExpectedConditions that must be met. All you need to know is the available ExpectedConditions options and you can master these three lines of code. WebDriverWait wait = new WebDriverWait(driver,30); WebElement e = driver.findElement(By.xpath(".//*[@id='mapDiv']/div/div/div[9]/div/div/div/a")); WebElement e1= wait.until(ExpectedConditions.visibilityOf(e)); |
Fluent wait gives testers the option to define their own ExpectedConditions in form of a function. You can override the function in wait.until to check pretty much any condition you want to fulfill before accessing the web element. FluentWait @Override |
Implicit wait is easy to understand and consists of just one line of code. driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); |
Explicit wait makes debugging shorter as it only waits for the required element, not all of them at once. Take the above line of codes as an example, web driver will wait for full 30 seconds no matter it finds the element or not. | Fluent wait makes debugging as short as it can by returning the control as soon as it finds the element. Consider the lines of code in above example, web driver will look for the web element every 5 seconds and will return the control immediately after it finds the element. In case it doesn't find the element, the control will be returned after the timeout limit expires. | The implicit wait can make debugging issues tiresome since it has to wait for the specified time for every element. Imagine the amount of time it will take your test case to run and conclude if it's unable to find more than one web elements. |
Explicit wait is efficient way to wait for web element and can be used when you can utilize one of the prebuilt ExpectedConditions. | Fluent wait is the most efficient of all. You don't always have to wait for the TimeOut attribute to expire and continue with the execution of test case. It is also the Hobson's choice when you want to implement your own version of ExpectedConditions. | The implicit wait can be very handy when you want to view the execution of every step in your test case while you are learning to code. |
No comments:
Post a Comment