Alerts in Selenium


     Alerts in Selenium



    I. Alert introduction

    Alerts on the webpage used to get the attention of the user to perform some operation on the alert or the webpage; sometimes, the alert expects input from the user. All the alerts are formed using javascript inside the HTML page; there are three kinds of alerts in HTML.

    Alert takes the focus away from the current window and forces the browser to read the message. Alert prevents the user from accessing other parts of the web page until the box is closed.

    Alert Types :

    • Alert
    • Confirmation dialog
    • Prompt
    Properties of Alerts with respect to Selenium :
    • We cannot identify alerts using inspect tools
    • We cannot write xpaths for alerts
    • It is not a Window
                We cannot handle alerts using javaScript Executor

    II. Common methods with alert




              1. Switch to alert popup

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

    2. Accept the popUp by clicking the OK button

    ale.accept();

    3. Dismiss the popUp by clicking the 'X' icon.

    ale.dismiss();

    4. Get popUp text by getText method

    ale.getText();

    5. Send a text to popUp by sendKeys (Applicable for Prompt only)

    ale.sendKeys("test Text");

    II. Alert

    The alert() method displays an alert box with a message and an OK button; an alert box is often used to inform a user about a particular operation like details saved in Database, right-click disabled, Loaded successfully such kind of messages.

    Alert is formed using alert("message") in javascript, alert considers the same irrespective of user operation whether he clicks OK or 'X' icon.

    Alert's sole purpose is to inform the user, nothing more.

    Alert on Different browsers
    Chrome:
    alert-on-chrome-selenium-webdriver

    Firefox
    alert-on-firefox-selenium-webdriver

      • Open Url : https://chercher.tech/practice/practice-pop-ups-selenium-webdriver
      • Click on the Alert button; the application throws an Alert box
      • Switch to the alert using driver.switchTo().alert(), we save this object in Alert type variable 'ale'
      • We can accept the alert by using accept() non-static method from the alert API, and this closes the popup.
    driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");
    driver.findElement(By.name("alert")).click();
    Alert ale = driver.switchTo().alert();
    // clicks 'OK' button
    ale.accept();
      • We can dismiss the popup using dismiss() method from alert API, this click 'X' icon on the popup. (if you have performed the above step then you cannot perform this step as pop up is closed in step 4 itself)
    Alert ale = driver.switchTo().alert();
    // clicks 'x' icon
    ale.dismiss();
      • We can get the text from the pop-up using getText(), if you have performed steps 4 or 5, this step will not work.
    Alert ale = driver.switchTo().alert();
    // clicks 'x' icon
    ale.getText();

    get-text-alert-selenium-webdriver


    III. Confirmation Box

    A Confirmation box is the second kind of alert; it displays a dialog with the OK and Cancel button. The confirmation box informs the developer about user choice whether the user pressed OK or Cancel. The confirm() method returns true if the user clicked "OK", and false otherwise('X' icon and 'Cancel') to the developer.

    Confirmation box confirm-chrome-selenium-webdriver

    We can handle the Confirmation box in selenium like an alert box; there is no coding difference.


    IV. Prompt

    Prompt is used to get value from the user in text format. Prompt provides a text bar for user input; Prompt is rarely used alert type.

    prompt-selenium-webdriver
    Prompt also follows the same coding as alert and prompt except the sendkeys method, and we can send a text to prompt text bar using the sendkeys() method in alerts API.

    You cannot use the Keys.ENTER or such kind of keys with sendkeys() method of alerts
    driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");
    driver.findElement(By.name("prompt")).click();
    Alert ale = driver.switchTo().alert();
    ale.sendKeys("chercher.tech");


    V. How to check whether the alert is present or not

    We can use Explicit wait / WebdriverWait to check whether the alert is there or not, alertIsPresent() method waits for the alert to be present till the given timeout, once it reaches a timeout, and if the alert is not present, then it throws TimeOutException if the alert is present before the timeout it proceeds with remaining code.

    I have used only 2 seconds to check for the alert.

    public static boolean isAlertPresent(){
        boolean presenceOfAlert = false;
        WebDriverWait wait = new WebDriverWait(driver, 2 /*timeout in seconds*/);
        try {
            wait.until(ExpectedConditions.alertIsPresent());
            presenceOfAlert = true;
        } catch (TimeoutException e) {
            presenceOfAlert = false;
        }
        return foundAlert;
    }