Here, we'll be creating an automation script that manages alerts. We'll create three different test classes, to test different modal dialogs.
The steps for completion of this process are as follows:
- Review and analyze the structure of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_2/exercise02_concept_03.html file.
- Open the https://trainingbypackt.github.io/Beginning-Selenium/lesson_2/exercise02_concept_03.html page; navigate around the page and make sure that you understand it, so that you can analyze its behavior.
- Create a new Java file for the automation script using IntelliJ IDEA. Make sure that you include the required libraries for the script to work.
- For the simple alert modal dialog, create a method and name it alertsChecks:
public static void main(String[] args) {
alertsChecks();
}
- Inside the alertsChecks method, create a variable of type Alert, and use the driver.switchTo().alert method to assign it to the modal dialog (as highlighted in the following code snippet):
private static void alertsChecks(){
WebDriver driver = new ChromeDriver();
try {
driver.get("https://trainingbypackt.github.io/
Beginning-Selenium/lesson_2/exercise02_concept_03.html");
// Accepting the first alert
Alert simpleAlert = driver.switchTo().alert();
System.out.println("Alert text contents: " + simpleAlert.
getText());
- Verify that the message on the modal dialog is:
"Welcome! This is a simple alert. Press 'Accept' to continue"
:
if (simpleAlert.getText().equalsIgnoreCase("Welcome! This
is a simple alert. Press 'Accept' to continue"))
- Using the System.out.println method, display a message that indicates whether the verification was successful:
{
System.out.println("It worked, the expected
simple alert was shown");
}
else {
System.out.println("Something went wrong,
the expected simple alert was NOT shown");
}
- Accept the alert:
simpleAlert.accept();
- Now, create a variable of type Alert, and use the driver.switchTo().alert method to assign it to the modal dialog:
Alert confirmAlert = driver.switchTo().alert();
- Either accept or dismiss the alert:
if (confirmAlert.getText().equalsIgnoreCase("This is a
confirm alert. Do you want to accept or cancel?"))
- Using the System.out.println method, display a message that indicates whether the alert was accepted or dismissed.
{
System.out.println("It worked, the expected
confirmation alert was shown");
}
else {
System.out.println("Something went wrong,
the expected confirmation alert was NOT shown");
}
- Accept the alert:
confirmAlert.accept();
- Now, create a variable of type Alert, and use the driver.switchTo().alert() method to assign it to the modal dialog:
Alert promptAlert = driver.switchTo().alert();
- Populate the input field with your desired answer:
promptAlert.sendKeys("Java");
- Accept the alert:
promptAlert.accept();
- Verify that the title of the page includes the text while populating the input field:
if (driver.getTitle().contains("Java"))
- Using the System.out.println method, display a message that indicates whether the text was included:
System.out.println("It worked, the expected prompt alert
was shown and the text was included in the title.");
} else {
System.out.println("Something went wrong,
the expected prompt alert did not work");
}
- Compile and execute the script.