Waiting mechanism – Selenium 4 Changes, pageLoadTimeout(), Ajax calls, loading process and Synchronization Problem (Session 17)

Waiting mechanism in Selenium

Waiting mechanism in Selenium and Java can be categorized as below:
  • Demonstrate a program which don’t use waiting mechanism to understand the importance of waiting mechanism in Selenium
    • NoSuchElementException
  • Using Thread.sleep() in Java to overcome the waiting problems
  • Implicit Wait  – Instead of halting the program till the specified time is reached, Implicit wait will wait for all the web elements dynamically (i.e. Global wait)
    • driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
  • Explicit Wait – Instead of waiting for all the statements in the program, Explicit wait will wait only for the specific web element
    • WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
    • WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(“Facebook”)));
    • Also demonstrate ‘ElementToBeClickable’
  • Fluent Wait  – Use Duration.ofSeconds(30) in the deprecated methods
    • Copy the FluentWait code from Selenium API Documentation
    • Import Function from google.common.base
    • Import NoSuchElementException from selenium

Handling Ajax Calls in Selenium

  • Web Pages make Ajax calls, to retrieve small amount of data from server without the need for reloading the page
  • Example for Ajax call
  • Selenium WebDriver handles Ajax calls using Waiting mechanism
    •  Implicit Wait
    •  Explicit Wait
    •  Fluent Wait
    •  etc.
  • Practical demonstration

Solving Synchronization Problem in Selenium

  • Selenium is faster and it won’t wait for any element on the web page by default
  • If the element is not available, Selenium will simply throw NoSuchElementException
  • To handle this problem, we have to use Waiting Mechanism in Selenium:
    • Implicit Wait
    •  Explicit Wait
    •  Fluent Wait
    •  etc.

Waiting for an Element to be displayed on the page

  • Sometimes, UI element won’t be displayed on the page immediately and make take time to display after performing an action
  • As Selenium WebDriver cannot wait for the UI element, we have to explicitly wait until the UI element is displayed on the page.
  • Practical Demonstration – omayo

Waiting for an Element to be click-able

  • Sometimes, we have to wait for the UI element to be clickable, as it won’t be clickable by default
  • Practical Demonstration – verifalia email checker

Waiting for an Alert to be present

  • Sometimes, we have to wait for the alert before we perform any operations on the alert
  • Practical Demonstration – omayo
  • TimeoutException will be displayed if the alert is not displayed, despite of waiting

Loading Process Icon and How to Wait for the required UI element on the web page

  • We get an Exception when we try to perform operation element, which is not displayed due to the loading icon
  • In this case, if we wait for the required UI element, things may or may not work
  • Instead, we have to wait for the loading icon to disappear by writing some programming logic
    • We have to pause the debugger if required
  • Practical Demonstration – Internet Heroku

pageLoadTimeout and TimeoutException – Selenium Exception Type

  • By default Selenium WebDriver waits indefinitely, until the page to load without any time limit
  • But we can set time limit for the web page to load using pageLoadTimeout()
    • driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(x))
    • If the page doesn’t load in the specified time, we will get TimeoutException
  • Practical Demonstration
    • Omayo website page takes 4 seconds to load
    • But if we set the limit to 2 seconds to load using pageLoadTimeout(), we get this TimeoutException

Arun Motoori

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

Open chat
Contact Us on Whatsapp