Creating an Automation Script

In the previous section, we created the AgeCalculatorPage Page Object that exposes the elements of the web page for the AgeCalculator application. For each test scenario that requires the use of AgeCalculatorPage, we can create an automation script in a different Java file. From each script, we can access the web page's elements, through the AgeCalculatorPage Page Object. When doing so, we should keep the following in mind:

  1. We should define a package for all of our tests and include it at the top of every script that we create, as follows:
package com.beginningselenium.examples.scripts;
  1. We should import the package that our object repository belongs to (and all other required libraries), as follows:
import com.beginningselenium.examples.pageobjects.
AgeCalculatorPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
  1. Since we are creating a script for the AgeCalculator web page, it would be a good practice to name our class AgeCalculatorScript:
public class AgeCalculatorScript {
}
  1. We should always create a method where the script's code can be placed:
public checkAgeCalculator() {
}
  1. To use the Page Object, we must create an instance of the AgeCalculatorPage class, and then start the driver:
WebDriver driver = new ChromeDriver();
AgeCalculatorPage ageCalculatorPage = new
AgeCalculatorPage(driver);
ageCalculatorPage.open();
  1. We can execute the test by using the calculate method of the AgeCalculatorPage:
ageCalculatorPage.calculate("11", "February", "1982");
  1. We should verify that the values of age and zodiac sign provided by the application correspond to the expected values. In our example, we mentioned February 11, 1982, which means that the user is 36 years old (the current year, as of writing this chapter, is 2018) and belongs to the Aquarius zodiac sign:
if (ageCalculatorPage.getAge().equals("36")) {
System.out.println("Age was calculated correctly!");
} else {
System.out.println("There was an error in the age
calculation");
}

if (ageCalculatorPage.getZodiacSign().equals("Aquarius")) {
System.out.println("Zodiac sign was calculated
correctly!");
} else {
System.out.println("There was an error in the zodiac sign
calculation");
}
  1. We can stop the test as follows:
ageCalculatorPage.close();

You should now understand how to implement an automation script with the Page Object Model.

..................Content has been hidden....................

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