如何修正动态表格单双位数数字的升序排序验证问题
Hey there, the problem you're facing is a classic case of lexicographical (string) sorting vs. numeric sorting. When you use String.compareTo(), it compares characters one by one based on their Unicode values—so "8" comes before "22" because the character '8' has a higher Unicode value than '2'. That's why your sorted output looks messed up for multi-digit numbers.
Here's how to fix this step by step:
1. Store Values as Numbers, Not Strings
Instead of saving the table cell text as strings, convert them directly to integers (or longs if dealing with very large numbers). This way, we can sort by actual numeric value.
2. Adjust Sorting Logic for Numeric Values
Use Java's collection sorting utilities designed for numbers, not strings.
3. Validate Against Numeric Values
When checking if the table is sorted correctly, compare the numeric values instead of raw strings.
Corrected Code Example
// Step 1: Extract numeric values from table cells and store in an Integer list List<Integer> originalNumbers = new ArrayList<>(); List<WebElement> numberCells = driver.findElements(By.xpath("//mat-table//mat-row/mat-cell[2]")); // Only locate elements once (no need to re-find in the loop!) for (WebElement cell : numberCells) { String cellText = cell.getText().trim(); Reporter.log(AddRule + " Column as per display order " + cellText); Add_Log.info(AddRule + " Column as per display order " + cellText); // Convert text to integer and add to list originalNumbers.add(Integer.parseInt(cellText)); } // Step 2: Create a sorted copy of the list // For ASCENDING order: List<Integer> sortedAscending = new ArrayList<>(originalNumbers); Collections.sort(sortedAscending); // For DESCENDING order (if you need to validate that instead): // List<Integer> sortedDescending = new ArrayList<>(originalNumbers); // Collections.sort(sortedDescending, Collections.reverseOrder()); // Print sorted values for debugging System.out.println("##################Sorted values in the Array and compare order####################"); for (Integer num : sortedAscending) { System.out.println(num); } // Step 3: Validate the table's order against the sorted list boolean isSortedCorrectly = true; for (int i = 0; i < originalNumbers.size(); i++) { int actualNumber = Integer.parseInt(numberCells.get(i).getText().trim()); int expectedNumber = sortedAscending.get(i); if (actualNumber != expectedNumber) { String errorMsg = String.format("%s Column is not in order. Expected: %d, Actual: %d", AddRule, expectedNumber, actualNumber); Reporter.log(errorMsg); Add_Log.info(errorMsg); isSortedCorrectly = false; break; // Stop checking further once a mismatch is found } else { String successMsg = String.format("%s %d Column is displayed in ascending order", AddRule, actualNumber); Reporter.log(successMsg); Add_Log.info(successMsg); } } // Fail the assertion if sorting is incorrect if (!isSortedCorrectly) { Assert.fail(AddRule + " Column is not sorted in ascending order as expected"); }
Key Improvements & Notes
- Avoid Repeated Element Locating: Your original code called
findElementsinside the loop—this is inefficient and can cause issues if the DOM changes. We locate elements once and reuse the list. - Handle Edge Cases: If your table cells have non-numeric characters (like "Item 12"), you'll need to extract just the numeric part first (e.g., using regex:
cellText.replaceAll("[^0-9]", "")). - Large Numbers: If your values exceed the
Integerrange, switch toLong.parseLong()instead ofInteger.parseInt().
内容的提问来源于stack exchange,提问作者ASSS

