Handling alerts and pop-ups

A pop-up is a browser window that opens randomly on surfing the Internet through the web browser. Web applications generate three different types of pop-ups, namely:

  • JavaScript alert (pop-ups) (for example, advertisements)
  • Browser pop-up (for example, a confirmation dialog box, an authentication prompt, and so on)
  • Native OS pop-ups (for example, Windows pop-ups such as upload/download notfications)

JavaScript pop-ups are generally in the form of alerts and advertisements, especially for marketing purposes. Selenium WebDriver provides an API to handle the JavaScript pop-ups. The following is an example of an alert:

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

Some of the helpful snippets using JavaScript alert functions are as follows:

  • The dismiss() function ignores or cancels the alert dialog box. The following is the syntax for this function:
    alert.dismiss();

    Let's see an example to wait for the alert dialog box, which is expected to appear, and then skip it to move forward:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    try {
      wait.until(ExpectedConditions.alertIsPresent());
      Alert alert = driver.switchTo().alert();
      alert.dismiss();
    } catch (Exception e) {
      System.out.println("Alert is not available");
    }
  • The accept() function acknowledges the alert dialog box upon our clicking the OK button.
    alert.accept();

    In general, the alert dialog box waits for an action to be initiated; it can be either alert cancellation or acceptance. Acceptance is one of two ways to close the alert pop-up, but with a reason. Let's see how to accept an alert dialog box by clicking on the OK button:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    try {
      wait.until(ExpectedConditions.alertIsPresent());
      Alert alert = driver.switchTo().alert();
      alert.accept();
    } catch (Exception e) {
      System.out.println("Alert is not available");
    }
  • The getText() function retrieves text or string values from the alert dialog box. The following is the syntax for this function:
    alert.getText();

    This method is normally used to verify the alert dialog box by fetching text values from the pop-up.

    Alert alert = driver.switchTo().alert();
    alert.getText();
  • The sendKeys() method passes text or string values to the alert dialog box. The following is the syntax for this function:
    alert.sendKeys(String arg0);

    Sending text values into an alert pop-up dialog box is quite possible using these alert methods. Let's see a small piece of code to achieve this:

    Alert alert = driver.switchTo().alert();
    alert.sendKeys("Text to be passed");
  • The authenticateUsing() method handles the basic HTTP authentication by passing the username and password to the browser pop-up. The following is the syntax for this function:
    UserAndPassword user = new UserAndPassword("USERNAME", "PASSWORD");
    alert.authenticateUsing(user);

    Authentication is a consistent blocker for many automation testers. Eventually, this method gets you into an environment to test with an API that helps you skip basic authentication blockers. Let's see an example to operate authentication pop-ups by passing valid credentials, as follows:

    WebDriverWait wait = new WebDriverWait(driver, 10);      
    Alert alert = wait.until(ExpectedConditions.alertIsPresent());
    alert.authenticateUsing(new UserAndPassword("USERNAME", "PASSWORD"));
    Actions action = new Actions(driver);
..................Content has been hidden....................

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