Handle Alert and PopUp Using selenium:

 

What is Alert?

  • Alert is a small message box that displays on-screen notifications to give the user some kind of information or asks for permission to perform some type of operation.
  • It can also be used for warning purposes.

 

Type of Operation’s perform on Alert:

  • accept() : To accept the alert.
  • dismiss() : To dismiss the alert.
  • getText() : To get the text of the alert.
  • sendKeys() : To write some text to the alert.

 

Switch to Alert to perform some operation on Alert:

 

There are 3 types of Alert box:

  1. Simple Alert :- It give the some information about the current screen or give some warning.
  2. Prompt Alert :- It Ask from the User to provide some inputs.
  3. Confirmation Alert :- This Alert box ask for a permission to perform some type of operation.

 

Package:

 

Alert Accept:

void accept() :- It allow programmer to click on the ‘OK’ Button of the Alert box.

 

Alert Dismiss:

void dismiss() :- It allow programmer to click on the ‘Cancel’ button of the Alert box.

 

Get Alert Text:

String getText() :- It simply allow programmer to capture the alert box.

 

Send Text To Alert:

void sendKeys(String stringToSend) :- It allow programmer to send data into alert box.

 

Note: To Handle Window go to this Link.

 

Scenario’s:

  • setProperty To Launch Browser.
  • Open URL.
  • Click on WebElement to Open Alert / PopUP.
  • After click on WebElment create object of Alert to switch into Alert / PopUP.
  • Perform Accept operation on WebElement.
  • Perform Dismiss operation on WebElement.
  • Perform GetText operation on WebElement.
  • Perform sendKeys operation on WebElement.

 

Example of Handle Alert and PopUp Using selenium: