I'm following page object design pattern for Selenium automation and I can guess that many people store locators in .properties
file and access them in code. It seems great to keep locators at separate place.
Since, I haven't work on any big project for Selenium automation, I would like to know thoughts on following so that I can avoid problems that might raise in future:
Is storing locators in properties file helpful in big projects(where test cases are more than 1000 or so)?
a) If not helpful in big projects, what are the difficulties that make us not to store locators in properties file?
b) If it's helpful, what are the precautions if that are taken makes job easier?
Best practice is to use the page object model. The core of this idea is that each page on your site is represented by a class in your test project. All locators for that page should reside in that class. All actions that a user might take on that page would be represented by a method in that class.
There is a PageObject Library that you can use or take inspiration from. Another way of centralising your locators is to use a custom locator like Click Element abc=SomeVirtualId .
I would argue storing the files in the page class itself. Loading from properties file would incur additional overhead or parsing a large file. Maintaining such a file would also be harder, even with a good tool support you would be forced to use CTRL + F more than you should.
Even on a more conceptual level it feels wrong. A good fit for storing in properties files are configurable parameters, and especially the ones that are good candidates to be tweaked in runtime.
Locators don't have this nature. If the benefit you're seeking is declaring in a central place you should instead use dedicated constant class, which will give you much better refactoring options.
I would definitely agree with @Master Slave. The main purpose of selenium
is to make the UI
testing easier. Storing locators
in the property
file is cumbersome and additional overhead plus a nightmare for refactoring. One of the main reasons why the PageObject
is popular is because it's ability to map elements with a very intuitive way
@FindBy(name="q")
private WebElement searchField;
@FindBy(name="btnG")
private WebElement searchButton;
It's just not only more readable but also far easier in case of refactoring and debugging. And, when something goes wrong on the page or changes there is a KNOWN place you go to change and you know where it is!
There are two basic ways:
1) Using FindBy annotation
@FindBy(xpath = "//*[@class = 'stackoverflow']")
private WebElement question;
2) Using By / WebElement class in the method structure
By stackoverflow = By.xpath("//*[@class = 'stackoverflow']");
WebElement stackoverflowElement = getDriver().findElement(stackoverflow);
And I completely agree with @Saifur and @MasterSlave.
I agree for the small projects with one running environment. Lets assume we have the project runs on two environments like Test and Production. Assume that in Test the locators are changed, then if you want to change code that will work properly in the both cases you should go to the branch. In case if locators placed in the property file you just change the file belongs to the environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With