Handling a simple JavaScript alert box

Web developers use JavaScript alerts to inform users about validation errors, warnings, getting a response for an action, accepting an input value, and so on. Alert's are modal windows displayed by browsers where user has to take action before processing further. You can find more about JavaScript alert() method at http://www.w3schools.com/jsref/met_win_alert.asp.

JavaScript alerts are implemented differently by browsers compared to other dialog windows such as Print, Save, File Download, and so on.

Tests will need to verify that the user is shown correct alerts while testing. It is also required to handle alerts while performing an end-to-end workflow. The Selenium WebDriver provides an Alert interface for working with JavaScript alerts.

In this recipe, we will automate interaction with a simple alert box that is often used to notify the user with information such as errors, warnings, and success. When an alert box pops up, the user will have to click on the OK button to proceed, as shown in the following screenshot:

Handling a simple JavaScript alert box

How to do it...

We will use a simple page on which a simple alert box is displayed to the user after a button is clicked. Let's create a test that checks that correct information is displayed in the alert box, as shown in the following code example:

package com.secookbook.examples.chapter06;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import org.openqa.selenium.Alert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

public class AlertsTest {

  private static WebDriver driver;

  @BeforeClass
  public static void setUp() {
    driver = new FirefoxDriver();
    driver.get("http://cookbook.seleniumacademy.com/Alerts.html");
    driver.manage().window().maximize();
  }

  @Test
  public void testSimpleAlert() {
    // Click Simple button to show an Alert box
    driver.findElement(By.id("simple")).click();

    // Optionally we can also wait for an Alert box using the WebDriverWait
    new WebDriverWait(driver, 10)
        .until(ExpectedConditions.alertIsPresent());

    // Get the Alert
    Alert alert = driver.switchTo().alert();

    // Get the text displayed on Alert
    String textOnAlert = alert.getText();

    // Check correct message is displayed to the user on Alert box
    assertEquals("Hello! I am an alert box!", textOnAlert);

    // Click OK button, by calling accept method
    alert.accept();
  }

  @AfterClass
  public static void tearDown() {
    driver.quit();
  }
}

How it works...

The Selenium WebDriver provides an Alert interface for handling alerts. It provides various methods for interacting with an alert box. The driver.switchTo().alert() method returns an instance of the Alert for the alert box displayed on the screen, as follows:

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

We might need to verify what message is displayed in an alert box. We can get the text from an alert box by calling the getText() method provided by Alert, as follows:

String textOnAlert = alert.getText();

An alert box is closed by clicking on the OK button; this is be done by calling the accept() method, as follows:

alert.accept();

Tip

An alert box can also be accessed by directly calling desired methods. The following code shows an example:

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

There's more...

We can also create an explicit wait condition for an alert to be displayed on the page using the WebDriverWait class and the alertIsPresent() method of the ExpectedConditions class, as shown in the following code example:

new WebDriverWait(driver, 10)
    .until(ExpectedConditions.alertIsPresent());

// Get the Alert
Alert alert = driver.switchTo().alert();

This is useful when there is a delay after a user performs an action and an alert is displayed.

The NoAlertPresentException

The driver.switchTo().alert() method throws a NoAlertPresentException exception when it tries to access an alert box that doesn't exist.

See also

  • The Handling a Confirm and Prompt Alert Box recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.139.8