×

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup

 An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser.

The Alert popup will be implemented with the help of JavaScript, so we cannot inspect any alert popup.

An Alert popup maximum contains two buttons inside it and with an error message.

To handle Alert popup, we need to take the help of an alert interface.

As you can see the below snapshot of alert popup,

snapshot of alert popup

Selenium web driver does not have the feature of switching to alert directly, to take control of alert popup, we will use the statement which is given below.

driver.switchTo().alert()

driver.switchTo().alert()

Alert is an interface which contains following methods-

  • accept()
  • dismiss()
  • getText()
  • sendKeys()
Alert
Alert Method Description Syntax
accept() This method is used to click on OK button. driver.switchTo().alert().accepts();
dismiss() This method is used to click on the CANCEL button. driver.switchTo().alert().dismiss();
getText() It is used to get the error message present in the popup, and the return type of getText() method is String. driver.switchTo().alert().getText();        
sendKeys() This method is used to send the data into the alert box.   driver.switchTo().alert().sendKeys("Text");  

Let us take one example to give you a better understanding of Alert Popup, and we created a sample test script which is as follows;

For our testing purpose, we are using the demo.automationtesting.in website to perform all the actions for alert popup handling in selenium web driver.

In this test case, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1. Open the Firefox Browser. System.setProperty()   The Firefox browser must be opened.
2. Navigate to the website. get() http://demo.automationtesting.in/Windows.html The home page window must be displayed.
3. Handling the alert popup with the OK and CANCEL button.     Ok and cancel button of the alert popup should be handled.
4. Create the Alert object     Alert object should be created.
5. Validating and printing the alert message. gettext()   The alert message should be printed and validated.
6. Click on the OK button to accept the alert popup. accept()   The Ok button should be clicked.
7. Move towards the main button to press the CANCEL button.     The CANCEL button should be pressed.
8. Click on the CANCEL button to dismiss the alert popup. dismiss()   The CANCEL button should be clicked.
9. Closing the browser window. close()   The browser window should be closed.

Open the Eclipse IDE and the existing test suite new_test, which we have created in WebDriver Installation section of the WebDriver tutorial.

Then, right-click on the src folder and create a new Class File from New ? Class.

created in WebDriver Installation
  • Give the Class name as Alert_popup and click onthe Finish button.
click on the Finish button

Step1:

To launch the Firefox browser, we need to download the geckoDriver.exe file and set the system property to the path of geckoDriver.exe file.

Here is the code for setting the system property of the Firefox Browser:

// System Property for gecko Driver   System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-v0.25.0-win64\geckodriver.exe");
// create an object for FirefoxDriver class.      
WebDriver driver=new FirefoxDriver(); 

Step2:

Now, we will get to the desired URL:

//navigate to the website.
driver.get("http://demo.automationtesting.in/Alert.html"); 

Step3:

After that, we will move to our next step, where we are handling the alert popup with the OK and CANCEL button, which is presented on the alert window of the website.

To identify the Alert popup first, right-click on the button called to Alert with OK & Cancel and select the Inspect element field.

alert window of the website.

The developer tool window will be launched, with all the specific codes used in the development of the particular button.

specific codes used in the development

Here the sample code,

//handling alert with OK and CANCEL button
 driver.findElement(By.xpath("//a[contains(text(),'Alert with OK & Cancel')]")).click();
 driver.findElement(By.id("CancelTab")).click();
 Thread.sleep(3000); 

Step4:

Now, we are creating the alert object with the help of the switchTo() method.

Here the sample code,

//creating the alert object
Alert act=driver.switchTo().alert();

Step5:

After that, we will be printing and validating the alert message using the getText() method and the switch.To() method.

Here the sample code,

//print the alert message
 System.out.println(act.getText());
 //validating the alert message
 String msg=act.getText();
 if(msg.equals("Press a Button !"))
 {
 System.out.println("actual alert message");
 } 
 else{
 System.out.println("in-correct alert message");
 }
 act= driver.switchTo().alert(); 

Step6:

In our next step, we are clicking on the OK button to accept the alert popup.

clicking on the OK button to accept the alert popup.

Here the sample code,

//click on OK button
 act.accept(); 
 msg=driver.findElement(By.xpath("//p[@id='demo']")).getText();
 System.out.println("click on the OK button:" + msg);
 Thread.sleep(5000);  

Step7:

Now again, we are moving towards the main button, i.e. display a confirm box and press the CANCEL button.

To identify the display a confirm box first, right-click on the button and select the Inspect element field.

identify the display a confirm box

The developer tool window will be launched, with all the specific codes used in the development of the particular button.

all the specific codes used in the development

Here the sample code,

//again move to main button to press cancel button
 driver.findElement(By.cssSelector("#CancelTab")).click();
 act= driver.switchTo().alert();
 Thread.sleep(5000);  

