I’m following the Page Object pattern suggested by Selenium, but how would I create a more specialized WebElement for a page. Specifically, we have tables on our pages and I have written some helper functions to get specific rows of a table, return the contents of a table, etc.
Currently, here is a snippet of a page object I created that has a table:
public class PermissionsPage {
@FindBy(id = "studyPermissionsTable")
private WebElement permissionTable;
@FindBy(id = "studyPermissionAddPermission")
private WebElement addPermissionButton;
...
}
So, what I’d like to do is have that permissionsTable to be a more customized WebElement that has some of those methods I mentioned earlier.
For example:
public class TableWebElement extends WebElement {
WebElement table;
// a WebDriver needs to come into play here too I think
public List<Map<String, String>> getTableData() {
// code to do this
}
public int getTableSize() {
// code to do this
}
public WebElement getElementFromTable(String id) {
// code to do this
}
}
I hope that this makes sense what I’m trying to explain. I guess what I’m looking for is a way to have this custom WebElement to do some additional stuff that’s table-specific. Add this custom element to a Page and take advantage of the way Selenium wires the webelements to the page based on the annotations.
Is it possible? And if so, does anyone know how this can be done?
I created an interface that combines all of the WebDriver interfaces:
It’s just there to wrap up all of the things WebElements can do when wrapping an element.
Then an implementation:
Then, for example a check box:
When using it in my script:
I’ve also come up with a way of wrapping the
Elementclasses. You have to create a few factories to replace the built-inPageFactory, but it is doable, and it lends a lot of flexibility.I’ve documented this process over on my site:
I’ve also got a project called selophane that was inspired by this and other questions: selophane