PO Tutorial Lesson 3: Inheritance

270

Inheriting a Base Page Object Class


Inheritance is a concept in programming in which a base class is written with some functionality so other functions can extend from it, inheriting the functionality it provides. This is very much inline with the goal of the Page Object model in testing as it allows us to reuse code on an even lower level, saving yet more time and effort.

By putting such methods in an inherited class, any future Page Objects we create that inherit from this class will be able to use them too.


BasePage.java

public class BasePage {
    protected static RemoteWebDriver driver;

    public BasePage(RemoteWebDriver driver) {
        this.driver = driver;
    }

    public WebElement getElement(By elementLocator) {
        return driver.findElement(elementLocator);
    }

    public String getTitle() {
        return driver.getTitle();
    }
}

Note: the protected keyword when used to create an object, means that any class that inherits from BasePage can also inherit this object


HomePage.java

public class HomePage extends BasePage {
    By searchLocatorXPath = By.xpath("//input[@id='searchInput']");

    public HomePage(RemoteWebDriver driver) {
        super(driver);
    }

    public CountryPage searchCountry(String countryName) {
        getElement(searchLocatorXPath).sendKeys(countryName + Keys.ENTER);
        return new CountryPage(driver);
    }
}

Note: by using the method super, we are telling the HomePage class to use inherited objects in the constructor.


CountryPage.java

public class CountryPage extends BasePage {
    public CountryPage(RemoteWebDriver driver) {
        super(driver);
    }
}

The following example shows a test that uses these inherited based page object classes to condense code.


Example


As you can see in this example, despite the getElement(...) and getTitle() methods being defined only in the BasePage class, both HomePage and CountryPage objects can use them.


Inheriting a Base Test Class


I also recommend using inheritance when writing tests for the same reasons. Often you will find yourself writing out the same few lines of code again and again. Using inheritance will save you a lot of time if you put this repeated functionality into a base class that other tests can inherit from.

The following example shows a test that inherits from a BaseTest class containing the @Before and @After methods that are usually written in test cases.


Example


The following code is contained within the BaseTest class.


BaseTest.java

public class BaseTest {
    protected static ApiService mxService = new ApiService();
    protected static RemoteWebDriver driver;

    @Before
    public void setUp() {
        MutableCapabilities caps = mxService.getBaseCaps();
        driver = new RemoteWebDriver(mxService.getURL(), caps);
        mxService.processSession(driver);
        mxService.waitForLiveVideo(); // Comment out this line to avoid waiting for live video (minimum 10 seconds)
    }

    @After
    public void tearDown() {
        if (driver != null)
            driver.quit();
    }
}

This saves time by reducing the amount of code written, not only in the volume of code within the methods themselves, but also by removing the need to import the classes required to make it work.

In the next lesson we will cover ways to better model a user’s journey through a website in your test.