PO Tutorial Lesson 2: The ingredients of a Page Object Class

269

The Ingredients of a Page Object Class

In Selenium testing, locators allow you to find elements on a page, and web elements are objects you use to interact with these elements when they are found.

Try it yourself [Lesson 2 example: same as lesson 1 except there are 2 locators that the user can alternate between to show XPath and CSS selectors are different approaches to the problem]

Locators are what the driver uses to locate different elements on the page before representing them as web elements for your program to interact with.

HomePage.java

    By searchLocator = By.xpath("//input[@id='searchInput']");

Here is a lesson about locators from our beginners guide.

HomePage.java

In our page object classes we store the locators we need outside the methods that use them. This allows for quicker coding and easier maintenance as you only need to point the related methods to a single locator. This means if the webpage changes in the future you only need to change one line of code as opposed to all references to that locator.

HomePage.java

    public void searchForCountryName(String country) {
        WebElement searchBar = driver.findElement(searchLocatorCSS);
        searchBar.sendKeys(country + Keys.RETURN);
    }

In the next lesson we will cover using inheritance to save even more time writing tests.