Selenium WebDriver

Alert Handling in Selenium

Master alert popup handling including simple alerts, confirmation alerts, prompt alerts, and authentication popups using Alert interface.

Alert Handling in Selenium

Why Handle Alerts?

We handle Alert Pop up in Selenium by using Alert Interface. Basically we can't inspect the alert pop up elements, so we have to use Alert interface methods to handle the alert pop up objects/elements.

🔑 Key Points

  • • Alert elements cannot be inspected in browser developer tools
  • • Must switch focus from main window to alert window
  • • Use Alert interface methods to interact with alert elements
  • • Different types of alerts require different handling approaches

Alert Interface Methods

Switch Focus to Alert

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

accept() Method

Click on OK button

alt.accept(); // void

dismiss() Method

Click on Cancel button

alt.dismiss(); // void

getText() Method

Capture alert text

String text = alt.getText();

sendKeys() Method

Enter value in alert textbox

alt.sendKeys("value");

Types of Alert Popups

1. Simple Alert Popup

Components:

  • • Alert popup text
  • • OK button

Example: https://mail.rediff.com/cgi-bin/login.cgi

Click Login button without entering credentials to trigger alert

package Tutorial6;

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

public class Demo1 {

    public static void main(String[] args) {

        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://mail.rediff.com/cgi-bin/login.cgi");

        // Locate Log In Button and click on it
        driver.findElement(By.name("proceed")).click();

        // Switch focus from Main window to Alert Pop Up window
        Alert alt = driver.switchTo().alert();

        // Capture Alert Pop Text
        String a = alt.getText();
        System.out.println(a);

        // Click on Alert Pop Up OK button
        alt.accept();
    }
}

2. Confirmation Alert Popup

Components:

  • • Alert popup text
  • • OK button
  • • Cancel button

Example: https://demoqa.com/alerts

Click on 3rd "Click Me" button (confirmButton)

ChromeDriver driver = new ChromeDriver();
driver.get("https://demoqa.com/alerts");

// Find and click on 3rd Click Me button
driver.findElement(By.id("confirmButton")).click();

// Switch focus from main window to alert window
Alert alt = driver.switchTo().alert();

// Capture alert pop up text
String a = alt.getText();
System.out.println(a);

// Click on Cancel button
alt.dismiss();

3. Prompt Alert Popup

Components:

  • • Alert popup text
  • • OK button
  • • Cancel button
  • • Alert popup text box

Example: https://demoqa.com/alerts

Click on 4th "Click Me" button (promtButton)

ChromeDriver driver = new ChromeDriver();
driver.get("https://demoqa.com/alerts");

// Find and click on 4th click me button
driver.findElement(By.id("promtButton")).click();

// Switch focus main window to alert window
Alert alt = driver.switchTo().alert();

// Capture the alert pop up text
String a = alt.getText();
System.out.println(a);

// Enter value in alert pop
alt.sendKeys("Durga");

Thread.sleep(4000);

// Click on ok button
alt.accept();

4. Authentication Alert Popup

Components:

  • • Username text box
  • • Password text box
  • • Sign In button
  • • Cancel button

🔐 Special Handling

Authentication alerts that appear immediately after opening URL are handled by providing credentials in the URL itself.

https://username:password@url

Example: Basic Authentication

Username: admin
Password: admin
URL: https://the-internet.herokuapp.com/basic_auth
package Tutorial6;

import org.openqa.selenium.chrome.ChromeDriver;

public class Demo4 {

    public static void main(String[] args) {

        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        // Handle authentication by embedding credentials in URL
        driver.get("https://admin:admin@the-internet.herokuapp.com/basic_auth");
    }
}

switchTo() Method Overview

WebDriver switchTo() Capabilities

switchTo() Method Usage

switchTo() method is present in WebDriver Interface and returns TargetLocator interface. It's used to switch focus to:

  • • Alert window
  • • Frame window
  • • New window/tab
  • • Different tabs or windows

Interface Details

WebDriver Interface:
driver.switchTo() → TargetLocator
TargetLocator Interface:
.alert() → Alert Interface

Alert Interface Methods

• accept() → void
• dismiss() → void
• getText() → String
• sendKeys() → void

Practice URLs

Alert Practice Websites

Simple & Confirmation Alerts

  • • https://demo.automationtesting.in/Alerts.html
  • • https://demoqa.com/alerts
  • • https://mail.rediff.com/cgi-bin/login.cgi
  • • https://rahulshettyacademy.com/AutomationPractice/

Authentication Alerts

  • • https://the-internet.herokuapp.com/basic_auth
  • • https://admin:admin@the-internet.herokuapp.com/basic_auth

Day 16 Knowledge Check

Question 1 of 5

Why can't we inspect alert popup elements in browser developer tools?

SDET Mastery

Master Test Automation

Home
CurriculumPracticeQ&ACheatsheet
🍵Buy me a Chai

Automation Testing Course

Comprehensive course covering Manual Testing, Java Programming, and Selenium WebDriver

🍵Buy me a Chai
Privacy Policy•GitHub
© 2024 Automation Testing Course. All rights reserved.