Handle Alerts, Prompt and Confirm Box

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

A MINI PROJECT

ON
Handle Alert, Prompt and Confirm Box

Submitted by
Sana
Case Study: Handle Alert, Prompt and Confirm Box
Problem Statement:
1) Identify and Handle Popups
2) Entering user data in an alert, Accept or Cancel the
alerts
Handle Alert, Prompt and confirm Box

Table of Contents

1.Project Objective
2.Assumptions
3.Handling Alerts and Popups -Step by step approach
3.1 Selenium IDE Set up.
3.2 Adding Selenium jar files.
3.3 Configuring the driver path.
3.4 Handling the multiple Browsers.
3.5 Handling the Alerts, Popups and Confirm boxes.
3.6 Executing the appropriate hands-on code.
3.7 Obtaining the corresponding results.
4. Conclusion

3
Handle Alert, Prompt and confirm Box

Project Objective:

1) Identify and Handle Popups

2) Entering user data in an alert, Accept or Cancel the alerts

Suggested site: http://demo.automationtesting.in/Alerts.html

Detailed Description:

 Launch the browser


 Go to URL, http://demo.automationtesting.in/Alerts.html
 Hover the mouse over “SwitchTo” menu
 Click on Alerts
 Click on the link “Alert with OK”" and click on “click the button to display an alert box:”.
 Check whether alert pop up appears or not.
 Click on “OK”
 Click the Link "Alert with OK & Cancel" in the left menu option.
 Click the link "click the button to display a confirm box.
 Check whether confirm box pop up appears or not.
 Click on “Cancel” and check the respective message displayed
 Click the Link "Alert with Textbox".
 Click the link "click the button to demonstrate the prompt box.
 Check whether prompt box pop up appears or not.
 Enter your name in the textbox and click on “OK”
 Check if the message Hello <your name> How are you today” is displayed
 Close the browser

Key Automation Scope:

 Implementation of Alert Class


 Navigation to various web elements
 Handle Multiple browsers
 Exception handling  

4
Handle Alert, Prompt and confirm Box

What are Alerts/popups in Selenium?


Alerts are small popup boxes/windows which display
the messages/notifications and notify the user with some information seeking
some permission on certain kinds of operations. Additionally, we can also use
them for warning purposes. Sometimes, the user can enter a few details in the
alert box as well.

Application alerts shift your focus from the current browser window to a newly


opened window and force the user to read the message displayed and act
accordingly. Users will be blocked and will not be able to work further unless the
alert message box gets handled.

What are the different types of Alerts/popups?


While automating any web application, Selenium WebDriver may encounter
alerts that can either be application dependant or the Operating system
dependant 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.  A few of those utilities are  AutoIT  and  Robot Class  in Java. A
sample operating system based alert will look as follows and are majorly
called Dialog-Boxes:
 Web/Javascript /Browser-based Alerts: Web/Browser based alerts are primarily
called  Javascript alerts  and are those alerts that are browser dependant. These
alerts are majorly called Popups.

 What are the various kinds of alerts


provided by Web Applications?
As we discussed, there are various types of alerts that the developers can
implement on web applications. Each of these alerts/popups needs different
kinds of actions to handle these alerts. Let's see what these different types of
alerts that the web applications provide are:

5
Handle Alert, Prompt and confirm Box

 Simple alert: These alerts are just  informational  alerts and have
an  OK  button on them. Users can click on the  OK  button after reading the
message displayed on the alert box

 Prompt Alert: In Prompt alerts, some  input requirement  is there from the
user in the form of text needs to enter in the alert box. A prompt alert box is
displayed like below, where the user can enter his/her username and press
the  OK  button or  Cancel  the alert box without entering any details .

 Confirmation Alert: These alerts  get some confirmation  from the user in
the form of  accepting  or  dismissing  the message box. They are different
from prompt alerts in a way that the user cannot enter anything as there is no
text-box available. Users can only read the message and provide the inputs by
pressing the  OK/Cancel  button.

How to handle Alerts/popups using Selenium WebDriver?

As we know, whenever we are executing any of the automation scripts


using Selenium WebDriver, the WebDriver always has the focus on the main
browser window and will run all the commands on the main browser window
only. But, whenever an alert/popup appears, it opens up a new window. So,
for handling the Alerts using Selenium WebDriver, the focus need to be shifted to
the child windows opened by the Alerts. To switch the control from the parent
window to the Alert window, the Selenium WebDriver  provides the following
command:

driver.switchTo( ).alert( );
Once we switch the control from the main browser window to the alert window, 
we can use the methods provided by  Alert Interface to perform various
required actions. For example, accepting the alert, dismissing the alert, getting the
text from the alert window, writing some text on the alert window, and so on.

To handle Javascript alerts, Selenium WebDriver provides the


package org.openqa.selenium.Alert  and exposes the following methods:

6
Handle Alert, Prompt and confirm Box

 Void accept(): This method clicks on the 'OK' button of the alert box.

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

 Void dismiss(): We use this method when the 'Cancel' button clicks in the alert
box.

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

 String getText(): This method captures the message from the alert box.

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

 Void sendKeys(String stringToSend): This method sends data to the alert box.

driver.switchTo().alert().sendKeys("Text");

7
Handle Alert, Prompt and confirm Box

Implementing the Project:

Handling Multiple Browsers:


 Creating Alert Class

8
Handle Alert, Prompt and confirm Box

Creating testng.xml

9
Handle Alert, Prompt and confirm Box

10
Handle Alert, Prompt and confirm Box

11
Handle Alert, Prompt and confirm Box

The following are the step-by-step outcomes during testing.


 Launching Firefox browser

12
Handle Alert, Prompt and confirm Box

13
Handle Alert, Prompt and confirm Box

14
Handle Alert, Prompt and confirm Box

 Launching Chrome simultaneously

15
Handle Alert, Prompt and confirm Box

16
Handle Alert, Prompt and confirm Box

17
Handle Alert, Prompt and confirm Box

18
Handle Alert, Prompt and confirm Box

19
Handle Alert, Prompt and confirm Box

Console Output:

20
Handle Alert, Prompt and confirm Box

Conclusion:
In this mini project, Handling Alerts, Prompts and Confirm Boxes, I have
implemented the project using Selenium IDE for automation testing.
Through this project I have implemented the Alert Class and handled
multiple browsers through which the web elements are navigated.
Exceptions are also handled during run time of the execution.

21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy