Alert Interface in Selenium:

Alert Interface in Selenium

Table of Contents

Here the Alert Interface in Selenium is a small message box that appears on screen to give the user an information or may be notification. It always notifies the user with some specific information or may be error that asks for permission to perform some tasks an also it provides a warning message.

There are few alert in selenium types. They are:

  1. Simple Alert- The simple alert class in selenium will display some information or may be some warning on the screen.
Alert & Popup Handling in Selenium WebDriver
  1. Prompt Alert- This prompt alert will ask some input from the selenium webDriver can enter the text by using the sendkeys(“……input”).
Alert & Popup Handling in Selenium WebDriver
  1. confirmation alert- This confirmation alert asks permission to do the operation.
Alert & Popup Handling in Selenium WebDriver

How we may handle the Alert in selenium Webdriver?

Alert interface will provide the below few methods which will be widely used in Selenium webDriver.

  1. Void dismiss()//To select and click on ‘cancel’ button of the alert.

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

  1. Void accept()//To click “OK” button of the alert.

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

  1. String getText()//To capture the alert message

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

  1. Void sendkeys(String stringToSend) //used to send some data to alert box.

driver.switchTo().alert().sendKeys(“Text”);

We can easily switch to alert from the main window by using Selenium.switchTo()method.

Alert & Popup Handling in Selenium WebDriver

Consider an example, to show the Selenium Alert handling

Step 1- Launch the web browser and also open the site http://demo.iitworkforce.com/test/delete_customer.php

          step 2- Enter any customer id

Alert & Popup Handling in Selenium WebDriver

   Step 3- once it enters the customer id, click on the “submit” button.

Alert & Popup Handling in Selenium WebDriver

Step 4- Reject or may be accept the alert.

Alert & Popup Handling in Selenium WebDriver

Example for handling alert

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.NoAlertPresentException;

import org.openqa.selenium.Alert;

public class AlertDemo {

public static void main(String[] args) throws NoAlertPresentException,InterruptedException  { //It will throw  exception

System.setProperty(“webdriver.chrome.driver”,”G:\\chromedriver.exe”);

WebDriver driver = new ChromeDriver();

                //  By Alert Message handling

        driver.get(“http://demo.iitworkforce.com/test/delete_customer.php”);

      driver.findElement(By.name(“cusid”)).sendKeys(“53920”);

        driver.findElement(By.name(“submit”)).submit();

        //By  Switching to Alert        

        Alert alert = driver.switchTo().alert();

        // Capturing alert message.    

        String alertMessage= driver.switchTo().alert().getText();

        // Displaying alert message

        System.out.println(alertMessage);

        Thread.sleep(5000);

        // Accepting alert

        alert.accept();

    }

}

Output 

When we run this code it will launch a site and try to delete customer ID by handling confirmation alert which will display the screen and also thereby deleting the customer id from the application.

How to handle the unexpected and sudden alerts by using selenium webdriver?

As we can surf web application, unexpected alerts these will happen due to some error or some  other reasons. This kind of alert will not be display every time we open the site but the surface can be at random intervals. This can cause an automation test case for such pages and not handled this kind of alerts where our scripts will fall immediately if such unexpected alert popup is displayed.

When we direct code without any try catch block to accept or dismiss the alert and if the alert does not appear then our test case will fail to throw any exception in selenium like timeout Exception. Try catch block can handle  both the situations.

There are examples about how we handle the unexpected alert

import org.openqa.selenium.*;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedCondition;

import org.openqa.selenium.support.ui.WebDriverWait;

public class alerts {

    public static void main(String[] args) {

        System.setProperty(“webdriver.chrome.driver”,”./src/resources/chromedriver”);

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.get(“https://demoqa.com/alerts”);

        try {

            driver.findElement(By.id(“timerAlertButton”)).click();

            WebDriverWait wait = new WebDriverWait(driver,10);

            wait.until(ExpectedConditions.alertIsPresent());

            Alert simpleAlert = driver.switchTo().alert();

            simpleAlert.accept();

            System.out.println(“Unexpected alert accepted”);

        } catch (Exception e) {

            System.out.println(“unexpected alert not present”);

        }

        driver.quit();

    }}

We have to launch the browser and open the site which has alerts.

Then locate the WebElement “timerAlertButton”and click on it.

Explicit wait as the driver will wait  for 10 seconds to see if an alert occurs and it will check for alert using the try-catch block.Here Explicit wait  used as WebDriverWait wait=new WebDriver(driver ,10); and wait.until(ExpectedCondition.alertlsPresent());

If the alert will be presentit will accept using driver.switchTo ().alert ().accept (); method will go to the catch block and the message “unexpected alert not present” will be displayed.

Questions

  1. What is Alert Interface?
  2. Demonstrate an example of alert interface using selenium?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class