Managing Alerts in an Automation Script

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:

  1. Review and analyze the structure of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_2/exercise02_concept_03.html file.
  2. 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.
  1. 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.
  1.  For the simple alert modal dialog, create a method and name it alertsChecks:
public static void main(String[] args) {
alertsChecks();
}
  1. 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());
  1. 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"))
  1. 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");
}
  1. Accept the alert:
simpleAlert.accept();

  1. 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();
  1. Either accept or dismiss the alert:
if (confirmAlert.getText().equalsIgnoreCase("This is a
confirm alert. Do you want to accept or cancel?"))
  1. 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");
}
  1. Accept the alert:
confirmAlert.accept();
  1. 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();
  1. Populate the input field with your desired answer:
promptAlert.sendKeys("Java");
  1. Accept the alert:
promptAlert.accept();
  1. Verify that the title of the page includes the text while populating the input field:
if (driver.getTitle().contains("Java"))
  1. 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");
}
  1. Compile and execute the script.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.138.223