Selenium Alert & Popup Window Handling

In this article we will see how to handle popup in Selenium and different types of alerts found in web applications Testing. We will also see how to handle Alert in Selenium WebDriver and how do we accept and reject the alert depending upon the alert types.

What is Alert in Selenium?

An Alert in Selenium is a small message box which appears on screen to give the user some information or notification. It notifies the user with some specific information or error, asks for permission to perform certain tasks and it also provides warning messages as well.

1) Simple Alert

The simple alert class in Selenium displays some information or warning on the screen.

Code to accept alert using alert() method of Selenium

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SimpleAlert {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//code to maximize chrome browser
driver.manage().window().maximize();
String baseURL = "https://www.rahulshettyacademy.com/AutomationPractice/";
driver.get(baseURL);

//Click on alert button and click on accept button using below alert() function.
driver.findElement(By.id("alertbtn")).click();
Thread.sleep(5000);
//Code to accept the alert pop up
Alert alert = driver.switchTo().alert();
alert.accept();


//Code to close chrome driver.
driver.close();
     }
}

2) Prompt Alert

This Prompt Alert asks some input from the user and Selenium webdriver can enter the text using sendkeys(" input…. ").

Code to enter value in edit box and press alert button and accept the Alert.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class PromptAlert {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//code to maximize chrome browser
driver.manage().window().maximize();

String baseURL = "https://www.rahulshettyacademy.com/AutomationPractice/";
driver.get(baseURL);

//Enter value in edit box and press ALert button.
driver.findElement(By.name("enter-name")).sendKeys("Rahul");
driver.findElement(By.id("alertbtn")).click();
Thread.sleep(5000);
//Code to accept the alert pop up
Alert alert = driver.switchTo().alert();
alert.accept();


//Code to close chrome driver.
driver.close();
}
}

3) Confirmation Alert.

This confirmation alert asks permission to do some type of operation.

Code to accept confirmation Alert.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ConfirmAlert {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//code to maximize chrome browser
driver.manage().window().maximize();

String baseURL = "https://www.rahulshettyacademy.com/AutomationPractice/";
driver.get(baseURL);

//Enter value in edit box and press ALert button.
driver.findElement(By.name("enter-name")).sendKeys("Rahul");
driver.findElement(By.id("confirmbtn")).click();
Thread.sleep(5000);
//Code to accept the alert pop up
Alert alert = driver.switchTo().alert();
alert.accept();


//Code to close chrome driver.
driver.close();
}
}

How to handle Alert in Selenium WebDriver.

Alert interface provides the below few methods which are widely used in Selenium Webdriver.

1) void dismiss()

To click on the 'Cancel' button of the alert.

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

2) void accept()

To click on the 'OK' button of the alert.

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

  • String getText()

To capture the alert message.

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

  • void sendKeys(String stringToSend)

To send some data to alert box.        

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

How to handle Selenium Popup window using WebDriver.

In automation, when we have multiple windows in any web application, the activity may need to switch control among several windows from one to other in order to complete the operation. After completion of the operation, it has to return to the main window i.e. parent window in Selenium. We will see this further in the article with an example.

In Selenium web driver there are methods through which we can handle multiple windows.

Driver.getWindowHandles();

To handle all opened windows by web driver, we can use "Driver.getWindowHandles()" and then we can switch window from one window to another in a web application. Its return type is Iterator<String>.

Driver.getWindowHandle();

When the site opens, we need to handle the main window by driver.getWindowHandle(). This will handle the current window that uniquely identifies it within this driver instance. Its return type is String.

For Window handling in Selenium, we will follow the below Code:

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;

public class WindowHandleSelenium {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        //code to maximize chrome browser
        driver.manage().window().maximize();

        String baseURL = "https://www.rahulshettyacademy.com/AutomationPractice/";
        driver.get(baseURL);

        //code to open new window of browser.
        driver.findElement(By.id("openwindow")).click();
        driver.manage().window().maximize();

        //Code for Parent Window
        String parentWindow = driver.getWindowHandle();

        //Code to handle all new windows opened.
        Set<String> childWindows = driver.getWindowHandles();

        for (String childWindow : childWindows) {
            if (!parentWindow.equalsIgnoreCase(childWindow)) {
                driver.switchTo().window(childWindow);
                driver.findElement(By.linkText("Login")).click();
                //Close child window
                driver.close();
            }
        }
        //code to switch back to parent window.
        driver.switchTo().window(parentWindow);

        //Code to close parent window.
        driver.close();
    }
}

Conclusion

  • We defined the types of alert and shown them with a screen shot.
  • Demonstrated handling the Alert with Selenium WebDriver using particular scenario.
  • Handled multiple windows with Selenium WebDriver using particular scenario.

Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}