Make Selenium Easy

Why It Is Not Possible To Read Locators From External Sources In Page Factory Of Selenium WebDriver

Hello Folks,

Many automation tester wants to read locators from external sources like database, properties file or excel sheets. Reading locators from external sources and using it in your program is very easy if we don’t use Page Factory. It becomes difficult because of @FindBy annotation.

What is annotation in Java?

Annotations in Java are tags which are used to provide metadata about Class, method, interface, field etc which can be used by Java compiler and JVM. A Java annotation can have elements for which we can pass some values. It is similar to attribute of a web element.

For example:

WebElement someElement;

“someID” must be a constant. It must not contain any variable in it.

Understanding the problems in reading locators from external source in Page Factory: 

  1. We use @FindBy annotations with variable of type WebElement at class level. It is not possible to call any method at class level i.e. outside any method in java. Also you can not create any method which can return you webelemet declaration with @FindBy annotation.

2. Suppose somehow you find any solution of above problem, there is another problem .Suppose you arrange locators with locator strategy in external source as “LocatorStrategy:LocatorValue”. When you split using code, you will store “LocatorStrategy” and “LocatorValue” in two different variables and you will write as below:

As soon as you write as above, Java compiler will give you error because it is expecting some locator strategy like id, name, xpath etc rather than any variable.

So, you need to face above problems if you want to read locators from external sources. You can do max parameterisation as below:

So, as of my knowledge, if you use page factory, you can not read locators from external sources.  But if you use plain page object model, you can read locators from external sources easily.

That’s it guys. Hope you would have clear concepts now.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Why It Is Not Possible To Read Locators From External Sources In Page Factory Of Selenium WebDriver

by Amod Mahajan time to read: 1 min
%d bloggers like this: