i

Selenium Step By Step Guide

Handling Alerts

- Identification of alerts?

Whenever we submit a form or we click on a button and system wants to interact with the user in order to give a notification to confirm the form submission or to expect a user input to complete the process such as “Cancel” and “OK” buttons in alert popups.

Therefore, we can identify the alert popup and can categorize them into different categories.

  1. Alert for notification.
  2. Alert for confirmations.
  3. Alert for user inputs.

Selenium has “Alert” interface to handle such cases where user can use a variety of alert handling methods to encounter the alert blockages. Alert interface package can be imported from “org.openqa.selenium.Alert”. Below are the methods which are part of “Alert” interface:

a-accept ()

b-dismiss ()

c-sendKeys()

d-getText()

In order to use the alert handling methods, first user must switch to the alert and then can perform the further actions by using alert handling methods. “driver.switchTo().alert()” command can be used to switch over to the alert and then other methods can be used to achieve the goals of handing alerts.

Let’s discuss more about each of them and see how to handle them using selenium.

a-Alert for Notification:

In these types of alert user gets a notification alert where user just acknowledges it by clicking on the “OK” button to tell the system that user has seen the notification. Let’s see the code overview:

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

//User just needs to accept the alert to keep the alert blockage away from the normal flow

Alert.accept();

b-Alert for Confirmations:

In these types of alerts user gets a popup message seeking user’s confirmation to perform the further actions. Like we see “OK” and “Cancel” or “Yes” and “No” buttons usually in such popups. This kind of situation is very important to handle in order to continue the flow.

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

alert.accept();// In order to select “Ok” or “Yes” option.

alert.dismiss();// In order to select “Cancel” or “No” option.

c-Alert for seeking user inputs:

In some cases, a working flow needs some data from user to complete the workflow of the application. In these types of alerts user enters some data to continue the flow of the application.

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

alert.sendKeys(“Happy Testing”).accept();

3-Getting text out of the alert?

This is another task which is most often happen where tester needs to fetch the text out of a alert popup and this can be achieved by using the getText() method as shown below:

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

alert.getText().accept();

4-Keyboard events in selenium:

Selenium has “Action” interface and “Actions” class to help us to simulate the keyboard actions in a very smooth and reliable way. There are various methods available to handle the automation needs and problems where keyboard actions are required. Let’s discuss them in detail below:

Actions class expects a parameter in its constructor as driver object to create a builder object. This builder object simulates all the actions and can be performed in a sequence. Once we create an instance of Actions class and see the methods available with “key” word, we can see the available methods.

 

In this way we have some other keyboard events which can be performed using the actions provided by Actions class.

Below are a set of methods available for the keyboard events available:

1-sendKeys(element, charSequence): This is utilized to send a set of keyboard actions on an element. We can send almost any key combination as parameter using this method.

2-keyDownAction(charSequence): This is used for pressing a keyboard button.

3-keyDownAction(element,charSequence): This is used for pressing a keyboard button with two parameters.

4-keyUpAction(charSequence): This is used for releasing the button up.

5-keyUpAction(element,charSequence): This is used for releasing the button up with two parameters.

A simple portion of code to use keyDown method to press the control button.

Actions builder=new Actions(driver);

Action performEvents=builder.keyDown(element1, Keys.CONTROL).build();