Handle Synchronization In Selenium PHP Using Implicit and Explicit Wait

Himanshu Sheth

Posted On: March 26, 2021

view count18567 Views

Read time21 Min Read

One of the significant challenges with automation testing is dealing with web elements that are loaded dynamically through AJAX (Asynchronous JavaScript And XML) and JavaScript. The Selenium WebDriver does not hold the responsibility of tracking the DOM’s real-time and active state (Document Object Model). Handling synchronization in Selenium becomes important with dynamically loaded web elements as they may load at different time intervals. That’s also where Implicit and Explicit Wait in Selenium comes into play.

image3

All of us are aware that intermittent connectivity (or internet) problems could break so-called well-designed test-cases, possibly resulting in ‘no such element found’ or ‘element not clickable’ exceptions! The asynchronous nature of web platforms poses challenges concerning synchronization in Selenium test automation.

In this Selenium PHP tutorial: How To Handle Synchronization In Selenium PHP Using Implicit and Explicit Wait, we deep dive into the usage of mechanisms involving synchronization in Selenium by resolving timing-related issues (or rather, wait commands in Selenium) that can occur in Selenium test automation.

Introduction to Wait Commands in Selenium

The Selenium WebDriver does not track the state of the DOM in real-time since the Selenium WebDriver generally has to block APIs, whereas the web platform is of asynchronous type.

Synchronization issues occur when operations are performed on a web element that is not yet present in the DOM, or it is not a state to accept commands (e.g., not visible, not clickable, etc.).

The possibility of race conditions with Selenium test automation implementation is likely when the user has instructed the browser to navigate to a particular page, and an ElementNotVisibleException is encountered when trying to locate a web element. The occurrence of race conditions between the browser and the Selenium WebDriver script can be averted using Selenium’s wait commands.

Synchronization in Selenium can be handled using wait commands where you wait for a particular duration or wait on a specific condition to occur. In further sections of this tutorial, we look into the different types of wait commands in Selenium PHP for handling timing (and synchronization) related issues during web automation testing.

Why Do We Require Wait Commands in Selenium?

Most modern-day websites use frameworks (or libraries) such as React, Angular, etc., for front-end development. With these frameworks, the contents on a web page are updated dynamically with interface components. The components are calculated using JavaScript interactions or AJAX requests that are sent to the server.

Though selective and dynamic loading of web elements on a page reduces the page-load time, web automation tests can fail when operations are performed on web elements that are still loading or not visible or not enabled. Interacting with such elements would result in Selenium throwing exceptions (e.g. ElementNotInteractableException, ElementNotVisibleException, etc.).

In the code snippet shown below, we execute the following test scenario:

  • Open the test URL.
  • Locate the ‘Get it on Google Play’ button.
  • Click on the button.
  • Assert if the Google Play window is not open.

FileName – WhyWaitTest.php

We used the PHPUnit framework for test automation. On the first run, the test execution was successful, whereas the second run resulted in ElementNotInteractableException. Hence, there is no guarantee about the test scenario’s behavior (i.e., it could pass or raise ‘different types’ of exceptions).

image1

Successful test execution

image2
Test Execution resulting in ElementNotInteractableException

This Selenium WebDriver script might be intermittent as there is no assurance about the web elements or events that are triggered asynchronously, without blocking or explicitly waiting on those events. This is where wait commands in Selenium PHP testing can come handy since it helps resolve issues with synchronization in Selenium web automation.

Types of Waits for Handling Synchronization in Selenium PHP

Implicit and Explicit Wait in Selenium are the two major types of waits supported in PHP. Irrespective of the type of wait being used, the major intent of using the wait is to have the Selenium web automation task execution elapse a certain duration before the script execution proceeds to the next step.

You can also learn how To handle waits in Selenium from the below video.

Implicit and Explicit Wait in Selenium PHP 1

Waits in Selenium

Explicit Wait in Selenium PHP

