Activity: Implementing Nested Page Object Instances

  1. Analyze the DOM and layout of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_6/activity_6_B-1.html file.
  2. Create a Page Object for the Header section:
package com.beginningselenium.examples.pageobjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;

public class HeaderPage {

//WebElements
private WebElement home;

///WebDriver
private WebDriver driver;

//Class Constructor
public HeaderPage(WebDriver driver) {
this.driver = driver; }

// Methods to navigate the header
public HomePage goToHome(){

home = driver.findElement(By.id("home"));
home.click();
return new HomePage();
}
}
  1. Create a Page Object for the Menu section:
package com.beginningselenium.examples.pageobjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class MenuPage {

//WebElements
private WebElement products;

///WebDriver
private WebDriver driver;

//Class Constructor
public MenuPage(WebDriver driver) {
this.driver = driver; }

// Methods to navigate the menu
public ProductsPage goToProducts(){
products = driver.findElement(By.id("products"));
products.click();
return new ProductsPage(this.driver);
}
}
  1. Create a Page Object for the Banner section:
package com.beginningselenium.examples.pageobjects; 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class BannerPage {

//WebElements
private WebElement mainInfo;
private WebElement secondaryInfo;

///WebDriver
private WebDriver driver;

//Class Constructor
public MenuPage(WebDriver driver) {
this.driver = driver;
}

// Methods to read info of the banner
public String getMainInfo(){
mainInfo = driver.findElement(By.id("mainInfo"));
return mainInfo.getText();
}
public String getSecondaryInfo(){
secondaryInfo = driver.findElement(By.id("secondaryInfo"));
return secondaryInfo.getText();
}
}
  1. Create a Page Object for the HomePage:
package com.beginningselenium.examples.pageobjects; 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;

public class HomePage {

//WebElements
private MenuPage menu;
private BannerPage banner;

///WebDriver
private WebDriver driver;
private String url = "https://trainingbypackt.github.io/Beginning-Selenium/lesson_6/activity_6_B-1.html";

//Class Constructor
public HomePage(WebDriver driver) {
this.driver = driver;
}

//Methods to open and close the WebDriver
public void open() {
this.driver.get(url);
}
public void close() {
this.driver.quit();
}

// Methods to navigate the header
public ProductsPage goToProducts(){
menu = new MenuPage(driver);
return menu.goToProducts();
}

// Methods to read the banner
public String getMainInfo(){
return new BannerPage(this.driver).getMainInfo();
}
public String getSecondaryInfo(){
return new BannerPage(this.driver).getSecondaryInfo();
}
}
  1. Create a Page Object for the Products page:
package com.beginningselenium.examples.pageobjects; 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class ProductsPage {

//WebElements
private HeaderPage header;
private BannerPage banner;

///WebDriver
private WebDriver driver;
private String url = "https://trainingbypackt.github.io/Beginning-Selenium/lesson_6/activity_6_B-1.html";

//Class Constructor
public ProductsPage(WebDriver driver) {
this.driver = driver;
}

//Methods to open and close the WebDriver
public void open() {
this.driver.get(url);
}
public void close() {
this.driver.quit();
}

// Methods to navigate the header
public HomePage goToHome(){
header = new HeaderPage(this.driver);
return header.goToHome();
}

// Methods to read the banner
public String getMainInfo(){
return new BannerPage(this.driver).getMainInfo();
}
public String getSecondaryInfo(){
return new BannerPage(this.driver).getSecondaryInfo();
}
}
  1. Write an automation script that uses the Page Objects that were created:
package com.beginningselenium.examples.scripts; 

import com.beginningselenium.examples.pageobjects.HomePage;
import com.beginningselenium.examples.pageobjects.ProductsPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GiganticStoreScript {

public void checkGiganticStore() {
WebDriver driver = new ChromeDriver();

HomePage homePage = new HomePage(driver);
homePage.open();
ProductsPage productsPage = homePage.goToProducts();

// Verify results in products page
String productsMainInfo = "Here's the stuff we sell";
if (productsMainInfo.equalsIgnoreCase(productsPage.getMainInfo())) {
System.out.println("Products main info is correct!");
} else {
System.out.println("Products main info is NOT correct!");
}

String productsSecondaryInfo = "We have everything... everything";
if (productsSecondaryInfo.equalsIgnoreCase(productsPage.getSecondaryInfo())) {
System.out.println("Products secondary info is correct!");
} else {
System.out.println("Products secondary info is NOT correct!");
}

// Go back home
homePage = productsPage.goToHome();

// Verify results in home page
String homePageMainInfo = "Our best selling product";
if (homePageMainInfo.equalsIgnoreCase(homePage. getMainInfo())) {
System.out.println("HomePage main info is correct!");
} else {
System.out.println("HomePage main info is NOT correct!");
}

String homePageSecondaryInfo = "Buy it before it's too late";
if (homePageSecondaryInfo.equalsIgnoreCase(homePage.getSecondaryInfo())) {
System.out.println("HomePage secondary info is correct!");
} else {
System.out.println("HomePage secondary info is NOT correct!");
}

// Close the page
homePage.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
13.58.112.1