How to handle Alerts and Pop-ups in Selenium?

Assume you're filling out an application form and, by accident, you leave out some information. How will you know till you receive an alert or a pop-up window informing you of this? Let's have a look at the following article to learn more about selenium alerts and how to handle and manage them. In selenium, an alert is any message or notice box that informs the user about something or requests permission to do something. It's also used to provide warnings.

  

In Selenium, there are three kinds of alerts:

 1) Simple Alert

Selenium's simple alert class displays information or displays a warning message on the screen.


 

 

 2) Prompt Alert

This Prompt Alert prompts the user for input on the information needed to accomplish the task. With the help of sendkeys(" input.... "), Selenium web drivers can enter text. 

 

 

 

3) Confirmation Alert

A confirmation alert requests permission to undertake a specific operation or job. This alert is used to confirm the completion of certain tasks or operations.

For instance, do you wish to continue with a specific task? Yes or No?


 
 
 

Alert Handling in Selenium

Handling alerts is a time-consuming process. Selenium provides a variety of methods for handling and managing alerts, reducing human involvement and simplifying this work.

Methods of handling alerts in Selenium

 

 

Void dismiss()- This method is applied when the ‘Cancel’ button is clicked in the alert box.
Syntax- driver.switchTo().alert().dismiss();

 

Void accept()- This method is used to click on the ‘OK’ button of the alert.
Syntax- driver.switchTo().alert().accept();

 

String getText(): This process is used to capture the alert messages
Syntax- driver.switchTo().alert().getText();

 

Void sendKeys(String stringToSend): This process is used to send some data to the alert box.
Syntax- driver.switchTo().alert().sendKeys("Text");

 Popups in Selenium

A popup is a window that appears on the screen as a result of some action. In computerization, if a web application has numerous windows, any action may require switching control from one window to another to complete the process or task. It must return to the main window, i.e., the parent window in Selenium, once the procedure is completed.

 

How to handle popups in Selenium

There are several methods for handling the popups in the selenium web driver:

 

1.  Driver.getWindowHandles();

To handle all the opened windows by Selenium web driver, you can use

Driver.getWindowHandles() to switch from one window to another in a web application.

 

2.  Driver.getWindowHandle();

When the webpage is loaded, or the site opens, you can handle the main window by using the driver.getWindowHandle(). This will handle the current window that is uniquely recognized within the driver instance.

 

Handling Popup Window using Selenium

The robot class in the Selenium web driver handles and manages all keyboard and mouse functions. It can also be used to close a pop-up window. The WindowHandle() function can be used to get the window handle from a pop-up window.

 

Handling Unexpected Alerts using Selenium WebDriver

Unexpected alerts appear while accessing a web application owing to an issue or other factors. This alert does not appear every time you visit the site; instead, it appears at random intervals. If you've created an automated test case for some sites but haven't addressed this type of alert, your script will immediately fail if such an unexpected alert pop-up appears.

 

You should also refer: What is SDET


Comments

Popular posts from this blog

How to Install JUnit in Eclipse

Docker Basic Commands Cheat Sheet

DevOps Engineer Skills You Must Have