Explicit Waits (also termed ‘smart waits’) in Selenium halt the program execution or freeze a thread until the occurrence of a certain condition. The status of the condition is checked at a certain frequency until the wait timeout has elapsed. If the condition is satisfied, the execution proceeds to the next step; else, it results in a timeout.

Sleep is also a type of Explicit Wait, but it sets the condition to an exact time period to wait. This is irrespective of whether the required condition (e.g., presence of web element in DOM, visibility of web element, etc.) is satisfied or not. We would not recommend using sleep() for synchronization in Selenium since it elongates the test execution time.

In Selenium PHP, the explicit wait is possible with prepared conditions that can be passed to the until() method. It is also possible to use callable methods with the until() method to use ‘custom conditions’ instead of prepared conditions.

Some of the sample conditions for explicit wait in Selenium WebDriver are:

  • Waiting for the web element that is present in the DOM to be visible (i.e., visibilityOfElementLocated)
  • Waiting for the web element to be clickable (i.e., elementToBeClickable)
  • Waiting for the presence of the web element on the DOM of a page (i.e., presenceOfElementLocated)

Here is the sequence of steps that occur when an explicit wait is triggered:

  • The Wait Time, Frequency for condition checking, and Condition are entered as a part of the explicit wait command.
  • The Selenium wait API takes two parameters – wait-time: Maximum time to wait for the occurrence of the condition and frequency-in-ms: Wait frequency at which the condition is checked.
  • The condition mentioned in until is checked till the occurrence of ‘wait-time’ or till the condition is met. On the occurrence of the condition, the execution immediately proceeds with the next line of execution.

The major difference between implicit and explicit wait in Selenium is that implicit wait is applicable till the time Selenium WebDriver instance is alive, whereas explicit wait is applicable on the requisite web element on the page. As explicit waits allow waiting for the condition to occur, they are better-suited for synchronization of the state between the browser and DOM and the Selenium WebDriver script.

Implicit and Explicit Wait in Selenium PHP

Explicit Wait in Selenium

WebDriverWait and WebDriverExpectedCondition in Selenium PHP

WebDriverExpectedCondition and WebDriverWait are the key facilitators for realizing explicit wait with PHP and Selenium. For PHP, the expected conditions are available in the WebDriverExpectedCondition class. The Selenium WebDriver looks for the web element along with the occurrence of conditions at the frequency configured in the explicit wait command.

In case of a potential issue with the web element, an exception is thrown. Below are some of the common exceptions that occur when synchronization in Selenium is not handled properly:

  • NoSuchElementException – The required web element is not a part of the DOM.
  • ElementNotVisibleException – The required web element is present in the DOM, but the element is not ready for any operation (e.g., click) as it is not yet visible.
  • ElementNotSelectableException – The required web element is present in the DOM, but it cannot be selected. Hence, the element cannot be interacted with.
  • NoSuchFrameException – This exception is thrown when the Selenium WebDriver cannot find the frame on the page to which it needs to switch.
  • StaleElementReferenceException – This exception is thrown when reference is made to a web element whose state is stale. The element is no longer on the DOM of the page.
  • NoAlertPresentException – This exception is thrown when the Selenium WebDriver tries switching to an alert that is no longer available (or present).
  • NoSuchWindowException – Selenium WebDriver is attempting to switch to a window that is invalid or no longer available.

Become an expert in PHP, you can acquire a benchmark industry certification specializing in core PHP programming skills and expand your opportunities to advance your career in PHP automation testing.

selenium php

‘Available Conditions’ to Wait for in Selenium PHP

The WebDriverExpectedCondition class in Selenium PHP contains a set of conditions that can be used with WebDriverWait in WebDriver tests. Here are some of the methods that are a part of the WebDriverExpectedCondition class in PHP:

WebDriverExpectedCondition

Explanation

Wait for Title

titleIs

Exception for checking the title of the page.

titleContains

Exception for checking the substring of the page title.

titleMatches

Exception for checking if the page title matches a given regular expression.

Wait for URL

