I like to use POM pattern with page factory and in fact I have been using it since long in my various automation frameworks.
Some time I am facing issue where I have to write text based xpaths in some projects and then when text changes, my xpath fails.
For example : currently my xpath is //div[text()='Welcome']
, Now when in application developer change welcome
to something else , my xpath will fail. I can not use id , class etc sometime as most of things are dynamic so I found text based xpath is only reliable option.
As we can not pass dynamic text with @Findby
, Is there any better way to handle such scenarios?
What I thought is as a solution :
- Create one separate class which will return me web elements only and I will pass text on call or from property file in xpath. For example :
private WebElement loginTextBox(String placeholder) {
By.xpath(String.format("//a/h4[text()='%s']", placeholder));
}
But does this make sense? It will need more time to write methods for each web element. Can this be replacement of @Findby for dynamic values?