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:
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