No votes yet.
Please wait...

Waits are characteristic features of any UI test for dynamic applications. They facilitate the synchronization of AUT and the testing script. An application’s response to commands is much slower than script performance. That’s why in scripts, you have to wait for a particular application state to interact with it.

Using wait commands is important when running tests in Selenium. It allows you to avoid problems that may occur due to changes in the time delay.

Nevertheless, waiting, especially in automated tests, isn’t always a good thing. But it’s possible to reorganize your work using a favorable method. Selenium WebDriver has three commands for implementing waits in tests: Implicit Wait, Explicit Wait, Fluent Wait.

In this article, we’ll focus on the first two commands.

Implicit Wait tells the Selenium WebDriver to wait the required time, only then generate an exception. After defining the time, the WebDriver waits for an item before generating an exception.

Once the commands are executed, Implicit Wait is valid for as long as the browser is open. The default value is 0, so an exception will be instant.

Syntax: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)

Setting Implicit Wait:

Setting Implicit Wait

Setting Implicit Wait

Implicit Wait extends the time frame of the test script execution. Commands must wait a certain amount of time before returning to test execution.

Explicit Wait is implemented when the condition is met, after which the test will be running again.

Syntax: WebDriverWait wait = new WebDriverWait(driver,10)

The list of conditions applied by ExpectedConditions:

• alertIsPresent()
• elementSelectionStateToBe()
• elementToBeClickable()
• elementToBeSelected()
• frameToBeAvaliableAndSwitchToIt()
• invisibilityOfTheElementLocated()
• invisibilityOfElementWithText()
• presenceOfAllElementsLocatedBy()
• presenceOfElementLocated()
• textToBePresentInElement()
• textToBePresentInElementLocated()
• textToBePresentInElementValue()
• titleIs()
• titleContains()
• visibilityOf()
• visibilityOfAllElements()
• visibilityOfAllElementsLocatedBy()
• visibilityOfElementLocated()

Example of Explicit Wait test:

private WebDriver driver;
private WebDriverWait wait;
private WebElement exampleElement;

@BeforeMethod(alwaysRun = true)
public void setUpDriver() {
//Set up driver
ChromeOptions options = new ChromeOptions();
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
//Set explicit wait
wait = new WebDriverWait(driver, 10);
}

@Test(description = “Open url”)
public void openUrl() {
//Open browser
driver.manage().window().setSize(new Dimension(1280, 970));
//Get url
driver.get(“https://some-site.com”);

//Wait until blockUI disappear
wait.until(ExpectedConditions.invisibilityOfElementLocated
(By.cssSelector(“element”)));
wait.until(ExpectedConditions.invisibilityOf(exampleElement));

//Search an element
WebElement element = driver.findElement(By.id(“some-id”));
Assert.assertTrue(element.isDisplayed());

//Click element
element.click();

//Wait until alert is present
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();

}

@AfterMethod(alwaysRun = true)
public void closeBrowser() {
//Close browser
driver.quit();
}

In the automated tester’s work for an independent quality assurance organization, it is very important to apply Selenium wait commands. This will save a lot of time and provide a great approach to solving many software testing problems.

Leave A Comment