1.

How To Handle web-based Alerts/Pop-Ups In Selenium?

Answer»

A small message/dialogue box that appears on the top of a web application and it provides some information/warning to the USER is an alert. It may expect input information from the user.

In web applications, there are three kinds of popup boxes,

  • Alert box
  • Confirm box
  • Prompt box

Alert box-

To display some information to the user an alert box is used. The user needs to click the OK button displayed on an alert box that shows up to proceed.

Confirm box -

When an application wants to get confirmation from the user, a confirmation box is used. The user needs to click on either Yes/Ok button or No/Cancel button on the confirm box to proceed further.

Prompt box -

When an application expects input from the user immediately after opening it, a prompt box is used. After ENTERING the input, the user needs to click on either Yes/Ok button or No/Cancel button when the confirm box shows up to proceed further.

You can’t inspect any button or textbox present on any popup boxes using the Developer tool. Pop-up boxes block screen and appear on top of the screen. USERS cannot PERFORM any action on screen unless you handle these pop-ups.

User can perform below operations on these pop-ups using selenium are,

  1. Accept - to click on the OK button
  2. Dismiss - to click on Cancel button
  3. SendKeys - to enter text in the text box available on the pop-up
  4. GetText - to get label text on pop-up

Below is the code to handle alert pop-up. Please note element and test SITE is a dummy –

String alertBoxOutput, popupText;      System.setProperty("webdriver.chrome.driver", "./Resources/chromedriver.exe");  WebDriver driver = new ChromeDriver();  driver.manage().window().maximize();      driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);      driver.get("https://www.testtutorial.com");  Thread.sleep(2000);    // Alert popups      driver.findElement(By.id("alertBox")).click();  popupText = driver.switchTo().alert().getText();  Assert.assertEquals(popupText, "I am an alert box!");  driver.switchTo().alert().accept();  alertBoxOutput = driver.findElement(By.id("output")).getText();  Assert.assertEquals(alertBoxOutput, "You selected alert box");  // Confirmation popups      driver.findElement(By.id("confirmBox")).click();  popupText = driver.switchTo().alert().getText();      Assert.assertEquals(popupText, "Press a button!");  driver.switchTo().alert().accept();  alertBoxOutput = driver.findElement(By.id("output")).getText();  Assert.assertEquals(alertBoxOutput, "You pressed OK in confirm box");  driver.findElement(By.id("confirmBox")).click();  driver.switchTo().alert().dismiss();  alertBoxOutput = driver.findElement(By.id("output")).getText();  Assert.assertEquals(alertBoxOutput, "You pressed Cancel in confirm box");  // Prompt pop ups      driver.findElement(By.id("promptBox")).click();  popupText = driver.switchTo().alert().getText();  Assert.assertEquals(popupText, "Please enter your name:");      driver.switchTo().alert().sendKeys("Reddy");  driver.switchTo().alert().accept();  alertBoxOutput = driver.findElement(By.id("output")).getText();  Assert.assertEquals(alertBoxOutput, "You entered text Reddy in prompt box");      driver.findElement(By.id("promptBox")).click();  driver.switchTo().alert().dismiss();  alertBoxOutput = driver.findElement(By.id("output")).getText();  Assert.assertEquals(alertBoxOutput, "You pressed Cancel in prompt box.");     driver.quit();


Discussion

No Comment Found