Related to Relative XPaths

As is usually the case, there are many different ways to do this effectively. I can show you how I’ve organized my POM in regards to how to store/access/utilize xpaths.

Lets pretend we want to create a page object for the login page of an application:

public class LoginPage {
    
    public void setUsernameField(String value) {
        // Code to set field...
    }

    public void setPasswordField(String value) {
        // Code to set field...
    }

    public void clickLoginButton() {
        // Code to click button...
    }

}

You would do this in Katalon by creating a Custom Keyword. Custom Keywords are just Katalon’s way of giving you a space to implement your own package/class hierarchy to be used by your scripts.

There are a couple of different ways you could store your xpaths for use in your page objects:

1.) Use the Object Repository

If you decide to stick with using Katalon’s object repository, that’s perfectly fine. You would store your xpaths in Test Objects, and call them in your page objects as follows (let’s use the clickLoginButton() method from above as an example):

Example test object for login button:

Example code in page object:

public void clickLoginButton() {
    WebUI.click(findTestObject('path/to/test/object'));
}

2.) Make the Page Objects the owners of your locators

This is my preferred method of doing things, mainly because I don’t have to look in two different places, and also because you can more easily do really complex things like parameterize xpaths, nest them, compile them together with xpaths from parent classes, etc. Also, if you decide to copy/paste this page object into another tool (blasphemous, I know), the migration is much easier than if you had used the Object Repository:

public class LoginPage {

    private final WebDriver driver = DriverFactory.getWebDriver();
        
    public void setUsernameField(String value) {
        // Code to set field...
    }

    public void setPasswordField(String value) {
        // Code to set field...
    }

    public void clickLoginButton() {
        WebElement button = driver.findElement(By.xpath(getLoginButtonXPath()));
        button.click();
    }

    // XPATHS:
    public String getLoginButtonXPath() {
        return "//button[@id='login_button']";
    }

}

Hopefully this gives you something to work from :smiley:

2 Likes