We can implement the objects of the Page Object model using the LoadableComponent
class of Selenium WebDriver. This helps in building a robust Page Object that provides a standard way to ensure that the page is loaded and that the page load issues are easy to debug.
In this recipe, we will further refactor the BmiCalcPage
class created in the previous recipes and extend it as a loadable component.
To implement an object of the Page Object model as the LoadableComponent
class, we need to extend it from the LoadableComponent
base class, as shown in the following example:
package com.secookbook.examples.chapter08.pageobjects; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.ui.LoadableComponent; import static org.junit.Assert.*; public class BmiCalcPage extends LoadableComponent<BmiCalcPage> { private WebElement heightCMS; private WebElement weightKg; private WebElement calculate; private WebElement bmi; private WebElement bmiCategory; private WebDriver driver; private String url = "http://cookbook.seleniumacademy.com/bmicalculator.html"; private String title = "BMI Calculator"; public BmiCalcPage(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } @Override protected void load() { this.driver.get(url); } @Override protected void isLoaded() throws Error { assertTrue("Bmi Calculator page not loaded", driver.getTitle().equals(title)); } public void calculateBmi(String height, String weight) { heightCMS.sendKeys(height); weightKg.sendKeys(weight); calculate.click(); } public String getBmi() { return bmi.getAttribute("value"); } public String getBmiCategory() { return bmiCategory.getAttribute("value"); } }
Finally, in the test, change the call of the bmiCalcPage.load()
method to the bmiCalcPage.get()
method, as follows:
package com.secookbook.examples.chapter08.tests; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import static org.junit.Assert.*; import com.secookbook.examples.chapter08.pageobjects.BmiCalcPage; public class BmiCalculatorTests { private WebDriver driver; @Before public void setUp() { driver = new ChromeDriver(); } @Test public void testBmiCalculation() { // Create an instance of Bmi Calculator Page class // and provide the driver BmiCalcPage bmiCalcPage = new BmiCalcPage(driver); // Open the Bmi Calculator Page bmiCalcPage.get(); // 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()); } @After public void tearDown() { driver.quit(); } }
By extending the object of the Page Object model with the LoadableComponent
base class, we overrode the load()
and isLoaded()
methods to the BmiCalcPage
class.
The load()
method will load the URL of the page we encapsulated in the Page Object, and when we create the instance of this Page Object in a test, we call the get()
method on the BmiCalcPage
class, which will in turn call the load()
method, as follows:
BmiCalcPage bmiCalcPage = new BmiCalcPage(driver); bmiCalcPage.get();
The isLoaded()
method will verify that the indented page is loaded by the load()
method.
18.222.32.67