Sunday, October 18, 2015

Multiple Column Sorting On WebTable

On previous post we learn how to handle webtable in a better manner. Now we try to validate the webtable with sorting facility.

Steps 1 :
 Load page which contains WebTable
e.g :
driver.get("https://en.wikipedia.org/wiki/List_of_largest_oil_and_gas_companies_by_revenue");

Step 2 :
 Access the WebTable.
e.g :
WebTable table =WebTable.getTable(driver.findElement(By.xpath(".//*[@id='mw-content-text']/table/tbody/tr/td/table")));

Step 3 :
  Store each row as a object in a list.
e.g :
for (int tableBodyRow =0; tableBodyRow < table.getTableBody().getRowCount();tableBodyRow++) {
TableDataObject tabObj = new TableDataObject();
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"));
tabObj.setCountry_Name(table.getTableBody().getRowElement(tableBodyRow).findElement(By.xpath(".//span/a/img")).getAttribute("alt").trim());
}
else if(tableBodyColumn ==1){
//System.out.println(" TableBody Cell Text : " + table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText());
tabObj.setCompany_Name(table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText().trim());
}
else if(tableBodyColumn ==2){
//System.out.println(" TableBody Cell Text : " + table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText());
tabObj.setRevenue_2014(table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText().trim());
}
}
ltTableDataObj.add(tabObj);
Step 4 :
 Now click on the desired column header to sort the table.

e.g :
driver.findElement(By.cssSelector(".headerSort:first-child+th")).click();

Step 5 :
 Now sort the previously stored list.

e.g :
Collections.sort(ltTableDataObj, new TableDataObject.CompanyNameSort()); 

This is the most important part of this post. Sorting a list of any object can not be done using only sort method. To get this facility from object we need to implement comparable or comparator. If we use comparable then we can choose only one field to sort but here we need to sort the objects against different column so we implement comparator.

e.g :
/**
 * Comparator implementation using anonymous class
 */
static Comparator<TableDataObject> country_NameComparator = new Comparator<TableDataObject>() {
@Override
public int compare(TableDataObject o1, TableDataObject o2) {
if(o1.getCountry_Name().equals(o2.getCountry_Name())){
if(o1.getRevenue_2014().compareTo(o2.getRevenue_2014())>1){
return o2.getCountry_Name().compareTo(o1.getCountry_Name());
}
else {
return o1.getCountry_Name().compareTo(o2.getCountry_Name());
}

}
else {
return o1.getCountry_Name().compareTo(o2.getCountry_Name());
}
}
};

/**
 * Comparator implementation using sub class
 */
static class CompanyNameSort implements Comparator<TableDataObject>{

@Override
public int compare(TableDataObject o1, TableDataObject o2) {
return o1.company_Name.compareToIgnoreCase(o2.company_Name);
}

}

Step 6 :
 Now iterate on the webtable again and store this on another different list of object.

Step 7 :
 Now compare two list

e.g :
System.out.println(ltTableDataObj.equals(ltTableDataObj2));

Comple Code Given Below :

Table Object Class :

import java.util.Comparator;

