How to handle Alerts in Selenium WebDriver

 
 
In this tutorial, we will discuss various types of Alerts available in web application testing and how to handle alerts in Selenium WebDriver.  We will cover the 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 providing some message to the user, needing some input from the user, or can be only for warning purposes. We will discuss the 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 the 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 has an OK button on them. It is used to display some information to the user. 
The important point to note is that we can switch from the main window to an alert using the below point

driver.switchTo().alert()

Below is a perfect example that 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()

Below is an example that shows how to handle Confirmation 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 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 the prompt alert, we get an option to add text to the alert box. This is used 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 illustrates 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

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Advertisement

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 )

Facebook photo

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

Connecting to %s