Selenium automated testing: workaround when encountering pop-ups

5 min read >

Selenium automated testing: workaround when encountering pop-ups

Engineering Insights & Mobile Solutions

During the runtime of our automated testing scripts, a common problem that blocks the completion of the tests arises from the appearance of pop-ups that are displayed for a small amount of time. This kind of situation can be easily missed when writing the test script, due to their short display time.

In the case above, the popup will appear for a short time when trying to search for a product in the database. While this popup is displayed, the rest of the page will not be clickable. This is problematic if you use the click() method for an element, immediately after searching for a product. The test script will fail with the below error:

org.openqa.selenium.WebDriverException: unknown error: Element < The element that should be clicked>…</a> is not clickable at point (m, n). Another element would receive the click: <”The pop-up that is displayed”>…</div>

As stated in the above error, the element that should be clicked is not available due to the pop-up being displayed.

In order to address this problem there are 2 methods:

The inefficient one

One workaround for this problem is to set an implicit wait. You specifically make the test script wait for a given amount of time. So if the pop-up lasts for 2-3 seconds, you can give it an appropriate wait time.

driver.manage().timeouts().implicitlyWait(amountOftime, time unit);

This is not recommended, as it is not efficient. On the machine on which the test scripts are developed, the script may work ok, but when deployed on another machine (i.e. a test server) in an environment where the internet bandwidth is smaller, this test script will most likely fail.
Also if you have multiple implicit waits, the total test script duration will be affected. For these reasons, this method should be avoided.

The scalable one

The best way is to wait the exact amount of time for such a popup to disappear. This can be done by checking if the popup is still displayed on the screen and when it’s not, clicking the element needed.

This can be easily done with a while loop:

while(popup.isdisplayed()){}

The loop will run until the condition is no longer respected, meaning as soon as the popup is no longer displayed, the loop will end and the script will move on to the next statements.

It’s a simple and efficient way that doesn’t load the script with the useless waiting time.

You can also define methods that can be used in these scenarios.

public static int longWait = 50;
public static WebElement clickable(WebDriver driver, By locator) {
             
return new WebDriverWait(driver, longWait).until(ExpectedConditions.elementToBeClickable(locator));
}

This method waits until an element is clickable.

Our QA team provides automated testing by using frameworks, such as Selenium, to create test scripts, while simulating the user interactions on various browsers, such as Chrome, IExplorer, and Firefox. Our team members are ISTQB certified and have tested 300 solutions. Just say hello@tremend.ro for a free consulting meeting.