Alert in Selenium – All Methods with Examples

Alert in Selenium post is a complete tutorial for all tester who needs to do proper Alert Handling in Selenium projects. The Selenium Alert methods are Accept, Dismiss, getText, and sendKeys. We will learn how to work with these methods while we are doing web automation with selenium webdriver.

Alert Handling in Selenium?

Generally speaking, modern websites do not contain alerts too much. However, when we are faced with an alert, we should know how to handle it. In JavaScript code alert(“Hello World!”) is an example of an alert and we can handle alerts by switching to alert window and then we call webdriver alert API’s interrogation and manipulation methods. These are:

  • .getText() – Gets the text of an alert
  • .sendKeys(String) – Send string to the alert
  • .accept() – Clicks the “Ok” button of the alert
  • .dismiss() – Clicks the “Cancel” button of the alert

Note: Alert window is an entirely different window so before using the above methods we have to switch to the alert window by using switchTo() method.

Now it is time to do exercises! :)

Selenium Switch to Alert, getText, and Accept Example

Test site: https://www.w3schools.com/js/tryit.asp?filename=tryjs_alert

Test Scenario:

  • Navigate to the above link.
  • Click the “Try it” button
  • Switch to alert and get the alert’s text
  • Do verification
  • Switch to alert and Accept the alert (In other words, Click the OK button.)

CSS of “Try it” Button: html>body>button

Test Code:

@Test
public void T01_AlertTest() {
    driver.navigate().to("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
    //Switch to iframeResult iframe because all elements located in this iframe
    driver.switchTo().frame("iframeResult");

    //Find "Try it" button
    WebElement alertButton = driver.findElement(By.cssSelector("html>body>button"));

    //Click alert button ("Try it" button)
    alertButton.click();

    //Alert Message (Expected Text)
    String expectedAlertMessage = "I am an alert box!";

    //Captured Alert Text (Actual Text)
    String actualAlertMessage = driver.switchTo().alert().getText();

    //Assertion
    Assertions.assertEquals(expectedAlertMessage, actualAlertMessage);

    //Accept the alert (Click OK)
    driver.switchTo().alert().accept();
}

Selenium Alert Accept and Alert Dismiss

Test site: http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm

Test Scenario-1 (Accept):

  • Navigate to the above link.
  • Click the “Try it” button
  • Click the OK button. (Accept it)
  • Check expected text as “You pressed OK!”

Test Scenario-2 (Dismiss):

  • Navigate to the above link.
  • Click the “Try it” button
  • Click the Cancel button. (Dismiss it)
  • Check expected text as “You pressed Cancel!”

Test Code:

@Test
public void T02_ConfirmTest() {
    driver.navigate().to("http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm");
    //Switch to iframeResult iframe because all elements located in this iframe
    driver.switchTo().frame("iframeResult");

    //Find "Try it" button
    WebElement confirmButton = driver.findElement(By.cssSelector("html>body>button"));

    //Actual Text Element
    WebElement actualConfirmMessage = driver.findElement(By.cssSelector("#demo"));

    //******************************
    // Accept Test (Test Scenario-1)
    //******************************
    //Click confirm button ("Try it" button)
    confirmButton.click();

    //Accept the alert (Click Ok button)
    driver.switchTo().alert().accept();

    //Assertion
    Assertions.assertEquals("You pressed OK!", actualConfirmMessage.getText());

    //******************************
    // Dismiss Test (Test Scenario-2)
    //******************************
    //Click confirm button ("Try it" button)
    confirmButton.click();

    //Accept the alert (Click Cancel)
    driver.switchTo().alert().dismiss();

    //Assertion
    Assertions.assertEquals("You pressed Cancel!", actualConfirmMessage.getText());
}

Selenium Alert SendKeys

Test site: http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt

Test Scenario:

  • Navigate to the above link.
  • Click the “Try it” button
  • Write “SW Test Academy” to the alert text box.
  • Click the OK button. (Accept it)
  • Check expected text as “Hello SW Test Academy! How are you today?”

Test Code:

@Test
public void T03_PromptTest() {
    driver.navigate().to("http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt");
    //Switch to iframeResult iframe because all elements located in this iframe
    //It will be described in next topics
    driver.switchTo().frame("iframeResult");

    //Find "Try it" button
    WebElement promptButton = driver.findElement(By.cssSelector("html>body>button"));

    //Actual Message
    WebElement actualPromptMessage = driver.findElement(By.cssSelector("#demo"));

    //Click confirm button ("Try it" button)
    promptButton.click();

    //Send "SW Test Academy" to Alert's text box
    driver.switchTo().alert().sendKeys("SW Test Academy");

    //Accept the alert (Click Ok button)
    driver.switchTo().alert().accept();

    //Assertion
    Assertions.assertEquals("Hello SW Test Academy! How are you today?", actualPromptMessage.getText());
}

Summary

We learned how to work with Selenium Accept, Dismiss, getText, and sendKeys Alerts methods.

Github Project

https://github.com/swtestacademy/selenium-examples/tree/main/src/test/java/alerts

Thanks,
Onur Baskirt

4 thoughts on “Alert in Selenium – All Methods with Examples”

  1. WebElement confirmButton = driver.findElement(By.cssSelector(“html>body>button”));

    This is not working. Tried other locators as well. None works.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.