WebDriver UI Mapper is a tool that maps JSON files with WebDriver locators to Locator java objects
- Easier maintenance by having a central location for UI objects instead of having them scattered throughout the tests.
- Readability can be improved by giving the selectors human-readable names.
- Possible to reuse the locators.
In your Maven project, add the following to your pom.xml file
<dependency>
<groupId>com.github.johanbrorson</groupId>
<artifactId>webdriver-ui-mapper</artifactId>
<version>[2.0.0, 3.0)</version>
</dependency>
[
{
"name": "searchInput",
"selector": "//input[@name='q']",
"method": "XPATH"
},
{
"name": "searchButton",
"selector": "btnG",
"method": "NAME"
}
]
@LocatorFile(filePath = "locators/SearchPage.json")
public class SearchPage {
private final WebDriver driver;
@ByLocator private By searchInput;
@ByLocator private By searchButton;
public SearchPage(WebDriver driver) throws IOException {
this.driver = driver;
ByLocatorHelper.initInstanceVariables(this);
}
public void enterSearchText(String searchText) {
driver.findElement(searchInput).clear();
driver.findElement(searchInput).sendKeys(searchText);
}
public void clickSearchButton() {
driver.findElement(searchButton).click();
}
}