On previous post we learn how to play with WebTable in a simple manner. Now we will try to write a code where we can separate our table body cell,row,header and footer part.
So code example is given below :
This class is our main entry point.
package com.automation.webtable.table;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.automation.webtable.cellandrow.WebTableRow;
import com.automation.webtable.part.WebTablePart;
public class WebTable {
private WebElement tableElement;
private WebTable(WebElement tableElement) {
this.tableElement = tableElement;
}
public static WebTable getTable(WebElement tableElement) {
return new WebTable(tableElement);
}
public WebElement getTableCaptionElement() {
// return tableElement.findElement(By.tagName("caption"));
return tableElement.findElement(By.xpath("./caption"));
}
public String getTableCaptionText() {
return getTableCaptionElement().getText();
}
public WebTableRow getRow(int index) {
return new WebTableRow(getRowElement(index));
}
public WebElement getRowElement(int index) {
// WebElement rowElement = (WebElement) tableElement.findElements(By.tagName("tr")).get(index);
WebElement rowElement = (WebElement) tableElement.findElements(By.xpath("./tr")).get(index);
return rowElement;
}
public int getRowCount() {
return tableElement.findElements(By.tagName("tr")).size();
}
public WebTablePart getTableHeader() {
return new WebTablePart(getTableHeaderElement());
}
public WebElement getTableHeaderElement() {
// WebElement tableHeadElement = tableElement.findElement(By.tagName("thead"));
WebElement tableHeadElement = tableElement.findElement(By.xpath("./thead"));
return tableHeadElement;
}
public WebTablePart getTableFooter() {
return new WebTablePart(getTableFooterElement());
}
public WebElement getTableFooterElement() {
// WebElement tableFootElement = tableElement.findElement(By.tagName("tfoot"));
WebElement tableFootElement = tableElement.findElement(By.xpath("./tfoot"));
return tableFootElement;
}
public WebTablePart getTableBody() {
return new WebTablePart(getTableBodyElement());
}
public WebElement getTableBodyElement() {
// WebElement tableBodyElement = tableElement.findElement(By.tagName("tbody"));
WebElement tableBodyElement = tableElement.findElement(By.xpath("./tbody"));
return tableBodyElement;
}
}
So code example is given below :
This class is our main entry point.
package com.automation.webtable.table;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.automation.webtable.cellandrow.WebTableRow;
import com.automation.webtable.part.WebTablePart;
public class WebTable {
private WebElement tableElement;
private WebTable(WebElement tableElement) {
this.tableElement = tableElement;
}
public static WebTable getTable(WebElement tableElement) {
return new WebTable(tableElement);
}
public WebElement getTableCaptionElement() {
// return tableElement.findElement(By.tagName("caption"));
return tableElement.findElement(By.xpath("./caption"));
}
public String getTableCaptionText() {
return getTableCaptionElement().getText();
}
public WebTableRow getRow(int index) {
return new WebTableRow(getRowElement(index));
}
public WebElement getRowElement(int index) {
// WebElement rowElement = (WebElement) tableElement.findElements(By.tagName("tr")).get(index);
WebElement rowElement = (WebElement) tableElement.findElements(By.xpath("./tr")).get(index);
return rowElement;
}
public int getRowCount() {
return tableElement.findElements(By.tagName("tr")).size();
}
public WebTablePart getTableHeader() {
return new WebTablePart(getTableHeaderElement());
}
public WebElement getTableHeaderElement() {
// WebElement tableHeadElement = tableElement.findElement(By.tagName("thead"));
WebElement tableHeadElement = tableElement.findElement(By.xpath("./thead"));
return tableHeadElement;
}
public WebTablePart getTableFooter() {
return new WebTablePart(getTableFooterElement());
}
public WebElement getTableFooterElement() {
// WebElement tableFootElement = tableElement.findElement(By.tagName("tfoot"));
WebElement tableFootElement = tableElement.findElement(By.xpath("./tfoot"));
return tableFootElement;
}
public WebTablePart getTableBody() {
return new WebTablePart(getTableBodyElement());
}
public WebElement getTableBodyElement() {
// WebElement tableBodyElement = tableElement.findElement(By.tagName("tbody"));
WebElement tableBodyElement = tableElement.findElement(By.xpath("./tbody"));
return tableBodyElement;
}
}
This class is created to handle Row for a particular part of the WebTable.
package com.automation.webtable.part;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.automation.webtable.cellandrow.WebTableRow;
public class WebTablePart {
private WebElement tablePartElement;
public WebTablePart(WebElement tablePartElement) {
this.tablePartElement = tablePartElement;
}
public WebTableRow getRow(int index) {
return new WebTableRow(getRowElement(index));
}
public WebElement getRowElement(int index) {
// WebElement rowElement = (WebElement) tablePartElement.findElements(By.tagName("tr")).get(index);
WebElement rowElement = (WebElement) tablePartElement.findElements(By.xpath("./tr")).get(index);
return rowElement;
}
public int getRowCount() {
// return tablePartElement.findElements(By.tagName("tr")).size();
return tablePartElement.findElements(By.xpath("./tr")).size();
}
}
This class is created to handle Row for the particular part of a Table.
package com.automation.webtable.cellandrow;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class WebTableRow {
private WebElement rowElement;
public WebTableRow(WebElement rowElement) {
this.rowElement = rowElement;
}
public WebTableCell getCell(int index) {
return new WebTableCell(getCellElement(index));
}
public WebElement getCellElement(int index) {
// WebElement cellElement = (WebElement) rowElement.findElements(By.tagName("td")).get(index);
WebElement cellElement = (WebElement) rowElement.findElements(By.xpath("./td")).get(index);
return cellElement;
}
public int getCoulmnCount() {
// return rowElement.findElements(By.tagName("td")).size();
return rowElement.findElements(By.xpath("./td")).size();
}
public int getHeaderCoulmnCount() {
// return rowElement.findElements(By.tagName("th")).size();
return rowElement.findElements(By.xpath("./th")).size();
}
public WebTableCell getHeaderCell(int index) {
return new WebTableCell(getHeaderCellElement(index));
}
public WebElement getHeaderCellElement(int index) {
// WebElement cellElement = (WebElement) rowElement.findElements(By.tagName("th")).get(index);
WebElement cellElement = (WebElement) rowElement.findElements(By.xpath("./th")).get(index);
return cellElement;
}
}
This class is created to handle Row of a particular Cell.
package com.automation.webtable.cellandrow;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
public class WebTableCell {
private WebElement cellElement;
public WebTableCell(WebElement cellElement)
{
this.cellElement = cellElement;
}
public String getText() {
return cellElement.getText();
}
public String getAttribute(String attribute)
{
return cellElement.getAttribute(attribute);
}
public Point getLocation() {
return cellElement.getLocation();
}
public Dimension getSize() {
return cellElement.getSize();
}
public boolean isDisplayed() {
return cellElement.isDisplayed();
}
public boolean isEnabled() {
return cellElement.isEnabled();
}
}
This class is a example class for accessing WebTable.
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import com.automation.webtable.table.WebTable;
public class AccessTable {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
try{
driver.get("https://en.wikipedia.org/wiki/List_of_largest_oil_and_gas_companies_by_revenue");
WebTable table =WebTable.getTable(driver.findElement(By.xpath(".//*[@id='mw-content-text']/table/tbody/tr/td/table")));
System.out.println("Table Head Row Count : " + table.getTableHeader().getRowCount() + " Table Head Column Count : " + table.getTableHeader().getRow(0).getHeaderCoulmnCount());
System.out.println("Table Body Row Count : " + table.getTableBody().getRowCount() + " Table Body Column Count : " + table.getTableBody().getRow(0).getCoulmnCount());
System.out.println("Table Footer Row Count : " + table.getTableFooter().getRowCount());
for (int tableHeaderRow =0; tableHeaderRow < table.getTableHeader().getRowCount();tableHeaderRow++) {
for (int tableHeaderColumn = 0; tableHeaderColumn < table.getTableHeader().getRow(tableHeaderRow).getHeaderCoulmnCount(); tableHeaderColumn++) {
System.out.println(" TableHeader Row : " + (tableHeaderRow+1) + " TableHeader Column : " + (tableHeaderColumn+1) +
" TableHeader Cell Text : " + table.getTableHeader().getRow(tableHeaderRow).getHeaderCell(tableHeaderColumn).getText());
}
}
for (int tableBodyRow =0; tableBodyRow < table.getTableBody().getRowCount();tableBodyRow++) {
for (int tableBodyColumn = 0; tableBodyColumn < table.getTableBody().getRow(tableBodyRow).getCoulmnCount(); tableBodyColumn++) {
System.out.print(" TableBody Row : " + (tableBodyRow+1) + " TableBody Column : " + (tableBodyColumn+1));
if(tableBodyColumn==0){
System.out.println(" TableBody Cell Text : " + table.getTableBody().getRowElement(tableBodyRow).findElement(By.xpath(".//span/a/img")).getAttribute("alt"));
}
else
System.out.println(" TableBody Cell Text : " + table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText());
}
}
for (int tableFooterRow =0; tableFooterRow < table.getTableFooter().getRowCount();tableFooterRow++) {
for (int tableFooterColumn = 0; tableFooterColumn < table.getTableFooter().getRow(tableFooterRow).getCoulmnCount(); tableFooterColumn++) {
System.out.println(" TableFooter Row : " + (tableFooterRow+1) + " TableFooter Column : " + (tableFooterColumn+1) +
" TableFooter Cell Text : " + table.getTableFooter().getRow(tableFooterRow).getCell(tableFooterColumn).getText());
}
}
}
finally{
driver.close();
}
}
}
No comments:
Post a Comment