top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Handling Alerts and drop downs in Selenium

Introduction


Alert is a small message box that appears on the screen to give some kind of information or warning for potentially damaging operation or asks permission for performing that operation.


Scenario for Alert in real time:


A lot of the times, when we are filling in a form and press the submit button, an alert pops up, telling me, "Email ID Required". At times, I forget to check the terms and conditions box, and I get the same as a notification via a popup-up alert. Pressing the OK button on the alert box takes me back to the form, and the form does not submit until I have filled everything "required". These use cases show how necessary are the "Alerts" or "Popups" in Web Applications. In this article, we will understand these features in more detail and will understand how we can automate various types of PopUps and Alerts in Selenium


Is Alert an interface in selenium?


Yes,Alerts are basically an interface between the current web page and user interface.


What are the different types of Alerts/popups?


While automating any web application, Selenium WebDriver may encounter alerts that can either be application dependent or the Operating system dependent on which the user is working. Based on these categorizations, we can divide the alerts majorly into the following categories:

  • Windows/OS Alerts: Window-based alerts are system-generated alerts/popups. The developers invoke the operating system APIs to show these alerts/dialogue-boxes. Handling these alerts in Selenium is a little tricky and beyond the WebDriver's capabilities, as Selenium is an automation testing tool for web applications only, and we need third party utility to automate window based popups.

  • Web/Javascript /Browser-based Alerts: Web/Browser based alerts are primarily called Javascript alerts and are those alerts that are browser dependent. These alerts are majorly called Popups

Types of Alerts provided by WebApplication :


1.Simple Alert

The Simple alert class in selenium displays some information or warning on the screen.

Attached Snapshot for Simple alert represents a warning message was thrown to the user because of

naming convention mismatch.



2.Confirmation Alert:

The Confirmation alert asks permission to do some type of operation.

Attached Snapshot for confirmation alert represents a computer shutdown warning message with two options for the user either to shutdown the computer or cancel the shutdown process.

3.Prompt Alert:


The Prompt Alert asks for user’s input which could be entered using send keys method by selenium web driver.

Attached Snapshot represents the registration process where password mismatch error is thrown which can be fixed by providing correct password.


Methods to automate Alert handling in Selenium WebDriver


Reference M-Method


M1 :To click the cancel/dismiss button of the Alert.

Function: void dismiss()

Statement: driver.switchTo().alert().dismiss();

M2:To click the Ok button of the Alert.

Function: void accept()

Statement: driver.switchTo().alert().accept();

M3:To Capture the Alert message

Function:String getText()

Statement: driver.switchTo().alert().getText();

M4:To send data to alert box

Function:void send keys(String input string)

Statement:driver.switchTo().alert().send keys(“Text”);


Sample code for handling simple,Confirmation and Prompt Alert.

Simple Alert :


After hitting the mentioned URL in the driver.get (“…”) statement control is transferred to the alert interface by driver.switchTo().alert() command and the accept function is enabled for transfering it’s execution to the browser.


Confirmation Alert :


After hitting the mentioned URL in the driver.get(“…”) statement control is transferred to the alert interface by driver.switchTo().alert() command and the dismiss function is enabled for transfering it’s execution to the browser.



Prompt Alert


After hitting the mentioned URL in the driver.get(“…”) statement control is transferred to the alert interface by driver.switchTo().alert() command and the text is entered using send keys method to enable the accept function for transfering it’s execution to the browser.



Conclusion :

This blog provides an overview about the various types of alerts and also provides information about the various methods available for handling those alerts along with the sample code for better understanding.


Handling drop downs in Selenium


In HTML, the drop downs are generally implemented either using the <select> tag or the <input> tag. To perform certain operations on the drop downs, which are declared using the <select> HTML tag,Selenium WebDrivers provides a class called "Select " class which is provided by the "org.openqa.selenium.support.ui " package of Selenium WebDriver.


Methods available in the select class


Before accessing the specific method for select class object for the select class needs to be created and the object/web element for dropdown is identified by Xpath preferably instead of id, class name or name.From the code snippet it is clear se is the object name for handling drop downs


/ Create object of the Select classSelect se = new Select(driver.findElement(By.xpath("//*[@id='oldSelectMenu']")));

​Method

Syntax with Example

Description

