Activity: Implementing the POM on a Multi-Page Application

  1. 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):
    1. Create a new Java file by using your IDE of choice.
    2. 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;
    1. Include all of the required libraries:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
    1. 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 { }
    1. 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();
}
    1. Write a constructor to the HomePage class:
public HomerPage(WebDriver webDriver) {
driver = webDriver;
}
    1. 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();
}
    1. 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);
}
  1. Create a Page Object for the page that was chosen in step 1.7, as follows:
    1. Create a new Java file by using your IDE of choice.
    2. Include this Page Object in the same Object Repository that includes the HomePage class:
package com.beginningselenium.examples.pageobjects;
    1. Include all of the required libraries:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
    1. 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);
}
    1. 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;
    1. 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;
    1. Write an operation to read the value of the quote element:
public String getQuote() {
quote = driver.findElement(By.id("quote"));
return quote.getText();
}
  1. 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:
    1. Define a package for the test:
package com.beginningselenium.examples.scripts;
    1. 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;

    1. 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(){
}
    1. Always create a method where the script's code can be placed:
public void checkHomeAndDealsPage(){
}
    1. 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();
    1. Start the test by invoking the clickDeals method of the HomePage class. This method returns a DealPage class object:
DealsPage dealspage = homePage.clickDeals();
    1. 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!");
}
    1. Stop the test:
dealsPage.close(); 
  1. Compile and run the test.

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

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