问题
I have 3 elements and they have 3 names with last names its a button. I need to figure out how to check if they sorted by first name and then by last name.Page object for elements is ListV_Sorted. I think I just sorted by first name elements. 0 element = Jack Daniels, 1 element = John Fera, 2 element = Mike Durov. ListV is a page object for the elements. It says expected true but found false.
//Verifies if APs Names Sorted Alphabetically @SuppressWarnings("unchecked") public void sortingOrder() {
Log.log(driver).info("About to Alphebetically Sort");
List<MobileElement> products_MobileElements = new LinkedList<MobileElement>();
products_MobileElements = (List<MobileElement>) TicketPassesNames;
LinkedList<String> product_names = new LinkedList<String>();
for(int i=0;i<products_MobileElements.size();i++){
String s = products_MobileElements.get(i).getAttribute("checked");
String[] tokens = s.split("");
String firstName = "";
String lastName = "";
if(tokens.length > 0) {
firstName = tokens[0];
lastName = tokens[tokens.length -1];
product_names.add(s);
product_names.add(firstName);
product_names.add(lastName);
}
}
boolean result = checkAlphabeticalOrder(product_names);
Assert.assertEquals(checkAlphabeticalOrder(product_names), true);
Log.log(driver).info("Tickest Passes names are in alphabetical order.");
System.out.println(result);
}
//Method takes a String to Sort AlphabeticalLy
public static boolean checkAlphabeticalOrder(LinkedList<String> product_names){
String previous = ""; // empty string
for (final String current: product_names) {
if (current.compareTo(previous) < 0)
return false;
previous = current;
}
return true;
}
来源:https://stackoverflow.com/questions/46419675/i-get-an-error-when-trying-to-sort-first-name-alphabetically-and-sort-last-name