Selenium For Beginners

Share this post

User's avatar
Selenium For Beginners
Synchronize the test script with the site using explicit waits
Waits

Synchronize the test script with the site using explicit waits

Alex Siminiuc's avatar
Alex Siminiuc
Apr 17, 2023
∙ Paid

Share this post

User's avatar
Selenium For Beginners
Synchronize the test script with the site using explicit waits
Share

The following test script, when executed repeatedly, fails randomly all over the place:

@Test
public void searchTest() {

   driver.get(HOME_PAGE_URL);

   String homePageUrl = driver.getCurrentUrl();
   Verify.verify(homePageUrl.equalsIgnoreCase(HOME_PAGE_URL));

   
   WebElement searchBox = driver.findElement(SEARCH_BOX_ID);
   searchBox.sendKeys(KEYWORD);

   WebElement searchButton = driver.findElement(SEARCH_BUTTON_ID);
   searchButton.click();


   String resultsPageUrl = driver.getCurrentUrl();
   Verify.verify(resultsPageUrl.startsWith(RESULTS_PAGE_URL), 
       "results page url does not start with " + RESULTS_PAGE_URL);

   Verify.verify(resultsPageUrl.contains("query=" + KEYWORD), 
       "results page url does not contain query=" + KEYWORD);

   Verify.verify(resultsPageUrl.contains("searchType=smart"), 
       "results page url does not contain searchType=smart");


   WebElement resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
   String foundText = resultsFoundLabel.getText();

   Verify.verify(foundText.contains("1 to 10"), 
       "results count does not include 1 to 10");


   String countText = StringUtils.substringBetween(foundText,"of "," results");
   int totalCount = Integer.parseInt(countText);
   
   Verify.verify(totalCount > 0, "totalCount is not > 0");


   WebElement searchBroaden = driver.findElement(BROADEN_SEARCH_XPATH);
   searchBroaden.click();

   Verify.verify(driver.getCurrentUrl().contains("searchType=bkw"), 
       "results page url doesnt include searchType=bkw");


   resultsFoundLabel = driver.findElement(RESULTS_FOUND_XPATH);
   foundText = resultsFoundLabel.getText();

   countText = StringUtils.substringBetween(foundText, "of ", " results");
   int newTotalCount = Integer.parseInt(countText);

   Assert.assertTrue(newTotalCount > 0, newTotalCount + " is not > 0");

   Assert.assertTrue(newTotalCount > totalCount, 
       newTotalCount + " is not > than " + totalCount);

}

Sometimes, it fails after the home page is loaded when trying to find the search box.

Other times, it fails after the results page is loaded when finding the results found label. Or after broadening the search and finding again the results found label.

The reason for the failures is that the site does not load fast enough sometimes.

driver.findElement() tries finding the element in the page. If the element is present, it is found and used. If the element is not present, an exception is generated. Unfortunately, driver.findElement() does not wait for the element if the element is not in the page right away.

Keep reading with a 7-day free trial

Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Alex Siminiuc
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share