urlIs

Exception for checking the URL of the page

urlContains

Exception for checking if the URL of a page contains a particular substring.

urlMatches

Exception for checking if the URL matches with the given regular expression

Elements and their text

presenceOfElementLocated

Exception for checking if the element is present in the DOM. Even if the web element is present, it does not necessarily mean that the element is visible.

presenceOfAllElementsLocatedBy

Exception for checking if there is at least one web element present on the page.

elementTextMatches

Exception for checking if a certain regular expression matches with the text in the web element.

elementTextIs

Exception for checking if the particular text ‘exactly matches’ with the text present in the specified web element.

elementTextContains

Exception for checking if the particular text is present in the specified web element.

Elements visibility and invisibility

visibilityOfElementLocated

Exception for checking if the element is not only present in the DOM but is also visible. The element’s height and weight should be greater than zero.

visibilityOf

Exception for checking if the required element that is present in the DOM is visible as well.

invisibilityOfElementLocated

Exception for checking if the web element is either not in the DOM or not visible.

invisibilityOfElementWithText

Exception for checking if the web element with the specified text is either not in the DOM or not visible.

Frames, Windows, Elements, and Alerts

frameToBeAvailableAndSwitchToIt

Exception for checking if the particular iFrame exists or not. If it exists, the focus shifts to that particular iFrame.

elementToBeClickable

Exception for checking if the web element is present, visible, and enabled in the DOM so that ‘click’ operation can be performed on it.

alertIsPresent

Exception for checking if an alert box is present.

numberOfWindowsToBe

Exception for checking the number of opened windows.

Special conditions

elementValueContains

Exception for checking if a particular text is present in the ‘values’ attribute of the specified web element.

elementSelectionStateToBe

Exception for checking if a particular web element is selected.

elementToBeSelected

Exception for checking if a particular web element is selected.

stalenessOf

Wait till the time the specified web element is no longer attached to the DOM.

not

Exception to perform the logical opposite of the given condition.

Shown below is a sample snippet that shows the usage of the expected condition titleIs. The expected condition is that the page title of https://lambdatest.github.io/sample-todo-app/ should match ‘Sample page – lambdatest.com’

Snippet: Usage of titleIs with default wait of 30 seconds

Snippet: Usage of titleIs with wait of 10 seconds and condition checking frequency set to 500 ms

For more detailed information, refer to the WebDriverExpectedCondition of the Selenium PHP WebDriver document.

‘Custom Conditions’ to Wait for in Selenium PHP

Custom conditions for the explicit wait in Selenium PHP use callable methods (or functions) as a part of the until() method.

Snippet: Usage of custom conditions for explicit wait

In the code snippet shown above, the custom condition will work until more than two elements with the specified XPath appear on the page. If the elements are not found within the specified time duration (i.e., 10 seconds), the error ‘Could not locate more than 2 web elements’ will be displayed on the screen.

Explicit Wait for Achieving Synchronization in Selenium

To demonstrate the use of Explicit Wait for synchronization in Selenium PHP testing, we would perform the tests on cloud-based Selenium provided by LambdaTest. To get started, create an account on LambdaTest and note the user-name & access-key from the profile section on LambdaTest. The browser capabilities are generated using the LambdaTest capabilities generator.

The PHPUnit framework is used for testing. For downloading the necessary packages for testing, we create composer.json in the project’s root folder.

FileName – composer.json

Run composer require in the terminal and press Enter button twice to install the necessary packages.

Here is the test scenario that we will use for demonstrating the usage of available conditions and custom conditions in explicit wait for achieving synchronization in Selenium PHP.

Test Scenario

  1. Navigate to the URL https://phptravels.com/demo/ in Chrome on Windows 10.
  2. Click on the ‘Get it on Google Play’ button present on the page.
  3. Compare the Window title with the expected title.
  4. Assert if the titles do not match.
  5. Locate the web element with the link ‘Learn More.’
  6. Assert if the window title of the resultant page does not match with the expected title.

