Author - StudySection Post Views - 55 views
wait methods in Selenium

Wait Methods in Selenium

Wait Methods are a group of methods that pause between execution statements. They’re beneficial for AJAX applications. AJAX stands for Asynchronous JavaScript. It is an XML which is a technique for designing asynchronous applications. This kind of application does not have to reload the web page while exchanging data with a server and updating parts of the web page. As a result, when a browser loads a web page, the WebElements may load at different time intervals. If a WebElement is not identified by a specific time, an exception will cause the Test Script to fail. Nevertheless, an automation engineer can avoid this problem using Wait Methods but the most utilized methods are Implicit Wait and Explicit Wait.

Implicit Wait

An Implicit Wait Method is used to implement a default waiting time for each Test Step within the entire Test Script. While waiting, WebDriver continues polling the Document Object Model (DOM) for the WebElement. If the WebElement is placed before the default time then the next Test Step is executed. However, if the WebElement isn’t located within the specified time then an exception shows up. The below-given screenshots display a description and syntax for Implicit Wait.

Screenshot 01: Above screenshot shows the Implicit Wait Syntax and Description

Screenshot 02: Above screenshot shows the Implicit Wait 5 Seconds
Implicit wait accepts the below given 2 parameters:

  1. time – accepts the time as an integer value
  2. unit – accepts the time measurement in terms of SECONDS, NANOSECONDS, MINUTES, MILLISECONDS, MICROSECONDS, HOURS, and DAYS

Note: Usually, an Implicit Wait Method is provided before loading the web page within the setUp() method

Explicit Wait

Explicit Wait Methods are used to interrupt execution until time has elapsed or an expected condition is met. That condition must be satisfied before moving to the next Test Step. Selenium WebDrvier offers a WebDriverWait class and an ExpectedConditions class to assist Explicit Wait. It’s important to know that Explicit Waits are limited to a specific WebElement.

The WebDriverWait class creates an object reference and uses an instance of WebDriver. Also, a maximum number of seconds is added for execution inactivity. If the WebElement isn’t found then an exception is thrown. The below-given screenshot shows a description and syntax for WebDriverWait:

web driver

Screenshot 03: Above screenshot shows the WebDriverWait Syntax and Description

web driver wait

Screenshot 04: Above screenshot shows the WebDriverWait- TimeOut In 5 seconds
In the above example, an object reference of wait is created for WebDriverWait while the driver is the reference object for WebDriver. A maximum of 5 seconds is provided to locate the WebElement. Afterward, an automation engineer has to inform WebDriver to wait until an expected condition is met. The ExpectedConditons class supplies many methods for using these scenarios that may occur before executing the next Test Step. Here’s a screenshot that displays a few ExpectedConditions methods.

timeout

Screenshot 05: Above screenshot shows the Methods via ExpectedConditions Class

Screenshot 06: Above screenshot shows the ExpectedConditions Example
The ExpectedConditions class is accessed after writing the WebDriverWait reference “wait, dot operator, then until() method. Subsequently, many methods are available when writing dot after ExpectedConditions. The above statements look ahead to whichever happens first: wait for seconds or until the element is clickable. In alphabetical order, below given is a list of additional methods that are not shown in the screenshot

  1. alertIsPresent()
  2. elementSelectionsStateToBe()
  3. elementToBeClickable()
  4. invisibilityOfTheElementLocated()
  5. invisibilityOfTheElementWithText()
  6. presenceOfAllElementsLocatedBy()
  7. presenceOfElementLocated()
  8. textToBePresentInElement()
  9. textToBePresentInElementLocated()
  10. textToBePresentInElementValue()
  11. titleIs()
  12. titleContains()
  13. visibilityOf()
  14. visibilityOfAllElements()
  15. visibilityOfAllElementsLocatedBy()
  16. visibilityOfElementLocated()

The following is a table summarizing Implicit and Explicit Wait:

wait methods

People having good knowledge of Financial accounting can get an Accounting Certification Exams from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.

Leave a Reply

Your email address will not be published.