How to handle Alerts in Selenium WebDriver

 
 
 
In this tutorial, we will discuss about various types of Alerts available in web application testing and how to handle alert in Selenium WebDriver.  We will cover below mentioned topics:-
1) What is Alert? 
2) Type of Alerts
3) How to handle Alert using Selenium WebDriver?
 

What is Alert?
Alert is a pop up window on the screen. These alerts perform different purposes, like provide some message to user, need some input from the user, or can be only for warning purpose. We will discuss about below mentioned alerts:-
1) Simple Alert
2) Confirmation Alert
3) Prompt

There is an interface in Selenium called Alert. It is present in the org.openqa.selenium.Alert package. Alert interface gives us following methods to deal with the alert:

  1. accept() To accept the alert by clicking on OK button
  2. dismiss() To dismiss the alert by clicking on CANCEL button
  3. getText() To get the text of the alert
  4. sendKeys() To write some text to the alert box

Type of Alerts

1) Simple alert – Simple alert just have an OK button on them. It is use to display some information to the user. 
Important point to note is that we can switch from main window to an alert using the below point

driver.switchTo().alert()

Below is a perfect example which shows how to handle Simple Alert with Selenium

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 SimpleAlert_Example {
     public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe"); 
 
           WebDriver driver = new FirefoxDriver();              
 
           driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS)
           driver.navigate().to("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html");
           driver.findElement(By.xpath("//*[@class='btn btn-default']")).click();     
 
           // accepting javascript alert 
           Alert simpleAlert = driver.switchTo().alert();
           String alertText = simpleAlert.getText();
 
           //Print the message mentioned on the AlertBox
           System.out.println("Alert text is :" + alertText);   
 
           //This step is only for demonstration purpose to show the alert box
           Thread.sleep(2000);
           simpleAlert.accept();     
 
         //Close the current page           
          driver.close();
    }
}

Output 
Alert text is :I am an alert box!

2) Confirmation Alert – This alert comes with an option to accept or dismiss the alert. To accept the alert we can use Alert.accept() and to dismiss we can use the Alert.dismiss()

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 ConfirmationAlert {

        public static void main(String[] args) throws InterruptedException {          
 System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");

               WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.navigate().to("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html");
              driver.findElement(By.xpath("//*[@class='btn btn-default btn-lg']")).click();
             Thread.sleep(2000); 

              // accepting javascript alert          
              Alert ConfirmationAlert = driver.switchTo().alert();
              String alertText = ConfirmationAlert.getText();
              System.out.println("Alert text is :" + alertText);
              ConfirmationAlert.accept();

             //Close the current page
             driver.close();
        } 
}

Output
Alert text is :Press a button!

3) Prompt Alert – In prompt alert, we get an option to add text to the alert box. This is use when some input is required from the user. We will use the sendKeys() method to type something in the Prompt alert box. 

Below is an example that illustrate how to handle Prompt Alert using Selenium WebDriver.

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 Prompt_Example {
 
      public static void main(String[] args) throws InterruptedException {                          System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");
 
                 WebDriver driver = new FirefoxDriver();
                   
                 driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
                 driver.navigate().to("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html");             driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[3]/div[2]/button")).click();
 
              Thread.sleep(2000);          
 
              // accepting javascript alert          
              Alert PromptAlert = driver.switchTo().alert();
              String alertText = PromptAlert.getText();
              System.out.println("Alert text is :" + alertText);               
 
              //Enter message in Alert Box
              PromptAlert .sendKeys("Accepting the alert");
 
              //This sleep is not necessary, just for demonstration
              Thread.sleep(4000); 
              PromptAlert.accept();            
 
              //Close the current page
              driver.close();
          } 
}

Output
Alert text is :Please enter your name

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s