How to Handle Alerts in Selenium Webdriver?

Welcome back to the Selenium WebDriver Testing Free Training series. We are going to talk about how to manage alerts with Selenium web driver code. Well, alerts are JavaScript’s functionalities. Alerts show text in the dialog box that pops up on the screen, and all browsers support the alerts.

Before jumping to the main discussion, let’s take a look at where alerts are used, and what are the types of alerts?

Let’s say you are submitting a form, and it has a number of fields, in other words, the form is pretty long. Somehow the user missed one or more fields or entered the wrong value, that’s where the alert comes, and it shows the error.  Or let’s say the developer wants to alert the user like your session is about to end.

There will be more scenarios where developers can use alerts.

All right. Let’s move to the next point, the types of alerts.

1. Alert Box

This alert box is used to when you wish to show some information to user.

When an alert box pops up, the user will have to click “OK” to proceed.

Let’s see code snippets to manage the alert box:
package seleniumWebDriverCodes;

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

public class HandleAlertTraining {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "chromedriverpath");
        driver = new ChromeDriver();
        driver.get("http://www.training.qaonlinetraining.com/testPage.php");
        // accept(You accepted the alert.)
        driver.findElement(By.id("alert")).click();
        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText());
        Thread.sleep(3000);
        alert.accept();

    }

}

 

In the above code, you can see switchTo() method. This method helps to switch the control to a new window or alert box from the main window.

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

The above code will switch control to the alert box. To switch back control to the main window, you can use the following code:

 

driver.switchTo().defaultContent()

 

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

The above code snippet is used to get an alert message.

 Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:For Instructor-led training

Master of Functional Automation Testing

For Self-Driven training

Automation with Selenium WebDriver (Java)

2. Confirm Box

As the name suggests, this box is used when you want to user to verify or accept. When the confirm box pops up, the user has to be clicked on the OK button to accept or click on the Cancel button to deny.

Let’s see code snippets to manage the confirm box:
package seleniumWebDriverCodes;

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

public class HandleAlertTraining {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "chromedriverpath");
        driver = new ChromeDriver();
        driver.get("http://www.training.qaonlinetraining.com/testPage.php");

        // dismiss(You pressed Cancel!)
        driver.findElement(By.id("confirm")).click();
        Alert alert1 = driver.switchTo().alert();
        System.out.println(alert1.getText());
        Thread.sleep(3000);
        alert1.dismiss();

    }

}
If you see the above code snippets, there is one method:
driver.switchTo().alert().dismiss(): This is to cancel or deny.
 3. Prompt Box

This box is used to take user input before entering a page. So, when a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value.

package seleniumWebDriverCodes;

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

public class handleAlertTraining {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "chromedriverpath");
        driver = new ChromeDriver();
        driver.get("http://www.training.qaonlinetraining.com/testPage.php");
        
        //accept and Type(Hello Mr. Bond! How are you today?)
        driver.findElement(By.id("prompt")).click();
        Alert alert2 = driver.switchTo().alert();
        System.out.println(alert2.getText());
        Thread.sleep(3000);
        alert2.sendKeys("Mr. Bond");
        alert2.accept();

    }

}

If you see above code snippets, there is one method:

driver.switchTo().alert().accept(): This is to accept.

Conclusion

This is how we manage the alerts thanks to selenium web driver code.  Similarly, a web page also has one or more than one iframe, but managing or handling the iframes is a bit different that we will see in the next tutorial.

Until then, Happy Learning 😀