Saturday, May 20, 2017

How to use explicit wait in selenium webdriver | David Tzemach

תוצאת תמונה עבור ‪selenium webdriver explicit wait‬‏

In addition to the basic method of "Implicit" wait, selenium WebDriver also provides a more advanced method to handle synchronization issues that may rise during the tests execution, this method called "Explicit wait".

The major benefits of using the explicit wait:
  1. We can implement it against specific elements and other cases (Implicit wait is relevant to all elements in the DOM).
  2. We can write a specific condition to be enforced prior proceeding of the test execution.
  3. Not like the implicit wait, the explicit wait is not relevant to the entire Driver life cycle.
To define an explicit wait, selenium WebDriver provides two classes:

The WebDriverWait Class

This class provides the basic instruction to the driver, including notifications, polling intervals and how to handle exceptions during code execution. 

Class Member
Operation Type: Get/Set
Define the message to be displayed when time expires (We can use it also for determining the notification syntax).
Operation Type: Get/Set
Determine how often the condition is evaluated against the element (Default value is 500 milliseconds)
Operation Type: Get/Set
Determine the time to wait for a condition to return result (Default value is 500 milliseconds)
This method will allow us to ignore a specific type of exceptions returned from a condition.
Throw a WebDriverException with the specified message

The Expected Condition Class

This class provides a set of common conditions that we can use in addition to the WebDriverWait command, this condition will tell the driver to wait prior proceeding with the test execution.

Here are the major class members (Feel free to explore the remaining once):
Class Member
ElementExists ()
Validate that a given element is available in a DOM a page.
ElementIsVisible ()
Validate that a given element is available and visible on the DOM a page
ElementToBeClickable (IWebElement)
Validate that a given element is available and visible on the DOM so the user can click on it.
ElementToBeSelectable (IWebElement)
Validate if a given element is selected or not
FrameToBeAvailableAndSwitchToIt (By)
Validate whether a specific frame is available to switch to, if 'true', then the driver will move to the specified frame
InvisibilityOfElementWithText ()
Used to validate that an element with text is invisible/not present on the DOM
PresenceOfAllElementsLocatedBy ()
Used to Validate that all elements that matched to a specific locator are available on the web page
TextToBePresentInElementValue (By, String)
Used to validate if a given text is present in an element attribute value
TitleContains ()
Validate that the title of the page contains a specific string
Titles ()
Validate the title of the page
UrlContains ()
Validate that the page URL against a predefined string

Practical Examples of Explicit wait

In the following code snippet, I will use the ExpectedCondition Class to implement different examples of explicit waits (using the WebDriverWait Class):


public void ExplicitWait()
//Creating a new WebDriver instance
IWebDriver Firefox = new FirefoxDriver();

//Navigate to a web page

//Create instance of the WebDriverWait Class
WebDriverWait driverwaitinstance = new WebDriverWait(Firefox, TimeSpan.FromSeconds(20));

Using the "TitleContains" condition

The following code will validate that the Web Page title contains a specific syntax.

driverwaitinstance.Until(ExpectedConditions.TitleContains("David Tzemach's Blog"));

Using the "URLContains" condition

The following code will validate that the Page URL contains a specific syntax.


Using the "TextToBePresentInElement" method

The following code will validate that a specific element contains a specific text that we can define as a condition.

IWebElement textvalidation = Firefox.FindElement(By.XPath("//a[contains(text(),'Quality Assurance')]"));
driverwaitinstance.Until(ExpectedConditions.TextToBePresentInElement(textvalidation,"Quality Assurance"));

Using the ElementExists method

In this code snippet, we will use the ElementExists method to validate if a specific element is available in the DOM.

driverwaitinstance.Until(ExpectedConditions.ElementExists(By.XPath("//a[contains(text(),'Quality Assurance')]")));

Using the isElementPresent method

The following code will check for the element presence in the DOM of a page or not. Just remember that although the method can return 'True' (Element is present in the DOM), that does not guarantee that the element is visible, therefore we will use this method when we don't care about the element visibility.

driverwaitinstance.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath("//a[contains(text(),'Quality Assurance')]")));

Using the isElementVisible method

The following code is used for checking that a specific element is both present and visible (With and Height > 0) in DOM.

The First option is to check the visibility of a single element:

y Assurance')]")));

The second option is to check the visibility of multiple elements:

The return value of this method is an IWebElement list of the visible elements

IList<IWebElement> ListOfElements = driverwaitinstance.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//option")));

Using the isElementClickable method

The following code is used for checking that a specific element is both Visible and Enabled.

driverwaitinstance.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[contains(text(),'Quality Assurance')]")));

Using the InvisibilityOfElementLocated method

We can use this method to validate the invisibility of an element in DOM, the return type is bool (True if the element is not displayed, false if visible).

if (driverwaitinstance.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("epic-nav-item-heading"))) == true)
{//Perform Task A}
{//Perform Task B}

Using the InvisibilityOfElementWithText method

Another option that we can use to validate the invisibility of an element in DOM is using the InvisibilityOfElementWithText, the return type is bool (True if the element is not displayed, false if visible).

if (driverwaitinstance.Until(ExpectedConditions.InvisibilityOfElementWithText(By.ClassName("epic-nav-item-heading"), "Home")) == true)
{//Perform Task A}
{//Perform Task B}

No comments:

Post a Comment

My Presentations