Skip to content

04. How to read from JSON to Page Factory

Hemanth Sridhar edited this page Nov 6, 2021 · 5 revisions

If we have a JSON for example

[
 {
"name": "forgottenPasswordLinkInAlert",
"type": "xpath",
"value": "//div[@role='alert']/descendant::a"
},
{
"name": "error_msg",
"type": "xpath",
"value": "//div[@role='alert']"
}
]
  • name : name of the locator
  • type : type of the locator
  • value : value of the locator

Then In the Page Objects class

@FilePath(value = PageObjectsConfig.ERROR_MSG_PAGE)
public class ErrorPopupPage extends PageInitializer {

@SearchBy
private WebElement forgottenPasswordLinkInAlert;

@SearchBy(nameOfTheLocator="error_msg")
private WebElement errorMsg;

}
  • The @FilePath value must be the path to the json
  • The nameOfTheLocator value is optional. By default, the variable name is picked up.