Monday 10 August 2015

How to Handle Alerts, JavaScript Alerts and PopUp Boxes in Selenium WebDriver

Alerts are different from regular windows. The main difference is that alerts are blocking in nature. They will not allow any action on the underlying webpage if they are present. So if an alert is present on the webpage and you try to access any of the element in the underlying page you will get following exception:
UnhandledAlertException: Modal dialog present

To reproduce this exception you can use this code:

    public static void main(String[] args)
    {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.toolsqa.com/handling-alerts-using-selenium-webdriver/");
        //This step will result in an alert on screen
        driver.findElement(By.xpath("//*[@id='content']/p[4]/button")).click();

        //Once alert is present try to click on any button on the page
        driver.findElement(By.xpath("//*[@id='content']/p[16]/button")).click();

    }
 

Types of Alerts:
Java scrip provides mainly following three types of alerts:
1. Simple alert
document.alert("This is a simple alert");
//or
alert("This is a simple alert");

2. Confirmation alert
var popuResult = confirm("Confirm pop up with OK and Cancel button");

3. Prompt alert
var person = prompt("Do you like toolsqa?", "Yes/No");


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

accept() To accept the alert
dismiss() To dismiss the alert
getText() To get the text of the alert
sendKeys() To write some text to the alert
Lets use these to handle the above mentioned types of alerts one by one.

Simple alert:
Simple alerts just have a OK button on them. They are mainly used to display some information to the user. The first alert on our test page is a simple alert. Following code will read the text from the Alert and then accept the alert. Important point to note is that we can switch from main window to an alert using the driver.switchTo().alert(). Below is the usage of that also:

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.toolsqa.com/handling-alerts-using-selenium-webdriver/");
        driver.manage().window().maximize();
        // This step will result in an alert on screen
        driver.findElement(By.xpath("//*[@id='content']/p[4]/button")).click();

        Alert simpleAlert = driver.switchTo().alert();
        String alertText = simpleAlert.getText();
        System.out.println("Alert text is " + alertText);
        simpleAlert.accept();
    }


Confirmation Alert:
This alert comes with an option to accept or dismiss the alert. To accept the alert you can use Alert.accept() and to dismiss you can use the Alert.dismiss(). Here is the code to dismiss a prompt alert.

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.toolsqa.com/handling-alerts-using-selenium-webdriver/");
        driver.manage().window().maximize();
        // This step will result in an alert on screen
        WebElement element = driver.findElement(By.xpath("//*[@id='content']/p[11]/button"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].click()", element);

        Alert confirmationAlert = driver.switchTo().alert();
        String alertText = confirmationAlert.getText();
        System.out.println("Alert text is " + alertText);
        confirmationAlert.dismiss();
    }


Prompt Alerts:
In prompt alerts you get an option to add text to the alert box. This is specifically used when some input is required from the user. We will use the sendKeys() method to type something in the Prompt alert box. Here is the code


    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.toolsqa.com/handling-alerts-using-selenium-webdriver/");
        driver.manage().window().maximize();
        // This step will result in an alert on screen
        WebElement element = driver.findElement(By.xpath("//*[@id='content']/p[16]/button"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].click()", element);

        Alert promptAlert  = driver.switchTo().alert();
        String alertText = promptAlert .getText();
        System.out.println("Alert text is " + alertText);
        //Send some text to the alert
        promptAlert .sendKeys("Accepting the alert");
        Thread.sleep(4000); //This sleep is not necessary, just for demonstration
        promptAlert .accept();
    }