在JAVA中同时验证两列的排序顺序
我正在与Selenium合作,从一个包含两列的表中获取行数据,并尝试验证它们在按升序和降序排序后是否对其列应用了正确的顺序。验证单列排序的代码如下:
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 + "\n------------------------------------------"
+ "\nReason: First column is NOT sorted in ascending order."
+ "\nDiscovered order: " + firstColumnEntries);
} else {
Assert.fail(FAIL + "\n------------------------------------------"
+ "\nReason: First column is NOT sorted in descending order."
+ "\nDiscovered 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 + "\n------------------------------------------"
+ "\nReason: Second column is NOT sorted in ascending order."
+ "\nDiscovered order: " + secondColumnEntries);
} else {
Assert.fail(FAIL + "\n------------------------------------------"
+ "\nReason: Second column is NOT sorted in descending order."
+ "\nDiscovered 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;
}
如果您想知道为什么我需要跟踪sortAttempts
,那是因为在应用每个排序之后,表模块必须重新加载。据我所知,Selenium无法将这种情况考虑在内。因此,有时应用排序顺序的单击操作会过早触发。Selenium没有意识到这一点,所以我必须自己进行补偿,迫使它重复点击操作几次,以防验证失败是由于这个问题造成的。这很烦人,但我没有建立网站。我只是测试一下
在任何情况下,这都适用于单个列排序验证。当我需要同时验证两列时,问题就出现了。当两列按升序排序时,第一列被指定为“主要”排序,第二列被指定为“次要”排序
例如,如果两列都按升序排序,第一列作为主要排序,则情况如下:
我一直在努力寻找一种好方法来验证这样的双重排序。我尝试过多种复杂的方法,但到目前为止,没有一种方法能完美地实现这一点。不幸的是,这似乎是一个如此独特的场景,以至于我无法研究这个问题的另一个例子。我花了这么长时间研究它,我觉得自己好像是在试图重新发明轮子。目前,这是双重排序验证的代码(是的,我知道这很混乱):
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 + "\n------------------------------------------"
+ "\nReason: Second column is NOT sorted in ascending order."
+ "\nDiscovered first column order: " + firstColumnEntries
+ "\nDiscovered 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 + "\n------------------------------------------"
+ "\nReason: Second column is NOT sorted in descending order."
+ "\nDiscovered first column order: " + firstColumnEntries
+ "\nDiscovered 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 + "\n------------------------------------------"
+ "\nReason: First column is NOT sorted in ascending order."
+ "\nDiscovered second column order: " + secondColumnEntries
+ "\nDiscovered 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 + "\n------------------------------------------"
+ "\nReason: First column is NOT sorted in descending order."
+ "\nDiscovered second column order: " + secondColumnEntries
+ "\nDiscovered first column order: " + firstColumnEntries);
}
}
}
}
} catch (StaleElementReferenceException e) {
// Standard element reassignment does not apply to Lists.
listView.verifyDualSort(sortTypeFirst, sortTypeSecond, primaryCol);
}
dualSortAttempts = 0;
}
到目前为止,这仅在验证两列是否按升序排序时有效。然而,当它试图以升序验证第一列为主要列,以降序验证第二列为次要列时,它最终陷入了一个无限循环。第三次重新单击后,dualSortAttempts
计数器会被重置,尽管无法验证二级排序顺序:
这就是我目前的困境。如果代码可以修改以工作,那就太好了。但如果有一个更简单的解决方案,我只是不知道,这将是更好的
# 1 楼答案
如果测试中的值按如下顺序排列,则可以将它们连接起来:
输出:
我将这些值与
|
连接起来,以避免将AB|C
和A|BC
这样的值混淆