Table classes

When building the page object classes, there will frequently be components on a page that are common to multiple pages, but not all pages, and rather than including the similar locators and methods in each class, users can build a common class for just that portion of the page. HTML tables are a typical example of a common component that can be classed.

So, what users can do is create a generic class for the common table rows and columns, extend the subclasses that have a table with this new class, and pass in the dynamic ID or locator to the constructor when extending the subclass with that table class.

Let's take a look at how this is done:

  1. Create a new page object class for the table component in the application, but do not derive it from the base class in the framework
  2. In the constructor of the new class, add a parameter of the type WebElement, requiring users to pass in the static element defined in each subclass for that specific table
  3. Create generic methods to get the row count, column count, row data, and cell data for the table
  4. In each subclass that inherits these methods, implement them for each page, varying the starting row number and/or column header rows if <th> is used rather than <tr>
  5. When the methods are called on each table, it will identify them using the WebElement passed into the constructor:
/**
* WebTable Page Object Class
*
* @author Name
*/
public class WebTablePO {
private WebElement table;

/** constructor
*
* @param table
* @throws Exception
*/
public WebTablePO(WebElement table) throws Exception {
setTable(table);
}

/**
* setTable - method to set the table on the page
*
* @param table
* @throws Exception
*/
public void setTable(WebElement table) throws Exception {
this.table = table;
}

/**
* getTable - method to get the table on the page
*
* @return WebElement
* @throws Exception
*/
public WebElement getTable() throws Exception {
return this.table;
}

....

Now, the structure of the class is simple so far, so let's add in some common "generic" methods that can be inherited and extended by each subclass that extends the class:

// Note: JavaDoc will be eliminated in these examples for simplicity sake

public int getRowCount() {
List<WebElement> tableRows = table.findElements(By.tagName("tr"));

return
tableRows.size();
}

public int getColumnCount() {
List<WebElement> tableRows = table.findElements(By.tagName("tr"));
WebElement headerRow = tableRows.get(1);
List<WebElement> tableCols = headerRow.findElements(By.tagName("td"));

return
tableCols.size();
}

public int getColumnCount(int index) {
List<WebElement> tableRows = table.findElements(By.tagName("tr"));
WebElement headerRow = tableRows.get(index);
List<WebElement> tableCols = headerRow.findElements(By.tagName("td"));

return
tableCols.size();
}

public String getRowData(int rowIndex) {
List<WebElement> tableRows = table.findElements(By.tagName("tr"));
WebElement currentRow = tableRows.get(rowIndex);

return currentRow.getText();
}

public String getCellData(int rowIndex, int colIndex) {
List<WebElement> tableRows = table.findElements(By.tagName("tr"));
WebElement currentRow = tableRows.get(rowIndex);
List<WebElement> tableCols = currentRow.findElements(By.tagName("td"));
WebElement cell = tableCols.get(colIndex - 1);

return
cell.getText();
}

Finally, let's extend a subclass with the new WebTablePO class, and implement some of the methods:

/**
* Homepage Page Object Class
*
* @author Name
*/
public class MyHomepagePO<M extends WebElement> extends WebTablePO<M> {

public MyHomepagePO(M table) throws Exception {
super(table);
}

@FindBy(id = "my_table")
protected M myTable;

// table methods
public int getTableRowCount() throws Exception {
WebTablePO table = new WebTablePO(getTable());
return table.getRowCount();
}

public int getTableColumnCount() throws Exception {
WebTablePO table = new WebTablePO(getTable());
return table.getColumnCount();
}

public int getTableColumnCount(int index) throws Exception {
WebTablePO table = new WebTablePO(getTable());
return table.getColumnCount(index);
}

public
String getTableCellData(int row, int column) throws Exception {
WebTablePO table = new WebTablePO(getTable());
return table.getCellData(row, column);
}

public String getTableRowData(int row) throws Exception {
WebTablePO table = new WebTablePO(getTable());
return table.getRowData(row).replace(" ", " ");
}

public void verifyTableRowData(String expRowText) {
String actRowText = "";
int totalNumRows = getTableRowCount();

// parse each row until row data found
for ( int i = 0; i < totalNumRows; i++ ) {
if ( this.getTableRowData(i).contains(expRowText) ) {
actRowText = this.getTableRowData(i);
break;
}
}

// verify the row data
try {
assertEquals(actRowText, expRowText, "Verify Row Data");
}

catch (AssertionError e) {
String error = "Row data '" + expRowText + "' Not found!";
throw new Exception(error);
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.104.214