Selenium(Java) Hack you MUST know as an Test Automation Engineer! P.1

Davit Danelia
2 min readMar 18, 2021

(How to Handle Pop-up windows)

Photo by Christopher Gower on Unsplash

Chances are that you use Selenium WebDriver as the go-to framework for your web automation needs. It is certainly one of the most popular test automation frameworks in existence. This article has taken the liberty of one trick that is usually is unknown to many but helps to get high quality code with trustable method!

How To Handle Pop-up Windows

Pop Up example

In Selenium web automation, a popular problem is dealing with pop-up windows. The windows typically exist in 3 formats:

  1. The prompt alert: informs the user that they need to input some data and interact with it.
  2. The simple alert: displays a message
  3. Confirmation alert: requests user to confirm some operation

As we know WebDriver cannot handle Windows-based allerts, but it can handle web-based alerts ( ‘alert()’ ). Use the driver.switchTo() method to manage pop-ups.

You can see how switchTo() method is used in this particular case. In this case, switch.to.alert is used to switch to the appeared alert pop-up box. Once this happens, you can use all switchTo().allert methods and also the alert is accepted with the alert.accept() method.

That’s it buddy! now you can handle any window Pop-Up by using switchTo() technique!

Peace ✌️!

--

--

Davit Danelia

SDET at EPAM systems / QA Lead at Sharemac / GeoSTQB Inovations boar member