0

I'm trying to use Page Objects pattern in my UI testing. Many examples suppose to save By (Locator) in class fields. Other recommends to save WebElement (or SelenideElement, if you're using Selenide). Though, both are great for hardcoded locators, I don't see how to use this for locators where the path contains variables.

For example, how to save in class field this locator?

public SelenideElement getTotal(String type) {
   return $(By.xpath("//h4[contains(text(), '"+ type +"')]");
}
Kyryl Havrylenko
  • 674
  • 4
  • 11

2 Answers2

1

Your solution is the right one in my opinion.

I usually put them in the top of my PageObject next to the other selectors like what you've done. Just use the method as you would use one of your SelenideElement fields. Something like:

private SelenideElement getTotalElementByType(String type) {
    return $(By.xpath("//h4[contains(text(), '"+ type +"')]");
}

I'd make it private or protected though, because with the Page Object Pattern your test scripts shouldn't know about the WebElements on a page object.

Your publicly accessible method would be something like this:

public int getTotalByType(String type) {
    string totalString = getTotalElementByType(type).getText();
    int total = //convert to int or whatever
    return total;
}

If you wanted to interact with the element instead of getting values, you would return the PageObject you expect to go to instead to follow the POPattern. :)

mrfreester
  • 1,981
  • 2
  • 17
  • 36
0

You actually don't need to save locators to class field. Page Object doesn't have to necessarily declare all elements in class fields. Page Object is an OBJECT, meaning that it must provide METHODs for manipulating with it.

So, you solution is just ideal. :)

Andrei Solntsev
  • 480
  • 2
  • 8