Handling Javascript Alerts/Popups In Selenium Webdriver

Hello Folks,

In last post, we have seen handing of website popups in selenium webdriver.

In this post, we will see how to handle JavaScript popups in selenium webdriver.

Types of JavaScript popups:

There are three types of popups in JavaScript which are given below:

  1. Alert popup
  2. Confirm popup
  3. Prompt popup

Alert popup:

  • It is a simple popup which shows some information to user in a small box within browser area.
  • It will have one “OK” button and close button.
  • Since, it is just a information box so OK button and close button will have similar functionality.
  • Popup will be disappeared when user clicks on either OK button or close button.
  • You can not inspect alert popup.
  • Without handling alert popup, you can not proceed further.

JavaScript code to create a alert popup:

Since you can not inspect alert popup, you can not handle it with selenium webdriver directly. So we need to use Alert interface.

Alert Interface:

Alert is an interface in selenium library. It has below methods in it:

  1. accept() : To click on OK button on popup.
  2. dismiss(): To click on close/cancel button of popup.
  3. getText(): To get text present on popup.
  4. sendKeys(java.lang.String keysToSend): To type on prompt popup.
  5. authenticateUsing(Credentials credentials) (Beta method)
  6. setCredentials(Credentials credentials) (Beta method)

A private class EventFiringAlert implements Alert interface.

Handling of alert popup in selenium webdriver:

To handle alert popup, first we need to switch to alert using switchTo() method. Syntax is as below:

Alert alert= driver.switchTo().alert();

  • alert() switches to the currently active popup for particular driver instance and return an Alert reference. It will throw NoAlertPresentException if no alert is present.

Java code:

Output:
Alert text is:I am alert popup and you can not inspect me. You need to handle me using Alert interface.
Alert closed successfully afetr accept.
Alert closed successfully after dismiss.

Confirm popup:

  • This popup is used to ask confirmation from user to perform some action within browser.
  • This popup will have two buttons “OK” and “Cancel”. Both button will lead to different actions.
  • Like alert popup, you can not inspect it also.
  • You need to use Alert interface to handle it in selenium webdriver.

Javascript code to create a confirm popup:

Java code:

Output:
Alert text is:I am a confirm popup. You can inspect me. You need to use alert interface to handle me.
Alert closed successfully afetr accept.
You pressed OK!
Alert closed successfully after dismiss.
You pressed Cancel!

Prompt popup:

  • This popup is used to get input from user as a text.
  • This popup will have a text box with OK and Cancel button.
  • Like alert popup, you can not inspect it also.
  • You need to use Alert interface to handle it in selenium webdriver.
  • Alert interface provides a method to type text in text box.
  • Prompt popup will have single text box only.

Javascript code to create a prompt popup:

Java code:

Output:
Alert text is:Please enter your name:
Alert closed successfully afetr accept.
Hello Amod Mahajan! How are you today?
Alert closed successfully after dismiss.
User cancelled the prompt.

 

That’s it guys. If you have any doubt, feel free to comment below.

If you like my posts, please like, comment, share and subscribe.

#ThanksForReading

#HappySelenium

Leave a Reply

Your email address will not be published. Required fields are marked *