- Advertisement -
In this Lesson we will understand the Waits Methods. It is most essential to understand these methods, as most common problem we face during automation is wait for certain condition to exists. for an instance, whether control is exists or not. or wait for certain control to visible or enable on the webpage.
Apart from it we do run some scripts such as javascripts, Jquery ( most commonly used in web applications) and render the part of web where we really don’t know what time it will take to execute and return the results. Waits plays very important role here.
In This post we will learn Three basic Waits Method which Selenium provides. if you Looking for more advance solution for Waits to handle Ajax calls please refer post “How to Handle Ajax call in Selenium Webdriver?”
In this Lesson we will look for following Waits:
[checklist]
- Explicit Waits
- Implicit Waits
- Fluent Waits
- Webdriver Waits
[/checklist]
1) Explicit Waits :
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.
Code:
- Advertisement -
WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
2) Implicit Waits:
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
Code:
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
3) Using FluentWait :
Selenium webdriver provides FluentWait option to handle uncertain waits. The advantage of this approach is that element polling mechanism is configurable. The code example below waits for 3 second and polls for a textarea every 100 milliseconds.
FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA")); \\ define element for which you want to poll fluentWait.pollingEvery(300, TimeUnit.MILLISECONDS); \\ it will ping for every 3 sec fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS); \\ max time out fluentWait.until(new Predicate<By>() { public boolean apply(By by) { try { return browser.findElement(by).isDisplayed(); } catch (NoSuchElementException ex) { return false; } } }); browser.findElement(By.tagName("TEXTAREA")).sendKeys("text to enter");
4) Using WebdriverWait:
Another approach is to use ExpectedCondition and WebDriverWait strategy. The code below waits for 20 seconds or till the element is available, whichever is the earliest.
public ExpectedCondition<WebElement> visibilityOfElementLocated(final By by) { return new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver driver) { WebElement element = driver.findElement(by); return element.isDisplayed() ? element : null; } }; } public void performSomeAction() { .. .. Wait<WebDriver> wait = new WebDriverWait(driver, 20); WebElement element = wait.until(visibilityOfElementLocated(By.tagName("a"))); .. }
Always try to avoid the Thread.sleep or any sleep method method in your code. if you are facing problem about slow execution of application, internet speed is slow. then try to put the wait in more logical way such as put wait in loops for certain seconds and try to perform the action again. this will minimize your execution time.
please let us know about Selenium webdriver series and we continuously improving the lessons.