Introduction:
In selenium web application handling Alerts is one of the important concept . Selenium handle web based
alert popups and it looks like a small popup box which is displayed on the web application and it blocks the user to not perform any actions without handling the alert box. Usually alert box comes with some alert messages that inform the user to perform the action in a correct manner and provide clear instructions in the form of messages. These alerts are not Html based its not tied up to the web so we cannot inspect the alert box.
selenium handle the java script alerts that display during the execution of automated test. Alert messages are a common feature in software applications is used in various platforms like E-commerce and Banking sites.
How to Handel JavaScript alert?
Handel java script alerts in selenium web application using alert interface and selenium
can easily switch to alert from the main window to switch To() method.
driver.switchTo().alert();
Different types of alerts in selenium are
Simple Alert
Simple java script or browser alert box always comes with a short message to the user that
contains the message and an OK button.
Here is the example of alert :
when user click the Alert button an alert popup appears with message that shown below.
user needs to perform click action on the OK button.
Use the Syntax:
driver. switchTo(). alert(). getText() ---to receive the text message present in the alert box
Code example:
The code explains after setting the driver and browser hitting the url and inspect the name field
and send a text name using send keys method .Also find the Css for the Alert button and perform
the click option .Once user clicks the button an Alert popup box will appear with message and ok button. Handel the alert box using switch To() method driver could interact with the alert and get the text using the get Text() method and accept the Ok button.
2 . Confirmation Alert
Confirmation Alert box gives you confirmation popup whether user want to proceed or not with two options OK and Cancel buttons.
When user clicks the confirm button a message with ok and cancel button Alert popup appears .
driver. switchTo().alert(). accept() ---to accept the alert box by clicking OK
driver. switchTo( ).alert(). dismiss() ---to dismiss the alert box by clicking Cancel
Code example:
3)Prompt Alert
Prompt Alert box takes some input from the user before entering the page
The user will click either OK or Cancel to proceed.
driver.switchTo() .alert().sendKeys("Selenium") --send data to the alert box
and perform the action by clicking OK it accepts the data and returned the
text by getText() method. When user clicks the cancel button it will returned
as null.
Handel Dropdowns in selenium:
Dropdowns is most commonly used web element and There are different kinds of dropdowns we could see in
most of the web applications usually dropdowns is like a list of options that user can select from it based on their
preference. Using selenium we can automate different nature of dropdowns.
How to Handel Static Dropdowns with select web driver in selenium?
In Static dropdown options are already fixed in that case we should inspect and check
the tag name is as "Select" then we can use a special class in selenium called Select
to choose the specific option from the dropdown.
By creating a select object we will select the options with three different methods:
Select dropdown=new Select();
select By Index();
select By Value();
select By Visible Text( );
Code example:
After create an object for select class we give a knowledge of the dropdown web element with the select
object by parsing the variable option to the select class object . Now the user could easily perform the actions using select methods.
dropdown . select By Index(3); --->it select the 3rd option present in the list
dropdown . select By Value("INR"); --->it select from the value in the Html properties that is INR
dropdown .select By Visible Text("USD" ); ----> it selects the text present in the list
2 . How to Handel Dynamic Dropdowns with selenium web driver ?
Dynamic dropdowns are dependent dropdown these dropdowns are loaded based
on user actions. These two dropdowns FROM and TO dependent each other so without
selecting the departure city the arrival city dropdown wont display their list of options.
The TO dropdown option will be dynamically populated based on the option selected
from the FROM dropdown.
Locate the FROM dropdown and perform click action to open the dropdown and inspect the element for the option what to select and perform click.
Locate the To drop down and identify the element and choose the state from the newly populated options.
code example:
3 . How to Handel Auto suggestive dropdown in selenium?
These type of Dropdown are also called dynamic dropdown here the country
menu provides options based on the user input.
It gives list of suggestions based on the character that is entered by the user
and it gives options without typing the entire text.
Enter the input by inspect the auto suggestive field.
It filtered and give some suggestions that is relate to the user input.
Get the list of suggestions and iterate each and every option present and
click the desired keyword.
The list holds number of suggestions so in order to get all the options use
find elements() web element method.
code example:
Conclusion:
This Alert and dropdown topic covers essential information on handling alert popups , different types of alerts and also provide code snippets for each type of alerts for better understanding and for the dropdown it explains some of the drop down types and how to it static and dynamically and understand the types and applying the specific methods. Readers can easily go through the topics and interact with alert dialogs and and dropdowns to improve their ability to work on it.