Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm working with Selenium to grab the row data from a table with two columns and trying to validate that they are applying the proper order to it's columns after they've been sorted by ascending and descending. The code to validate a single column's sort is:

int sortAttempts = 0;

protected void verifyFirstColSort(List<String> firstColumnEntries, int sortType) throws Exception {
        boolean sorted;
        try {
            // Sort type = 1 for descending, otherwise sort type = ascending
            if (sortType == 1) Collections.reverse(firstColumnEntries);
            sorted = Ordering.natural().isOrdered(firstColumnEntries);
            if (!sorted) {
                if (sortAttempts > 10) {
                    if (sortType == 0) {
                        Assert.fail(FAIL + "
------------------------------------------"
                                + "
Reason: First column is NOT sorted in ascending order." 
                                + "
Discovered order: " + firstColumnEntries);
                    } else {
                        Assert.fail(FAIL + "
------------------------------------------"
                                + "
Reason: First column is NOT sorted in descending order." 
                                + "
Discovered order: " + firstColumnEntries);
                    }
                } else {
                    System.out.print("P" + sortAttempts + " ");
                    listView.sortPrimaryColumnHeader();
                    listView.verifyPrimarySort(sortType);
                }
            } 
        } catch (StaleElementReferenceException e) {
            System.out.print("_");
            // return to the method that gathers the row data, which calls this method again
            listView.verifyPrimarySort(sortType); 
        }
        sortAttempts = 0;
    }
    
protected void verifySecondColSort(List<String> secondColumnEntries, int sortType) throws Exception {
        boolean sorted;
        try {
            // Sort type = 1 for descending, otherwise sort type = ascending
            if (sortType == 1) Collections.reverse(secondColumnEntries);
            sorted = Ordering.natural().isOrdered(secondColumnEntries);
            if (!sorted) {
                if (sortAttempts > 10) {
                    if (sortType == 0) {
                        Assert.fail(FAIL + "
------------------------------------------"
                                + "
Reason: Second column is NOT sorted in ascending order." 
                                + "
Discovered order: " + secondColumnEntries);
                    } else {
                        Assert.fail(FAIL + "
------------------------------------------"
                                + "
Reason: Second column is NOT sorted in descending order." 
                                + "
Discovered order: " + secondColumnEntries);
                    }
                } else {
                    System.out.print("S" + sortAttempts + " ");
                    listView.sortSecondaryColumnHeader();
                    listView.verifySecondarySort(sortType);
                }
            } 
        } catch (StaleElementReferenceException e) {
            System.out.print("_");
            // return to the method that gathers the row data, which calls this method again
            listView.verifySecondarySort(sortType); 
        }
        sortAttempts = 0;
    }

If you are wondering why I need to track sortAttempts, it's because the table module has to reload after each sort is applied. As far as I know, Selenium isn't able to take that scenario into account. As a result, sometimes the click action that applies a sort order fires off prematurely. Selenium doesn't recognize this, so I have to compensate for it myself by forcing it to repeat the click action a few times just in case the validation failure is due to this issue. It's annoying, but I didn't build the website. I just test it.

In any case, this works perfectly for individual column sort validation. My problem arises when I need to validate two columns at once. When two columns are sorted in ascending order, the first column to be sorted is designated as the "Primary" sort, and the second column to be sorted as the "Secondary" sort.

As an example, this is how it would look if both columns were sorted by ascending, with the first column as the primary sort:

enter image description here

I have been struggling to find a good way to validate a dual sort like this. I've tried multiple complicated ways of doing this, but so far nothing has worked perfectly. Unfortunately, this seems like such a unique scenario that I haven't been able to research another example of this problem. I've been working on it for so long that I feel as though I've tried to reinvent the wheel. At the moment, this is the code for dual sort validation (yes, I know this is a mess):

    
    protected void verifyDualSort(List<String> firstColumnEntries, List<String> secondColumnEntries, int sortTypeFirst, int sortTypeSecond, int primaryCol) throws Exception {
            try {
                // first column is primary
                if (primaryCol == 0) {
                    verifyFirstColSort(firstColumnEntries, sortTypeFirst);
                    for (int i = 0; i < firstColumnEntries.size(); i++) {
                        String current1 = firstColumnEntries.get(i);
                        String current2 = secondColumnEntries.get(i);
                        if (i == 0 || current2.equals("")) continue;        
                        String previous1 = firstColumnEntries.get(i - 1);
                        String previous2 = secondColumnEntries.get(i - 1);
                        if (current1.equals(previous1)) {
                            // Sort type: 0 = ascending, 1 = descending
                            if (sortTypeSecond == 0 && current2.compareTo(previous2) < 0) {
                                if (dualSortAttempts < 10) {
                                    dualSortAttempts++;
                                    System.out.print("DSA" + dualSortAttempts + " ");
                                    listView.sortSecondaryColumnHeader();
                                    listView.verifyDualSort(sortTypeFirst, sortTypeSecond, primaryCol);
                                } else {
                                    Assert.fail(FAIL + "
------------------------------------------"
                                            + "
Reason: Second column is NOT sorted in ascending order."
                                            + "
Discovered first column order: " + firstColumnEntries
                                            + "
Discovered second column order: " + secondColumnEntries);
                                }       
                            } else if (sortTypeSecond == 1 && current2.compareTo(previous2) > 0 ){
                                if (dualSortAttempts < 10) {
                                    dualSortAttempts++;
                                    System.out.print("DSD" + dualSortAttempts + " ");
                                    listView.sortSecondaryColumnHeader();
                                    listView.verifyDualSort(sortTypeFirst, sortTypeSecond, primaryCol);
                                } else {
                                    Assert.fail(FAIL + "
------------------------------------------"
                                            + "
Reason: Second column is NOT sorted in descending order."
                                            + "
Discovered first column order: " + firstColumnEntries
                                            + "
Discovered second column order: " + secondColumnEntries);
                                }
                                    
                            }
                        }   
                    } 
                // second column is primary
                } else {
                    verifySecondColSort(secondColumnEntries, sortTypeSecond);
                    for (int i = 0; i < secondColumnEntries.size(); i++) {
                        String current2 = firstColumnEntries.get(i);
                        String current1 = secondColumnEntries.get(i);
                        if (i == 0 || current1.equals("")) continue;        
                        String previous2 = firstColumnEntries.get(i - 1);
                        String previous1 = secondColumnEntries.get(i - 1);
                        if (current2.equals(previous2)) {
                            // Sort type: 0 = ascending, 1 = descending
                            if (sortTypeFirst == 0 && current1.compareTo(previous1) < 0) {
                                if (dualSortAttempts < 10)  {
                                    dualSortAttempts++;
                                    System.out.print("DPA" + dualSortAttempts);
                                    listView.sortPrimaryColumnHeader();
                                    listView.verifyDualSort(sortTypeFirst, sortTypeSecond, primaryCol);
                                }
                                    Assert.fail(FAIL + "
------------------------------------------"
                                            + "
Reason: First column is NOT sorted in ascending order."
                                            + "
Discovered second column order: " + secondColumnEntries
                                            + "
Discovered first column order: " + firstColumnEntries);
                            } else if (sortTypeFirst == 1 && current1.compareTo(previous1) > 0 ){
                                if (dualSortAttempts < 10)  {
                                    dualSortAttempts++;
                                    System.out.print("DPS" + dualSortAttempts);
                                    listView.sortPrimaryColumnHeader();
                                    listView.verifyDualSort(sortTypeFirst, sortTypeSecond, primaryCol);
                                }
                                    Assert.fail(FAIL + "
------------------------------------------"
                                            + "
Reason: First column is NOT sorted in descending order."
                                            + "
Discovered second column order: " + secondColumnEntries
                                            + "
Discovered first column order: " + firstColumnEntries);
                            }
                        } 
                    } 
                }
        } catch (StaleElementReferenceException e) {
            // Standard element reassignment does not apply to Lists. 
            listView.verifyDualSort(sortTypeFirst, sortTypeSecond, primaryCol);
        }
        dualSortAttempts = 0;
    }

So far, this only works when validating that both columns are sorted in ascending order. However, when it tries to validate the first column as primary in ascending order and second column as secondary in descending order, it ends up caught in an infinite loop. Somehow the dualSortAttempts


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
152 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can concatenate the values an test if they are sorted like this:

package click.webelement.sandbox;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestSorting {

    public static void main(String[] args) {
        List<String> col1 = Arrays.asList("A", "C", "D", "D", "D", "E", "E");
        List<String> col2 = Arrays.asList("A", "C", "A", "B", "C", "A", "B");
        List<String> col1Reverse = Arrays.asList("E", "E", "D", "D", "D", "C", "C");
        List<String> col2Reverse = Arrays.asList("B", "A", "C", "B", "A", "B", "A");
        System.out.println(testSorting(col1Reverse, col2Reverse, 1));
        System.out.println(testSorting(col1Reverse, col2Reverse, 2));
        System.out.println(testSorting(col1, col2, 1));
        System.out.println(testSorting(col1, col2, 2));
    }

    public static boolean testSorting(List<String> col1, List<String> col2, int sortType){
        List<String> concat = new ArrayList<>();
        for(int i = 0; i < col1.size(); i++){
            concat.add(col1.get(i).concat("|").concat(col2.get(i)));
        }
        List<String> expected = new ArrayList(concat);
        Collections.sort(expected, sortType == 1 ? Collections.reverseOrder(String::compareTo) : String::compareTo);
        return concat.equals(expected);
    }

}

Output:

true
false
false
true

I concatenate the values with | not to mix up the values like AB|C and A|BC.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...