​selectByIndex

selectByIndex(int arg0) se.selectByIndex(2)

This method will select an option from the dropdown at index point 2 and its return type is integer

selectByValue


​selectByValue(String arg0) se.selectByValue("7");

This method will select an option from the dropdown based on the attribute value whose return type is string.

selectByVisibleText

​selectByVisibleText(String arg0) se.selectByVisibleText("Blue");

This method will select options having the text as "Blue",

​​deselectByIndex

​dselectByIndex(int arg0) se.deselectByIndex(2)

This method will deselect an option from the dropdown at index point 2 and its return type is integer

​deselectByValue


​deselectByValue(String arg0) se.deselectByValue("7");

This method will deselect an option from the dropdown based on the attribute value whose return type is string.

deselectByVisibleText

​deselectByVisibleText(String arg0) se.deselectByVisibleText("Blue");

​This method will deselect options having the text as "Blue",

​​deselectAll

​deselectAll()

This method will deselect all the selected options from the drop down.

Below Document Object Model snapshot indicates the select class with various attributes mentioned in the Tag through which selections are made in the dropdown.

How to check whether dropdown is Multi-Select?

The Select class provides the "isMultiple() " method, which determines whether the web element in say supports multiple selections. It returns a boolean value, i.e., True/False, without taking any argument. It checks the attribute 'multiple' in the HTML code for the web element. Consequently, it possesses the following syntax,


isMultiple(): boolean

Once you determine whether the web element is multi-select or not, you can use the Select class's various select methods on the multiple values you intend to select which is described in the code snippet attached below,


Select mSel = new Select(driver.findElement(By.xpath(//*[@id='color']);if(mSel.isMultiple()){ //Selecting multiple values by index mSel.selectByIndex(1); mSel.selectByIndex(2); //Or selecting by values mSel.selectByValue("Red"); mSel.selectByValue("Blue"); //Or selecting by visible text mSel.selectByVisibleText("Blue "); mSel.selectByVisibleText("Orange"); }


How to get options from a dropdown in Selenium?


Select class provides the following methods to get the options of a dropdown,

  1. getOptions()

  2. getFirstSelectedOption()

  3. getSelectedOptions()


getOptions()

There are times when you need to get all the options in a dropdown or multi-select box. This is where you can use the getOptions() method of the Select class. It possesses the following syntax:

Syntax : getOptions(): List<WebElement>

Example:

Select select = new Select(driver.findElement(By.id("oldSelectMenu"))); // Get all the options of the dropdown List<WebElement> options = select.getOptions();


getFirstSelectedOption()

This method returns the first selected option of the dropdown. If it is a single-select dropdown, this method will return the selected value of the dropdown, and if it is a multi-select dropdown, this method will return the first selected value of the dropdown. It possesses the following syntax,

Syntax :getFirstSelectedOption(): WebElement


Example:

Select select = new Select(driver.findElement(By.id("oldSelectMenu"))); // Get the first selected option of the dropdownWebElement firstSelectedOption = select.getFirstSelectedOption();


getAllSelectedOptions()

This method returns all the selected options of the dropdown. If it is a single-select dropdown, this method will return the only selected value of the dropdown, and if it is a multi-select dropdown, this method will return all the selected values of the dropdown. It possesses the following syntax,


Syntax :getAllSelectedOptions():List<WebElement>

Example:

Select select = new Select(driver.findElement(By.id("oldSelectMenu"))); // Get all the selected option of the dropdown List<WebElement> selectedOptions = select.getAllSelectedOptions();


Conclusion :

The Select class in selenium can be used by importing the org.openqa.selenium.support.ui.Select package and

the Select Class provides different methods to select values from dropdown/multi-select.Select Class also provides deselection methods to deselect certain values from a dropdown.Multiple attributes can differentiate the multi-select elements provided bt the <select> tag.select and deselect methods can use an index, visible text, or values to select/deselect the values from a dropdown.





131 views0 comments

Recent Posts

See All

Exception Handling in Selenium Webdriver

What is an exception? An exception is an error that occurs during the execution of a program. However, while running a program, programming languages generate an exception that must be handled to prev

Opmerkingen

Beoordeeld met 0 uit 5 sterren.
Nog geen beoordelingen

Voeg een beoordeling toe
bottom of page