public class TableDataObject {
private String country_Name;
private String company_Name;
private String revenue_2014;

@Override
public String toString() {
return "TableDataObject [country_Name=" + country_Name + ", company_Name=" + company_Name + ", revenue_2014="
+ revenue_2014 + "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((company_Name == null) ? 0 : company_Name.hashCode());
result = prime * result + ((country_Name == null) ? 0 : country_Name.hashCode());
result = prime * result + ((revenue_2014 == null) ? 0 : revenue_2014.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TableDataObject other = (TableDataObject) obj;
if (company_Name == null) {
if (other.company_Name != null)
return false;
} else if (!company_Name.equals(other.company_Name))
return false;
if (country_Name == null) {
if (other.country_Name != null)
return false;
} else if (!country_Name.equals(other.country_Name))
return false;
if (revenue_2014 == null) {
if (other.revenue_2014 != null)
return false;
} else if (!revenue_2014.equals(other.revenue_2014))
return false;
return true;
}

public String getCountry_Name() {
return country_Name;
}

public void setCountry_Name(String country_Name) {
this.country_Name = country_Name;
}

public String getCompany_Name() {
return company_Name;
}

public void setCompany_Name(String company_Name) {
this.company_Name = company_Name;
}

public String getRevenue_2014() {
return revenue_2014;
}

public void setRevenue_2014(String reevenue_2014) {
this.revenue_2014 = reevenue_2014;
}
/**
* Implement Comparator using anonymous class
*/
static Comparator<TableDataObject> country_NameComparator = new Comparator<TableDataObject>() {
@Override
public int compare(TableDataObject o1, TableDataObject o2) {
if(o1.getCountry_Name().equals(o2.getCountry_Name())){
if(o1.getRevenue_2014().compareTo(o2.getRevenue_2014())>1){
return o2.getCountry_Name().compareTo(o1.getCountry_Name());
}
else {
return o1.getCountry_Name().compareTo(o2.getCountry_Name());
}

}
else {
return o1.getCountry_Name().compareTo(o2.getCountry_Name());
}
}
};
/**
* Implement Comparator using inner class
*/
static class CompanyNameSort implements Comparator<TableDataObject>{

@Override
public int compare(TableDataObject o1, TableDataObject o2) {
return o1.company_Name.compareToIgnoreCase(o2.company_Name);
}
}
}

Test Class :

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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) {
List<TableDataObject> tableExpected = new ArrayList<TableDataObject>();
List<TableDataObject> tableOnPage = new ArrayList<TableDataObject>();
WebDriver driver = new FirefoxDriver(new FirefoxBinary(new File("G:/Program Files/Mozilla Firefox/firefox.exe")),new FirefoxProfile());
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 Body Row Count : " + table.getTableBody().getRowCount() + " Table Body Column Count : " + table.getTableBody().getRow(0).getCoulmnCount());
for (int tableBodyRow =0; tableBodyRow < table.getTableBody().getRowCount();tableBodyRow++) {
TableDataObject tabObj = new TableDataObject();
for (int tableBodyColumn = 0; tableBodyColumn <  table.getTableBody().getRow(tableBodyRow).getCoulmnCount(); tableBodyColumn++) { 
if(tableBodyColumn==0){
tabObj.setCountry_Name(table.getTableBody().getRowElement(tableBodyRow).findElement(By.xpath(".//span/a/img")).getAttribute("alt").trim());
}
else if(tableBodyColumn ==1){
tabObj.setCompany_Name(table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText().trim());
}
else if(tableBodyColumn ==2){
tabObj.setRevenue_2014(table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText().trim());
}
}
tableExpected.add(tabObj);
}
driver.findElement(By.cssSelector(".headerSort:first-child")).click();
Collections.sort(tableExpected, TableDataObject.country_NameComparator);
System.out.println("After Sorted Data : "+ tableExpected);
for (int tableBodyRow =0; tableBodyRow < table.getTableBody().getRowCount();tableBodyRow++) {
TableDataObject tabObj = new TableDataObject();
for (int tableBodyColumn = 0; tableBodyColumn <  table.getTableBody().getRow(tableBodyRow).getCoulmnCount(); tableBodyColumn++) {
if(tableBodyColumn==0){
tabObj.setCountry_Name(table.getTableBody().getRowElement(tableBodyRow).findElement(By.xpath(".//span/a/img")).getAttribute("alt").trim());
}
else if(tableBodyColumn ==1){
tabObj.setCompany_Name(table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText().trim());
}
else if(tableBodyColumn ==2){
tabObj.setRevenue_2014(table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText().trim());
}
}
tableOnPage.add(tabObj);
}
System.out.println("New Table :"+ tableOnPage);
System.out.println(tableExpected.equals(tableOnPage));
driver.findElement(By.cssSelector(".headerSort:first-child+th")).click();

/**
* Sort using inner class which is defined in TableDataObject class.
*/
//Collections.sort(ltTableDataObj, new TableDataObject.CompanyNameSort());
Collections.sort(tableExpected, new Comparator<TableDataObject>() {

@Override
public int compare(TableDataObject o1, TableDataObject o2) {
return o1.getCompany_Name().compareToIgnoreCase(o2.getCompany_Name());
}
});
System.out.println("After Sorted Data : "+ tableExpected);
for (int tableBodyRow =0; tableBodyRow < table.getTableBody().getRowCount();tableBodyRow++) {
TableDataObject tabObj = new TableDataObject();
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"));
tabObj.setCountry_Name(table.getTableBody().getRowElement(tableBodyRow).findElement(By.xpath(".//span/a/img")).getAttribute("alt").trim());
}
else if(tableBodyColumn ==1){
//System.out.println(" TableBody Cell Text : " + table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText());
tabObj.setCompany_Name(table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText().trim());
}
else if(tableBodyColumn ==2){
//System.out.println(" TableBody Cell Text : " + table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText());
tabObj.setRevenue_2014(table.getTableBody().getRow(tableBodyRow).getCell(tableBodyColumn).getText().trim());
}
}
tableOnPage.add(tabObj);
}
System.out.println("New Table :"+ tableOnPage);
System.out.println(tableExpected.equals(tableOnPage));
}
finally{
driver.close();
}
}
}

WebTable Code:

Complete Code given on previous post.


2 comments:

  1. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

    selenium training in bangalore|

    ReplyDelete
  2. Hi arun,

    I tried use the above code, but I got error at below line.
    saying the web table can not resolved. can you please let me know how to resolve it

    WebTable table =WebTable.getTable(driver.findElement(By.xpath(".//*[@id='mw-content-text']/table/tbody/tr/td/table")));

    ReplyDelete