Implementation (Explicit Wait with Available Conditions)

FileName – Explicit_WaitTest.php

Code Walkthrough

Line (22 – 28): The create method of Remote WebDriver takes two parameters – the browser capabilities array which was generated using the LambdaTest capabilities generator and the LambdaTest Grid URL [@hub.lambdatest.com/wd/hub] on which the tests would be executed.

Line (68 – 69): Once the test URL https://phptravels.com/demo/ is open, we perform a vertical scroll by executing the JavaScript command window.scrollTo(0, 800).

Line (73 – 74): An explicit wait for 10 seconds is set with the wait being performed on the condition visibilityOfElementLocated for the web element with XPath – //p[.=’Get it on Google Play’].

We used the POM Builder Extension in Chrome to fetch the details of the required web element.

image5

The frequency at which the condition is checked is set to 500 ms.

Line (77): In the previous step, we used explicit wait with available conditions to ensure that the element is in the DOM and it is visible.

Since the element is visible, we now wait on the expected condition elementToBeClickable for the same element (i.e., Get it on Google Play button). This is to ensure that the click operation performed in the subsequent step works as expected.

Line (82): As we have already checked for the clickability of the web element, the ‘Get it on Google Play’ button is clicked.

Line (93 – 95): With the focus on the newly opened window with URL https://play.google.com/store/apps/details?id=com.phptravelsnative, we use explicit wait of 10 seconds with titleContains condition. It waits on the condition till the title does not contain the sub-string ‘PHPTRAVELS Native.’

If the title of the window under focus does not have the required sub-string, the wait will result in a timeout, thereby halting the execution.

Line (98 – 100): On the current page, we explicitly wait for 10 seconds on the available condition presenceOfElementLocated for the web element with XPath – //a[.=’Learn More’]. A click operation is performed on the ‘Learn More’ link.

image6

Line (104 – 106): The click on the ‘Learn More’ link opens the Support Page for Google Play. An explicit wait of 10 seconds with titleContains condition is passed to the until() method.
The execution proceeds to the next step when the window contains the substring ‘Games content ratings on Google Play.’

Execution

Run the following command on the terminal:

As seen in the output snapshot, the test was successfully executed. The execution steps can be witnessed by visiting the Logs on LambdaTest for the particular Test ID and Build ID.

image8
unnamed (1)

Implementation (Explicit Wait with Custom Conditions)

FileName – Custom_Explicit_WaitTest.php

Code Walkthrough

Lines (67 – 69): The use keyword (in the callable function for implementing custom conditions) lets you access the Selenium WebDriver (i.e. $driver) inside the closure {…}

Lines (70 – 83): In the closure (i.e., the function implemented under the until condition), the findElements Selenium API is used for checking the count of the element with XPath – //p[.=’Get it on Google Play’]

On successful execution, the element count should be 1. Since variables inside the closure are not accessible outside of this namespace, we use the global variable $GLOBALS[‘ELEM_STATUS’] to set the status. If the required web element is not found within the wait time of 10 seconds, an error message ‘Error locating the Google Play button’ is displayed, and execution is stopped.

Lines (104 – 111): The strpos() function is used for finding the occurrence of the string “PHPTRAVELS Native” inside the current window title.

Rest of the implementation is the same as the one used for the demonstration of ‘Explicit Waits with available conditions’.

Execution

Here is the execution snapshot, which indicates the tests executed successfully.

automation

Implicit Wait in Selenium PHP

Implicit wait tells the Selenium WebDriver to poll the DOM for a certain element(s) if the element(s) is/are not immediately available. If the web element is not found in the DOM, a “No such element” exception is thrown.

The default setting for the implicit wait is 0. Implicit and Explicit wait in Selenium differs in terms of how they are set for the lifetime of the WebDriver object instance. Here is the syntax of the implicit wait, which is used for synchronization in Selenium:

Implicit Wait for Achieving Synchronization in Selenium

