DEV Community

Cover image for 🛑 How To Handle Alerts/Popups in Selenium? (with Source Code)
Pramod Dutta
Pramod Dutta

Posted on • Edited on

🛑 How To Handle Alerts/Popups in Selenium? (with Source Code)

✅ Join us - https://sendfox.com/thetestingacademy

In this video, We are going to learn How To Handle Alerts in Selenium and How To Popups Alerts in Selenium, I am going to show step by step How to handle them with accept, dismiss and sendkeys methods provided by Selenium.

Selenium alerts

🚀 Download Solution : https://scrolltest.com/automation/day16

🚀 What is Javascript Alert?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. ... JavaScript functions are called in response to events. When the page loads, or when a user clicks on, hovers over, or tabs to a specific link, these are all events.

🚀 Handling Confirm Box in Selenium

A confirm box is often used if you want the user to verify or accept something.

When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.

If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

🚀 What is Prompt Box and How to handle it in Selenium?

A prompt box is often used if you want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.

If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
Source - https://www.w3schools.com/js/js_popup.asp

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

Full Source Code.

package SeleniumCommands;

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

public class AlertPopups {
    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/javascript_alerts");
        driver.manage().window().maximize();
        //
        driver.findElement(By.xpath("//button[contains(text(),'Click for JS Alert')]")).click();
        Alert alert = driver.switchTo().alert();
        alert.accept();
        String result = driver.findElement(By.cssSelector("#result")).getText();
        System.out.println(result);
        Assert.assertEquals("You successfuly clicked an alert",result);


        driver.findElement(By.xpath("//button[contains(text(),'Click for JS Confirm')]")).click();
        Alert alert2 = driver.switchTo().alert();
        alert2.dismiss();
        String result2 = driver.findElement(By.cssSelector("#result")).getText();
        System.out.println(result2);
        Assert.assertEquals("You clicked: Cancel",result2);

        driver.findElement(By.xpath("//button[contains(text(),'Click for JS Confirm')]")).click();
        Alert alert3 = driver.switchTo().alert();
        alert3.accept();
        String result3 = driver.findElement(By.cssSelector("#result")).getText();
        System.out.println(result3);
        Assert.assertEquals("You clicked: Ok",result3);
driver.findElement(By.xpath("//button[contains(text(),'Click for JS Prompt')]")).click();
        driver.switchTo().alert().sendKeys("Hello Pramod");
        driver.switchTo().alert().accept();
        String result4 = driver.findElement(By.cssSelector("#result")).getText();
        System.out.println(result4);
        Assert.assertEquals("You entered: Hello Pramod",result4);
        //

        driver.get("https://admin:admin@the-internet.herokuapp.com/basic_auth");
        String result5 = driver.findElement(By.xpath("//p[contains(text(),'Congratulations! You must have the proper credenti')]")).getText();
        System.out.println(result5);
        Assert.assertEquals("Congratulations! You must have the proper credentials.",result5);
        driver.quit();

    }
}

🚀 Demo Website Used - https://the-internet.herokuapp.com/javascript_alerts

--
Be sure to subscribe for more videos like this!

 TheTestingAcademy

Top comments (2)

Collapse
 
bravemaster619 profile image
bravemaster619

Nice, article! Can you make a puppeteer version too?

Collapse
 
promode profile image
Pramod Dutta

Great idea, let me do it.