# Related to Relative XPaths

**URL:** <https://forum.katalon.com/t/related-to-relative-xpaths/23516>\
**Category:** Web Testing\
**Tags:** web-testing, katalon-studio\
**Created:** [April 19, 2019, 2:25pm UTC](https://forum.katalon.com/t/related-to-relative-xpaths/23516 "2019-04-19T14:25:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asaikumar2007](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@asaikumar2007](https://forum.katalon.com/u/asaikumar2007)\
**Post date:** [April 19, 2019, 2:25pm UTC](https://forum.katalon.com/t/related-to-relative-xpaths/23516/1 "2019-04-19T14:25:29Z")

</div>

I am able to write Xpaths using WebDriver instance in Katalon Test Case.

But i am not sure how to write POM model in Katalon.  
Because i want to keep all my Xpaths written in one File and want to call from another File.

Could you please help me how to do it.

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [April 19, 2019, 4:28pm UTC](https://forum.katalon.com/t/related-to-relative-xpaths/23516/2 "2019-04-19T16:28:38Z")

</div>

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.

> [@asaikumar2007](#):
>
> But i am not sure how to write POM model in Katalon.

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](https://docs.katalon.com/katalon-studio/docs/introduction-to-custom-keywords.html). 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.

> [@asaikumar2007](#):
>
> Because i want to keep all my Xpaths written in one File and want to call from another File.

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:

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/7/7a6c27f43d5d285074f919a01563d37c14c9b070.png)

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 😃

---

<div class="post-metadata">

**Author:** ![devalex88](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/devalex88/32/336_2.png) [@devalex88](https://forum.katalon.com/u/devalex88)\
**Post date:** [April 20, 2019, 3:48pm UTC](https://forum.katalon.com/t/related-to-relative-xpaths/23516/3 "2019-04-20T15:48:04Z")

</div>

Perfect answer @Brandon_Hein.

@asaikumar2007 There are some samples here

> [@Katalon Studio vs Selenium what should I learn](http://forum.katalon.com/t/katalon-studio-vs-selenium-what-should-i-learn/17652):
>
> I had seen this FAQ “Katalon Studio vs Selenium” from beginners wondering what to learn first, so I’ve created a sample project to show how to write vanilla Selenium code in Katalon Studio. Selenium is one of the building blocks of Katalon Studio, so while we provide convenient features like Test Case / Test Suite management, Test Object, built-in WebDriver, Custom Keywords, integration, etc., we do not prevent users from writing vanilla Selenium code. You can even start learning Selenium usi…

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [April 20, 2019, 4:13pm UTC](https://forum.katalon.com/t/related-to-relative-xpaths/23516/4 "2019-04-20T16:13:32Z")

</div>

> [@Brandon\_Hein](#):
>
> blasphemous, I know

Blasphemy? I’m agnostic, thank God.

Great post, Brandon.

---

<div class="post-metadata">

**Author:** ![albert.vu](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/albert.vu/32/20595_2.png) [@albert.vu](https://forum.katalon.com/u/albert.vu)\
**Post date:** [August 8, 2022, 7:49am UTC](https://forum.katalon.com/t/related-to-relative-xpaths/23516/5 "2022-08-08T07:49:27Z")

</div>

Archived on August 8, 2022, 7:49am UTC


