Store and access locator details from excel, json and properties file in Selenium POM

When @FindBy annotation is used in the Selenium Page Object Model the value of its attributes how, using etc need to be available in the code. There is no mechanism to store and access these locator data from outside the source code, say in an excel file. This mechanism requires Java-8.

Overview

This mechanism seamlessly integrates with the current Page Object Model and PageFactory implementations. More importantly this supports existing @FindBy, @FindBys and @FindAll annotations alongside new development in the same PageObject.

To achieve this the following points need to be addressed –

  • Inform the PageFactory the location of the data file and indicate PageObject fields which need to be decorated.
  • Fixed format for storing the locator data.
  • Logic to access these locator data.
  • Store these data in a Java class which can be retrieved concurrently.
  • An ElementLocatorFactory and DefaultFieldDecorator implementation which will combine all the new classes to the existing PageFactory initialization logic.

Source code and jar download is available in Github and Maven respectively.

Let’s look at each one of these in further details using the case of storing and accessing locator data from an excel file.

Data File Location And Field Decorator Annotation

For the former part we use an annotation named @ExcelFile which has two attributes, filePath and sheetName. filePath points to the location of the excel file. sheetName refers to the sheet within the workbook, this defaults to the value of ‘Data’. To indicate that a field in a PageObject locators are in the excel file, we use the @FindByExcel annotation. This has no attributes and is just a marker annotation.

@ExcelFile(filePath=”E:\\locator\\sample-xlsx-file.xlsx”)
public class LoginFileExcelPageObject {

@FindByExcel
private WebElement usernameInput;

File Format

This is fixed and needs to be followed for the framework to work correctly. The value of the class does not need to be specified for each field. If it is left blank the last value of class will be reused. The header of the columns have no specific purpose but the column order is important.

Access Locator Data

The parser logic is contained in the ExcelFileProcessor class inside the parseDataSource() method which uses Apache POI jars to access the excel file. We need to pass an instance of this class to the custom ElementLocatorFactory mentioned in the next point.

Custom ElementLocatorFactory and DefaultFieldDecorator

These integrate the new classes to the existing PageFactory mechanism. The following code is all that is required in the PageObject class.

FileElementLocatorFactory felf = new FileElementLocatorFactory(driver, new ExcelFileProcessor());
FileFieldDecorator ffd = new FileFieldDecorator(felf);
PageFactory.initElements(ffd, this);

Locator Data Store

This is an implementation detail which is not necessary for the user of the framework to know. The locator data is cached in a ConcurrentHashMap to avoid repeated file access.

Excel Data File Summary

File Detail Annotation   ->   @ExcelFile

Field Decorator Annotation   ->   @FindByExcel

ElementFactory Initialization   ->   FileElementLocatorFactory fileFactory = new FileElementLocatorFactory(driver, new ExcelFileProcessor());

Data File Format   ->   Column Order is Full Classname, WebElement Variable Name, How, Using. Multiple PageObject locator details can be added to a single excel file.

JSON Data File Summary

File Detail Annotation   ->   @JsonFile

Field Decorator Annotation   ->   @FindByJson

ElementFactory Initialization   ->   FileElementLocatorFactory fileFactory = new FileElementLocatorFactory(driver, new JsonFileProcessor());

Data File Format   ->   Array of PageObject classes with fullname in ‘className’. The WebElements for each PageObject are also described in details in an array in ‘fieldBy’.  Multiple PageObject locator details to a single json file.

Properties Data File Summary

File Detail Annotation   ->   @PropertiesFile

Field Decorator Annotation   ->   @FindByProperties

ElementFactory Initialization   ->   FileElementLocatorFactory fileFactory = new FileElementLocatorFactory(driver, new PropertiesFileProcessor());

Data File Format   ->   Key contains of delimited text of pageobject classfullname and webelement variable name. Values contains of delimited text of how and using. The delimiter is defined in the @PropertiesFile annotation in the delimiter attribute, default is ‘##’. Multiple PageObject locator details to a single properties file.

Implementation Process

  • Include the jar file by adding the following dependency to pom.xml. For importing dependencies for other languages please refer here.

<dependency>
<groupId>tech.grasshopper</groupId>
<artifactId>findbyfilewebelement</artifactId>
<version>1.0.0</version>
</dependency>

  • Specify locator data in the desired file format as described above.
  • Mention the file location (@ExcelFile or @JsonFile or @PropertiesFile) annotation at class level in PageObject class specifying the locator data file location. It can be absolute or relative path.
  • Decorate the required WebElement or WebElement list fields of the PageObject class using @FindByExcel or @FindByJson or @FindByProperties annotation. Allowed to mix with existing @FindBy annotation family decorated fields.
  • Add the following code to the PageObject constructor. Use JsonFileProcessor for Json and PropertiesFileProcessor for Properties file format for FileElementLocatorFactory constructor.

FileElementLocatorFactory felf = new FileElementLocatorFactory(driver, new ExcelFileProcessor());
FileFieldDecorator ffd = new FileFieldDecorator(felf);
PageFactory.initElements(ffd, this);

  • Instantiate the PageObject the usual way by passing the driver instance.

Sample Implementation

A sample implementation using this mechanism for automating basic workflow of the StackOverflow (valid as of 22nd October 2018) website is available in Github. All three file storage formats are implemented. It looks at the following three cases –

  • navigates and tests components of the home page. (Xls PO and Test)
  • navigates to home page and clicks on first available question to display details. (Xls PO and Test)
  • navigates to home page clicks on tags menu to display details and sort tag names. (Xls PO and Test)

It is setup to run in parallel using Chrome. Feel free to change it to run serially or on your favorite browser by adding the desired driver location as system property to the surefire plugin configuration in pom.xml.

1 thought on “Store and access locator details from excel, json and properties file in Selenium POM”

  1. Hi,

    This is a great Cucumber resource site. Thanks for maintaining it. I don’t see any options to subscribe to new posts though. Have you considered adding that option?

Leave a Reply

Your email address will not be published. Required fields are marked *