In this section, we will explore how to handle alerts and pop-ups using Selenium WebDriver. Alerts and pop-ups are common elements in web applications, and being able to interact with them is crucial for automating tests effectively.

Key Concepts

  1. Types of Alerts:

    • Simple Alert: Displays a message and an OK button.
    • Confirmation Alert: Displays a message with OK and Cancel buttons.
    • Prompt Alert: Displays a message with a text input field and OK/Cancel buttons.
  2. Handling Alerts:

    • Selenium provides the Alert interface to interact with alert boxes.
    • Common methods include accept(), dismiss(), getText(), and sendKeys().

Practical Examples

Example 1: Handling a Simple Alert

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

public class SimpleAlertExample {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("http://example.com/simple-alert");

        // Trigger the alert
        driver.findElement(By.id("trigger-alert")).click();

        // Switch to alert
        Alert alert = driver.switchTo().alert();

        // Get alert text
        System.out.println("Alert Text: " + alert.getText());

        // Accept the alert
        alert.accept();

        driver.quit();
    }
}

Explanation:

  • We navigate to a page with a simple alert.
  • We trigger the alert using a button click.
  • We switch to the alert using driver.switchTo().alert().
  • We print the alert text and accept it using alert.accept().

Example 2: Handling a Confirmation Alert

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

public class ConfirmationAlertExample {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("http://example.com/confirmation-alert");

        // Trigger the confirmation alert
        driver.findElement(By.id("trigger-confirmation")).click();

        // Switch to alert
        Alert alert = driver.switchTo().alert();

        // Dismiss the alert
        alert.dismiss();

        driver.quit();
    }
}

Explanation:

  • Similar to the simple alert, but here we use alert.dismiss() to cancel the alert.

Example 3: Handling a Prompt Alert

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

public class PromptAlertExample {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("http://example.com/prompt-alert");

        // Trigger the prompt alert
        driver.findElement(By.id("trigger-prompt")).click();

        // Switch to alert
        Alert alert = driver.switchTo().alert();

        // Send text to the prompt
        alert.sendKeys("Selenium");

        // Accept the alert
        alert.accept();

        driver.quit();
    }
}

Explanation:

  • For prompt alerts, we use alert.sendKeys("text") to input text into the alert.

Practical Exercises

Exercise 1: Handle a Simple Alert

Task: Write a Selenium script to handle a simple alert on a webpage. Print the alert text and accept it.

Solution:

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

public class ExerciseSimpleAlert {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("http://example.com/simple-alert");

        driver.findElement(By.id("trigger-alert")).click();

        Alert alert = driver.switchTo().alert();
        System.out.println("Alert Text: " + alert.getText());
        alert.accept();

        driver.quit();
    }
}

Exercise 2: Handle a Confirmation Alert

Task: Write a Selenium script to handle a confirmation alert. Dismiss the alert and verify the action.

Solution:

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

public class ExerciseConfirmationAlert {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("http://example.com/confirmation-alert");

        driver.findElement(By.id("trigger-confirmation")).click();

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

        // Verify the action (e.g., check a message on the page)
        // Example: Assert.assertEquals(driver.findElement(By.id("message")).getText(), "Action cancelled");

        driver.quit();
    }
}

Common Mistakes and Tips

  • Not Switching to Alert: Always switch to the alert before interacting with it.
  • Ignoring Alert Types: Understand the type of alert you are dealing with to use the correct methods.
  • Handling Unexpected Alerts: Use try-catch blocks to handle unexpected alerts gracefully.

Conclusion

In this section, we covered how to handle different types of alerts and pop-ups using Selenium WebDriver. Mastering these interactions is essential for automating tests that involve user prompts and confirmations. In the next section, we will explore managing browser windows and frames, which will further enhance your ability to automate complex web applications.

Test Automation with Selenium

Module 1: Introduction to Test Automation

Module 2: Getting Started with Selenium

Module 3: Locating Web Elements

Module 4: Interacting with Web Elements

Module 5: Synchronization in Selenium

Module 6: Test Frameworks and Selenium

Module 7: Advanced Selenium Concepts

Module 8: Selenium Grid and Parallel Testing

Module 9: Continuous Integration and Selenium

Module 10: Best Practices and Troubleshooting

© Copyright 2024. All rights reserved