Skip to content

Instantly share code, notes, and snippets.

@pragmatictesters
Created December 26, 2018 17:24
Show Gist options
  • Save pragmatictesters/a3089a76410470a8714c3d844440e0e7 to your computer and use it in GitHub Desktop.
Save pragmatictesters/a3089a76410470a8714c3d844440e0e7 to your computer and use it in GitHub Desktop.
Selenium WebDriver Example Working with Javascript popups
package com.pragmatic.selenium.examples;
import io.github.bonigarcia.wdm.WebDriverManager;
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.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* <b>Selenium WebDriver Examples</b>
*<br>
* Working with <i>JavaScript popups</i>
* <p>
* This class demonstrate working with Javascript popup windows such as <i>Alert, Confirmation and Prompt</i>
* </p>
*
* @see <a href="http://pragmatictestlabs.com/2018/02/12/working-javascript-popup-boxes-selenium-webdriver/">PTL Blog Post on WORKING WITH JAVASCRIPT POPUP BOXES IN SELENIUM WEBDRIVER</a>
*
*
**/
public class JavaScriptPopups {
private static final String BASE_URL = "http://demosite.pragmatictestlabs.com/Alerts.html";
private WebDriver webDriver;
/**
* Download (if required) required web browser driver and configure automatically using WebDriverManager
*/
@BeforeClass
public void beforeClass() {
WebDriverManager.chromedriver().setup();
}
/**
* Create a browser instance and navigate to the BASE_URL
*/
@BeforeMethod
public void beforeMethod() {
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
webDriver = new ChromeDriver(options);
webDriver.manage().window().maximize();
webDriver.navigate().to(BASE_URL);
}
/**
* Closes the browser instance
*/
@AfterMethod
public void afterMethod() {
waitFor(2);
webDriver.close();
}
/**
* Demonstrate working with a simple alert
*/
@Test
public void testAlert() {
//Click on the Simple Alert button
webDriver.findElement(By.xpath("//button[contains(text(),'Simple Alert')]")).click();
//Switch to the alert. This is compulsory before working with any JavaScript popup
Alert alert = webDriver.switchTo().alert();
//Verify the message in the popup window
Assert.assertEquals(alert.getText(), "This Is Simple Alert");
//Clicking the button
waitFor(2);
alert.accept();
//Verify the message in the web page
Assert.assertEquals(webDriver.findElement(By.id("confirm-demo")).getText(), "Alert is gone.");
}
/**
* Demonstrate working with a JavaScript confirmation
*/
@Test
public void testConfirmationClickingOKButton() {
webDriver.findElement(By.xpath("//button[contains(text(),'Confirm Alert')]")).click();
//Switch to the alert
Alert alert = webDriver.switchTo().alert();
//Verify the message in the popup window
Assert.assertEquals(alert.getText(), "Press a button!");
//Clicking the OK button
waitFor(2);
alert.accept();
//Verify the message in the web page
Assert.assertEquals(webDriver.findElement(By.id("confirm-demo")).getText(), "Confirmed.");
}
/**
* Demonstrate working with a JavaScript confirmation, clicking Cancel button
*/
@Test
public void testConfirmationClickingCancelCancel() {
//Click the Confirm Alert button
webDriver.findElement(By.xpath("//button[contains(text(),'Confirm Alert')]")).click();
//Switch to the alert
Alert alert = webDriver.switchTo().alert();
//Verify the message in the popup window
Assert.assertEquals(alert.getText(), "Press a button!");
//Clicking the NO/Cancel button
waitFor(2);
alert.dismiss();
//Verify the message in the web page
Assert.assertEquals(webDriver.findElement(By.id("confirm-demo")).getText(), "Rejected!");
}
/**
* Demonstrate working with a JavaScript prompt, Input text into the input box and clicking OK
*/
@Test
public void testPromptInputText() {
//Text to be typed into the input box
String strInputText = "Selenium";
webDriver.findElement(By.xpath("//button[contains(text(),'Prompt Alert')]")).click();
//Verify the message in the popup window
//Switch to the alert
Alert alert = webDriver.switchTo().alert();
alert.sendKeys(strInputText);
//Clicking
waitFor(3);
alert.accept();
//Verify the message in the web page
Assert.assertEquals(webDriver.findElement(By.id("confirm-demo")).getText(), strInputText);
}
/**
* Demonstrate use of Explicit wait. Waiting for an Alert to be present
*/
@Test
public void testTimingAlert() {
webDriver.findElement(By.xpath("//button[contains(text(),'Timing Alert')]")).click();
//Explicit wait configuration
WebDriverWait wait = new WebDriverWait(webDriver, 30);
//Wait till an alert is present
wait.until(ExpectedConditions.alertIsPresent());
//Switch to the alert
Alert alert = webDriver.switchTo().alert();
//Verify the message in the popup window
Assert.assertEquals(alert.getText(), "This is Timing Alert");
//Clicking the button
waitFor(2);
alert.accept();
//Verify the message in the web page
Assert.assertEquals(webDriver.findElement(By.id("confirm-demo")).getText(), "0");
}
private void waitFor(int seconds) {
try {
Thread.sleep(seconds*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment