- Create a Page Object for the https://trainingbypackt.github.io/Beginning-Selenium/lesson_6/exercise_6_1.html file (which is considered the home page of the Gigantic Store application):
- Create a new Java file by using your IDE of choice.
- Define a package for our Object Repository and include it at the top of every Page Object file that you create:
package com.beginningselenium.examples.pageobjects;
-
- Include all of the required libraries:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
-
- Since we are creating a Page Object for the Home page, it would be a good practice to name our class HomePage:
public class HomePage { }
-
- Set variables and methods to handle the driver:
private WebDriver driver;
private WebElement linkText;
private String url = "https://trainingbypackt.github.io/Beginning-Selenium/lesson_6/activity_6_B-1.html";
public void open() {
this.driver.get(url);
}
public void close() {
this.driver.quit();
}
-
- Write a constructor to the HomePage class:
public HomerPage(WebDriver webDriver) {
driver = webDriver;
}
-
- Write an operation to click on any of the options of the menu:
private void clickOption(String option){
linkText = driver.findElement(By.linkText(option));
linkText.click();
}
-
- Create an operation that uses the clickOption from any other page of the application. For this activity, we will choose the https://trainingbypackt.github.io/Beginning-Selenium/lesson_6/activity_6_B-1/deals.html page.
public DealsPage clickDeals(){
this.clickOption("Deals");
return new DealsPage(driver);
}
- Create a Page Object for the page that was chosen in step 1.7, as follows:
- Create a new Java file by using your IDE of choice.
- Include this Page Object in the same Object Repository that includes the HomePage class:
package com.beginningselenium.examples.pageobjects;
-
- Include all of the required libraries:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
-
- Since we are creating a Page Object for the Deals page, it would be a good practice to name our class DealsPage, as follows:
public class DealsPage {
}
//Set variables and methods to handle the driver:
private WebDriver driver;
private String url = "https://trainingbypackt.github.io/Beginning-Selenium/lesson_6/activity_6_B-1/deals. html";
public void open() {
this.driver.get(url);
}
-
- Set the variables of the required web elements. The Deals page includes an element with the ID quote, with specific text, as follows:
private WebElement quote;
-
- Write a constructor to the DealsPage class that verifies that the Deals page loads and initializes the quote element:
public DealsPage(){
driver = new ChromeDriver();
if (!"Deals".equalsIgnoreCase(this.driver.getTitle())){
this.driver.open(url);
}
}private WebElement quote;
-
- Write an operation to read the value of the quote element:
public String getQuote() {
quote = driver.findElement(By.id("quote"));
return quote.getText();
}
- Create an automation script for the HomePage class. The test should involve clicking on the Deals option of the menu and verifying that the Deals page loads:
- Define a package for the test:
package com.beginningselenium.examples.scripts;
-
- Import the Object Repository of the Page Objects, and all other required libraries:
import com.beginningselenium.examples.pageobjects.DealsPage;
import com.beginningselenium.examples.pageobjects.HomePage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
-
- Since we are creating a script for the Age Calculator application, it would be a good practice to name our class HomeScript:
public void HomeScript(){
}
-
- Always create a method where the script's code can be placed:
public void checkHomeAndDealsPage(){
}
-
- To use the Page Object, we must create an instance of the HomePage class, and then start the driver:
WebDriver driver = new ChromeDriver();
HomePage homePage = new HomePage(driver);
homePage.open();
-
- Start the test by invoking the clickDeals method of the HomePage class. This method returns a DealPage class object:
DealsPage dealspage = homePage.clickDeals();
-
- Verify the test:
String quote = "We have cheap stuff that will break in a few days";
if (quote.equalsIgnoreCase(dealsPage.getQuote())) {
System.out.println("Quote is correct!");
} else {
System.out.println("Quote is NOT correct!");
}
-
- Stop the test:
dealsPage.close();
- Compile and run the test.