Explicitly Wait in Selenium WebDriver

Explicitly Wait in Selenium WebDriver

Explicitly wait time is configured to look for only a particular WebElement in the DOM before the test proceed further. On the other hand, Thread.sleep() sets the condition to wait for the specified period of time which makes the test much slower. The main goal of using explicitly wait is to execute the automation testing faster. The following code was taken from the test:

//wait for 2 secs
Thread.sleep(2000);

Here note that static Thread is good for visualizing the actions while performing the test for the front-end. I highly recommend not to use static wait because it always makes the test slower. A Static wait is fixed meaning that 2 seconds should be waited before it goes to the next element. 

//stop resharing
driver.findElement(By.cssSelector("div.Dp div")).click();
One of the ways, it can be accomplished with ExpectedConditions. Below it is shown taken from the real test:
//wait for the element 35 secs max
WebDriverWait wait = new WebDriverWait(driver, 35);
WebElement elementForLogOut =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a/
span")));
elementForLogOut.click();

Here notice in the above code snippet that driver waits maximum 35 seconds before it throws a TimeoutException error; else it continues with the next step. In every 500 milliseconds, WebDriver calls by default the ExpectedConditions until it returns the WebElement successfully. However, a successful return is always Boolean true or not null values for all ExpectedConditions types. ExpectedCondition is an interface used to implement the conditional wait for a specific WebElement. Thus, implicitly wait is overridden exclusively for the elements.

Note: XPath always makes the test slower because it searches for the element backward and forward on the DOM whereas CSS Selector searches for forward in the DOM.It is wise to use CSS Selector ( with jQuery ) to speed up the test performance.

Comments

Post a Comment

Popular posts from this blog

Implicitly Wait in Selenium WebDriver

How to take a screenshot in Selenium WebDriver