I am asking myself if I am on the right track:
I have pages which are initialized with PageFactory. The static elements, I describe with FindBy annotation. This works fine.
However, on this pages can be tables (sometimes many).
The tables have filtering, column selection, row selection etc. The tables are always built the same way and have the same elements. I started to build the locators with FindBy, but that seems tedious and that for each table, each element etc. Simplified it could look like this:
@FindBy(css="div[id*='tableName1'] div[class='filterOpen']")
WebElement filterOpen;
@FindBy(css="div[id*='tableName1'] div[class='openedFilter'] div[class=''apply]")
WebElement filterApply;
@FindBy(css="div[id*='tableName2'] div[class='filterOpen']")
WebElement filterOpen;
@FindBy(css="div[id*='tableName2'] div[class='openedFilter'] div[class=''apply]")
WebElement filterApply;
If anything changes in the table build (e.g. in the tableName part), I need to adapt all annotations.
Therefore, I created a class where I build up these locator strings in a general way. I use them to replace tableName and return a By element, which I can user for further actions.
//Table (Header)
String tableHeader = "div[id*='+|tableName+|']";
String tableColumnHeader = tableHeader+" div[data-col-id='"+columnName+"']";
String tableFilterOpen = tableHeader+" div[class='filterOpen']";
etc.
public TableLocators(String tableN) {
tableName = tableN;
}
public By getByTableHeader(){
return By.cssSelector(replaceTableName(tableHeader ));
}
etc.
Again, this is simplified, but I hope, you get the strategy.
But I am asking myself: Is this really the proper way to do it? On top I am having a mixture of static FindBy elements and dynamic elements. But that seems ok according to other posts.
Thank you for your answer.
Jonas