Using the PageFactory class for exposing an operation on a page

In the previous recipe, we created the BmiCalcPage class, which provides elements from the BMI Calculator page to the test. Along with elements, we define operations or behaviors on a page. In the BMI Calculator application, we are calculating the BMI by entering height and weight values. We can create an operation named calculateBmi and call it directly in a test, instead of calling individual elements and operations.

In this recipe, let's refine the BmiCalcPage class. And instead of elements, let's provide the operations that are supported on the page, and some common properties. We will also move the WebDriver instance of the test to the BmiCalcPage class to make the test generic.

Getting ready

Identify operations that will be required in a test and can be exposed from a page. This recipe uses the BmiCalcPage class created in the previous recipe.

How to do it...

Let's modify the BmiCalcPage class created in the previous recipe and refactor it a bit to provide operations and properties to the test through the following steps:

  1. Make the page's elements private in the BmiCalPage class for better encapsulation. Also, add an instance variable of the WebDriver class and a string variable for the URL of the page, as follows:
    package com.secookbook.examples.chapter08.tests;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.PageFactory;
    
    public class BmiCalcPage {
    
        private WebElement heightCMS;
        private WebElement weightKg;
        private WebElement Calculate;
        private WebElement bmi;
        private WebElement bmi_category;
        private WebDriver driver;
        private String url = "http://dl.dropbox.com/u/55228056/bmicalculator.html";
    }
  2. Update the BmiCalcPage class constructor so that it initializes the WebDriver instance, as follows:
    public BmiCalcPage() {
        driver = new ChromeDriver();
        PageFactory.initElements(driver, this);
    }
  3. Add the load() and close methods to the BmiCalcPage class, as follows:
    public void load() {
        this.driver.get(url);
    }
    public void close() {
        this.driver.close();
    }
  4. Add the calculateBmi() method to the BmiCalcPage class, as follows:
    public void calculateBmi(String height, String weight) {
        heightCMS.sendKeys(height);
        weightKg.sendKeys(weight);
        Calculate.click();
    }
  5. Add the getBmi and getBmiCategory() methods to the BmiCalcPage class, as follows:
    public String getBmi() {
        return bmi.getAttribute("value");
    }
    
    public String getBmiCategory() {
        return bmi_category.getAttribute("value");
    }
  6. Finally, create a test that will use the methods defined in the BmiCalcPage class to test the BMI Calculator page, as follows:
    package com.secookbook.examples.chapter08.tests;
    
    import org.junit.Test;
    
    import static org.junit.Assert.*;
    import seleniumcookbook.tests.pageobjects.*;
    
    public class BmiCalculatorTests {
    
    @Test
    public void testBmiCalculation()
    {
      //Create an instance of Bmi Calculator Page class
      //and provide the driver
      BmiCalcPage bmiCalcPage = new BmiCalcPage();
    
      //Open the Bmi Calculator Page
      bmiCalcPage.load();
    
      //Calculate the Bmi by supplying Height and Weight values
      bmiCalcPage.calculateBmi("181", "80");
    
      //Verify Bmi & Bmi Category values
      assertEquals("24.4", bmiCalcPage.getBmi());
      assertEquals("Normal", bmiCalcPage.getBmiCategory());
    
      //Close the Bmi Calculator Page
      bmiCalcPage.close();
    }

How it works...

In this example, the BmiCalcPage class defines various methods for loading, closing the page, calculation functionality, and providing access to elements as properties to the test. This simplifies the test development by creating a layer of abstraction, hiding the internals of a page, and exposing only the operations and fields needed for testing from the page.

When any change happens to the structure or behavior of the page, only the BmiCalcPage class will be refactored, while the test will remain intact.

In this example, we created the load() method, which navigates the application using the URL, as follows:

public void load() {
    this.driver.get(url);
}

Using this approach, we can also expose the elements as properties, which provide specific attributes, such as the value attribute, instead of exposing elements fully. For example, the getBmi() method provides only the value attribute of the bmi label to the test, as follows:

public String getBmi() {
    return bmi.getAttribute("value");
}

Selenium WebDriver provides a very neat and clean way to implement 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
3.145.191.134