Wednesday, June 26, 2024

Handling Alert popup:

 

Handling Alert popup:

*** FAQ : How can you handle alert  popup window ?

Handling alert popups in Selenium WebDriver involves several methods and considerations, especially when dealing with both regular and confirmation-type alerts. Here's a detailed approach to handling them:

Types of Alert Popups:

1.     Regular Alerts (Simple Alerts):

    • These alerts display a message but do not require user input (e.g., informational messages).

2.     Confirmation Alerts (Prompt Alerts):

    • These alerts require the user to take an action, such as confirming or cancelling an operation.

 

 

 After saving data in customer/ employee/  order page-  it displays some  alert popup window

saying "customer ='Ram' is created succefullly"

            Order No.123 created succefully

 

Note : 

-----

1. Alert popup window -- cannot be inspected

 We cannot inspect any element in alert window

 

2. Some alert popup window ele - we can inspect

Methods to Handle Alerts in Selenium WebDriver:

1. Switch to Alert:

  • Use switchTo().alert() method to switch the driver's focus to the alert.
java
Alert alert = driver.switchTo().alert();

2. Get Text from Alert:

  • Retrieve the text displayed in the alert using getText() method.
java
String alertText = alert.getText();

3. Accepting (Clicking OK) or Dismissing (Clicking Cancel) Alerts:

  • Use accept() method to click on the 'OK' or 'Yes' button.
  • Use dismiss() method to click on the 'Cancel' or 'No' button.
java
alert.accept(); // To accept the alert (click on 'OK' or 'Yes')
alert.dismiss(); // To dismiss the alert (click on 'Cancel' or 'No')

4. Send Data to Confirmation Alerts (Prompt Alerts):

  • For confirmation alerts that expect user input, use sendKeys() method before accepting or dismissing.
java
alert.sendKeys("Text to send"); // Send text to prompt alert

 

package AlertFrameWindowsBasics;

 

import java.time.Duration;

 

import org.openqa.selenium.Alert;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

 

public class AlertBasics1 {

 

            public static void main(String[] args) throws InterruptedException {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

 

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

 

                        //***********************************

 

                        //   Click 'Display alert box in 3 seconds' button  to get alert popup window

                        System.out.println("Click 'Display alert box in 3 seconds'  to get alert popup window");

                        driver.findElement(By.id("alertid1")).click();

 

 

                        //  it is taking some time to display alert -- we have to define explicit wait

                        //   condition :  wait for till  alert window is present

                        WebDriverWait  wait  = new WebDriverWait(driver, Duration.ofSeconds(10));

                        wait.until(ExpectedConditions.alertIsPresent());

                        //   max time is over--> TimeoutException

 

 

                        //  switch control  from main page to alert popup window

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

 

                        //                                 org.openqa.selenium.NoAlertPresentException: no such alert

                        ///  if there is not alert popup window, and we try to swicth to alert popup window, we will get 'NoAlertPresentException:

 

 

                        // alert is pre-defined Interface in Selenium

                        //  which has some methods to handle alert window

                        //         1.gettext() : to get text from alert window

                        //                                 //2Accept() :  click ok btn

                        //         3. dismiss() :  to dismisss alert //  click cancel button

                        //         4.sendkeys();

 

                        Thread.sleep(5000);

                        //  to get text from alert window

                        String alertPopupMsg = al.getText();

                        //                        Please enter Employee name

                        // alertPopupMsg = Please enter Employee name

                        System.out.println("alertPopupMsg="+alertPopupMsg);

                        // alertPopupMsg=Please enter Employee name

 

 

                        // Send data 'Ramu' to alert

                        al.sendKeys("Ramu");

                        // not working  ???

                        Thread.sleep(3000);

 

                        // accept alert -- i.e click ok btn/ yes button

//                     al.accept();

 

                        //  to dismiss alert //  click cancel button/ No button

                        al.dismiss();

 

                        // driver.switchTo().alert();//  no alertPresent Exception

                        // org.openqa.selenium.NoAlertPresentException: no such alert

                        // If no alert popup window is present, if we try to switch to alert popup window, it throws 'NoAlertPresentException'

 

                        // note :  control is coming to main page by default

                        /// enter some value in first name text box

                        driver.findElement(By.id("idfirst")).sendKeys("Ramu");

 

 

            }

 

}

 

 

-------------------------------------------

 

Revision:

--------

  

   Alert al=  driver.switchto().alert();

   get popup msg --> 

   send data --> 

   accept() --> 

   dismiss()--> 

  no ele -->

  no alert -->  if we try switch to alert -->

Can we inspect alert popup window -->

-------------------------------------------

 

                        // HW   Click 'Display Multiple alert msg' btn  to get alert popup window

 

                        System.out.println("Click 'Display Multiple alert msg'  to get alert popup window");

                        // HW get the msg from alert window

//o/p:Please enter Employee name

 

//HW click Ok button

 

//HW get popup msg  i.e Order No : 101 created successfully

 

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...