What is this @FindBy ?
It’s an annotation on Selenium that organize the elements and using the PageFactory, these WebElements are usually initialised when a Page Object is created. It’s a best practice of Page Object Models, but more than this is a way to let your group of elements well-structured and just call when you need it.
The @FindBy annotation is used to locate one or more WebElements using a single criterion. For example:
To create the instance for these elements, you can use:
For the cases you want to search for all the given criteria, you can use @FindBys and for the ones you want to search for at least one of the given criteria, @FindAll
So, for example:
The loginfiends list should contain the element with name = username and the element that contains name = password, 2 elements. If you use @FindBys it wouldn’t bring anything, since there is no element with two names (both of the criteria).
So, this structure makes the code much easier to read. You should place the @FindBy in the beginning of your page object class.
What is the Object repository ?
It could be a properties/text file where you place all the locators for the elements and you use selenium to call each one. The property file contains a collection of key-value pairs, with the key being a logical name identifying the object and the value containing unique objects properties used to identify the object on a screen. You can use this concept of Object Repository for any language, not only Java and not only with Selenium.
It contains the key of the element and the value to find it. The corresponding value consists of two parts: the attribute type used for uniquely identifying the object on screen and the corresponding attribute value.
After this, you need to create a class to get the elements . For example:
As you can see, objects can be identified using a number of different properties, including object IDs, CSS selectors and XPath expressions.
Now you can use these in your scripts to execute any scenario. You just need to call the Map repository and get the locator key.
So, in the end you just need to update this properties file with the new locators.
The aim of both of the methods is to reduce the maintenance time when you have new locators. If you are using Selenium, I suggest you to use @FindBy since it’s a good practice and the framework provides this option, but you don’t have this for all the test frameworks in the market, for those you can have a map repository.
Cheers guys, see you next weekend 🙂