For demonstrating the use of Implicit Wait commands in Selenium, we use the following test scenario:

Test Scenario

  1. Navigate to the URL https://phptravels.com/demo/ in Chrome on Windows 10.
  2. Locate the ‘Blog’ link on the page.
  3. Compare the Window title with the expected title.
  4. Assert if the titles do not match.

Implementation

FileName – Implicit_WaitTest.php

Code Walkthrough

Lines (69 – 70): The ‘blog’ web element on the test URL is located using the XPath //div[@class=’yx-njp’]//a[.=’Blog’].

An implicit wait of 20 seconds is added before the search for the element is triggered.

Lines (70 – 74): A click operation is performed on the web element, which leads to the opening of the PHP Travels Blog Page.

Lines (80 – 93): Test is marked as ‘Fail’ if the window title of the blog page does not contain the sub-string ‘PHPTRAVELS Blog.’

Execution

Here is the execution snapshot that demonstrated the usage of Implicit Wait for synchronization in Selenium:

image9
image11

Difference between Implicit and Explicit Wait in Selenium

Though implicit and explicit wait in Selenium are used for resolving issues related to synchronization in Selenium, there is a vast difference between the two options. Here are some of the salient features of Explicit Wait when it comes to Selenium web automation:

  • Explicit wait is not only documented but also has a guaranteed and defined behavior. Behavior with Implicit Wait is not guaranteed and can vary in certain test scenarios.
  • Explicit wait executes on the local part of Selenium. On the other hand, implicit wait works on the part that controls the web browser (i.e., remote part of Selenium).
  • Explicit wait lets you use WebDriverWait in combination with WebDriverExpectedCondition (i.e., wait on the occurrence of a certain condition). On the other hand, Implicit wait tells the Selenium WebDriver to poll the DOM for a certain time when trying to find web element(s) that are not immediately available.
  • Explicit wait can be effectively used for checking the absence of web element(s) on the page.
  • Customizing timeouts in Implicit Wait is done through global timeouts, whereas timeouts and polling frequency (for condition) in Explicit Wait can be customized for every explicit wait command used in the code.

It’s a Wrap

image12

Source

Synchronization in Selenium has significant value as it is instrumental in overcoming exceptions that can arise due to timing-related issues. In this Selenium PHP tutorial: How To Handle Synchronization In Selenium PHP Using Implicit and Explicit Wait, we had an extensive look at the mechanisms of implicit and explicit wait in Selenium and their usage in test automation.

Sleep is a form of unconditional synchronization, which should be avoided since it increases the chance of unnecessary wait. Implicit and Explicit wait in Selenium are forms of conditional synchronization. Explicit wait lets you wait on the occurrence of a certain condition which is not possible with implicit wait in Selenium. When looking for options to achieve synchronization in Selenium PHP, the recommendation is to use explicit wait over implicit wait & sleep.

Frequently Asked Questions

What is meant by the implicit wait in Selenium?

Implicit wait tells the Selenium WebDriver to poll the DOM for a certain element(s) if the element(s) is/are not immediately available. If the web element is not found in the DOM, a “No such element” exception is thrown.

Implicit wait tells the Selenium WebDriver to poll the DOM for a certain element(s) if the element(s) is/are not immediately available. If the web element is not found in the DOM, a “No such element” exception is thrown.

What is the difference between implicit wait and thread sleep?

Implicit wait allows you to halt the WebDriver for a specific period of time until the WebDriver is able to locate the desired element on a web page. On the other hand, thread.sleep() is a static Java method that suspends the code for a specific amount of time. It pauses the execution and helps us to know what has happened during the pause. It accepts the time specified in milliseconds.

What are the different types of wait in Selenium?

Implicit and Explicit Waits are the two major types of waits supported in Selenium (there are others as well, like Fluent Wait). Irrespective of the type of wait being used, the major intent of using the wait is to have the Selenium web automation task execution elapse a certain duration before the script execution proceeds to the next step.

Author Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 132



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free