Creating an Automation Script for Age Calculator

The aim here is to create an automation script that uses the AgeCalculator Page Object that was created in the previous section. Consider the Age Calculator application. The Age Calculator is a single-page application that takes the user's date of birth and calculates their age and zodiac sign:

<html lang="en">
<head>
<title>Lesson 6 - Age Calculator</title>
</head>

<body class="bg-light">
<div class="container">
<div class="py-5 text-center">
<h2>Lesson 6 - Age Calculator</h2>
</div>

<div class="row">
<div class="col-md-4 order-md-2 mb-4">
<h4 class="d-flex justify-content-between align-
items-center mb-3">
<span class="text-muted">Your data</span>
</h4>
<ul class="list-group mb-3">
<li class="list-group-item d-flex justify-
content-between lh-condensed">
<div>
<h6 class="my-0">Age</h6>
</div>
<span class="text-muted" id="age"></span>
</li>
<li class="list-group-item d-flex justify-content-
between lh-condensed">
<div>
<h6 class="my-0">Zodiac Sign</h6>
</div>
<span class="text-muted"
id="zodiacSign"></span>
</li>
</ul>
</div>
<div class="col-md-8 order-md-1">
<h4 class="mb-3">Enter your information</h4>
<form>
<div class="row">
//[…]

The steps of achieving our aim are as follows:

  1. Review and analyze the DOM of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_6/exercise_6_1.html file.
  2. Review the Page Object of the Age Calculator application, as defined in the https://github.com/TrainingByPackt/Beginning-Selenium/blob/master/docs/lesson_6/lesson6/src/main/java/com/beginningselenium/examples/pageobjects/AgeCalculatorPage.java file.
  3. Create a new Java file for the automation script using IntelliJ IDEA. Make sure to include the following:
    • The name of the package.
    • The libraries required for the Page Object to work.
    • A class method to encapsulate the code of the script.
package com.beginningselenium.examples.scripts;

import com.beginningselenium.examples.pageobjects.
AgeCalculatorPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AgeCalculatorScript {

public static void main(String[] args) {
checkAgeCalculator();
}
  1. Using the Calculate method of the AgeCalculatorPage, provide input values to execute the test. Make sure to include the expected results, to compare them with the results provided by the application.
private static void checkAgeCalculator() {
WebDriver driver = new ChromeDriver();
// Create an instance of AgeCalculatorPage class
and open it
AgeCalculatorPage ageCalculatorPage = new
AgeCalculatorPage(driver);
ageCalculatorPage.open();
// Start the test by means of the calculate
method
ageCalculatorPage.calculate("11", "February",
"1982");
// Verify results
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");
}
ageCalculatorPage.close();
}
}
  1. Compile and run 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.223.32.230