Thursday, 30 November 2017

Tricky question, How can you verify sorting working as expected via selenium Automation.

Assume that we have a grid which is hosting students information, columns names ( id, name ) and wanted to verify sorting ( name ) is working or not.

I think, you know how to loop each row in the table to get student names.

here is the tricky part to verify, weather all the rows are in sorted order or not ( ye we can do it for asc/desc order )

in Java we have String compareTo() method, this helps us.

compareTo() : is used for comparing two strings lexicographical ( alpha Numerical order )


returns 0 ; if both are equals
returns +Number , means First is Greater than Second
returns -Number , means Second is Greater than First



public class CompareToExample {
   public static void main(String args[]) {
       String str1 = "String method tutorial";
       String str2 = "compareTo method example";
       String str3 = "String method tutorial";

       int var1 = str1.compareTo( str2 );
       System.out.println("str1 & str2 comparison: "+var1);

       int var2 = str1.compareTo( str3 );
       System.out.println("str1 & str3 comparison: "+var2);

       int var3 = str2.compareTo("compareTo method example");
       System.out.println("str2 & string argument comparison: "+var3);
   }
}

out put:

str1 & str2 comparison: -16
str1 & str3 comparison: 0
str2 & string argument comparison: 0