Step8:

Click on the CANCEL button to dismiss the alert popup.

Click on the CANCEL button

Here the sample code,

//click on CANCEL Button
 act.dismiss();  
 msg=driver.findElement(By.cssSelector("#demo")).getText();
 System.out.println("click on the CANCEL button:" + msg);
 Thread.sleep(3000); 

Step9:                                                                                 

Finally, we close the browser window.

 //close the browser
  driver.close();

After completing all the given steps, our final text script will look like this:

package testpackage;
 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.Alert;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver; 
 public class Alert_popup_handling {
 public static void main(String[] args) throws InterruptedException {
 // System Property for gecko Driver   
 System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-v0.25.0-win64\geckodriver.exe");
 // create an object for FirefoxDriver class.       
 WebDriver driver=new FirefoxDriver();
 //maximize the browser 
 driver.manage().window().maximize(); 
 //navigate to the url
 driver.get("http://demo.automationtesting.in/Alerts.html");
 driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
 //handling alert with OK and CANCEL button
 driver.findElement(By.xpath("//a[contains(text(),'Alert with OK & Cancel')]")).click();
 driver.findElement(By.id("CancelTab")).click(); 
 Thread.sleep(3000);
 //creating the alert object
 Alert act=driver.switchTo().alert();
 //print the alert message
 System.out.println(act.getText());
 //validating the alert message
 String msg=act.getText(); 
 if(msg.equals("Press a Button !"))
 {
 System.out.println("actual alert message");
 }
 else{
 System.out.println("in-correct alert message");
 }
 act= driver.switchTo().alert(); 
 //click on OK button
 act.accept(); 
 msg=driver.findElement(By.xpath("//p[@id='demo']")).getText();
 System.out.println("click on the OK button:" + msg);
 Thread.sleep(5000); 
 //again move to main button to press cancel button
 driver.findElement(By.cssSelector("#CancelTab")).click();
 act= driver.switchTo().alert();
 Thread.sleep(5000);  
 //click on CANCEL Button
 act.dismiss();  
 msg=driver.findElement(By.cssSelector("#demo")).getText();
 System.out.println("click on the CANCEL button:" + msg);
 Thread.sleep(3000);
 //closing the broswer window                     
 driver.close();
 }
 }
  • To run the test script in Eclipse, right-click on the window and then click Run as ? Java application and the test script will be launched in the chrome browser, and automate all the test scenarios.

Related Topics

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

1 minute read.

Drawback or Limitations of Selenium

Drawback or Limitations of Selenium Followings are the Drawback or limitations of Selenium:- Drawback /LimitationsDescriptionDesktop applications cannot be automated by Selenium.To test the desktop application, we have tools like Test Complete, Winnium, and...

2 minutes read.

Characteristics of Selenium IDE

Characteristics of Selenium IDE Selenium IDE is separated into different parts, each having their features and functionalities. The seven various components of Selenium IDE are as follows: MENU BAR: The menu bar is...

3 minutes read.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

Selenium WebDriver-CSS Selector: Tag and Attribute

Selenium WebDriver-CSS Selector: Tag and Attribute We will locate the web element with the help of Tag and attribute CSS selector in this section of the tutorial. Where Tag and attribute are...

5 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium WebDriver Characteristics

Characteristics of Selenium WebDriver Some of the essential characteristics of Selenium WebDriver are as follows: Multiple Operating System Support Selenium Webdriver supports multiple operating systems like: Desktop OS: Window, Linux, and macOS Mobile OS:...

2 minutes read.

WebDriver-Class Name Locator

WebDriver-Class Name Locator In this tutorial, we will learn about the Class name locator of selenium WebDriver. The Class Name locator is used to identify any element in UI based on the...

4 minutes read.

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

4 minutes read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

4 minutes read.

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

4 minutes read.

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling In this tutorial, we will be learning about handling the checkbox in selenium WebDriver. Let us take one example to give you a better understanding of Checkbox handling,...

4 minutes read.

Selenium WebDriver - Google Chrome Browser

Selenium WebDriver - Google Chrome Browser In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser. To implement the WebDriver protocol with the help...

3 minutes read.

Selenium WebDriver - Navigation Controls Commands

Navigation Controls Commands in Selenium WebDriver WebDriver is used to hold the browser history and perform browser navigation operations like, back, forward, and refresh. Whenever a WebDriver launches a browser, all...

5 minutes read.

Selenium WebDriver - Firefox or Gecko (Marionette) browser

Selenium web driver-Firefox or Gecko (Marionette) browser In this tutorial, we are going to learn how to run the Selenium WebDriver test script in the Firefox Browser using the Gecko Driver. Before going...

4 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

Selenium WebDriver &ndash; Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.