简体   繁体   中英

Problem with sorting elements Selenium webdriver java

I have a problem with sorting web elements, sorted elements from the website are in a different order than elements from my sorted-list.

Result: My sorted String list:

[0 0, 1 1, A A, A A, A A, AAAA, AAAA, Cascasc Aaaaa, Jan Jankowski, Jan Janowski, Jan Kon, Jan kowalski, pp]

String List from website:

[0 0, 1 1, A A, A A, A A, AAAA, AAAA, Cascasc Aaaaa, Jan Jankowski, Jan Janowski, Jan kowalski, Jan Kon, pp]

difernt order: Jan Kon, Jan kowalski

My sorted String list:

[0 0, 464 464, A A, A A, a a, a a, a a, a b, a s, aa a, abv sada, ala Janka, ala ola, anna, anna, anna, bankowa]

List from website:

[0 0, 464 464, a a, a a, a a, a b, a s, aa a, abv sada, ala Janka, ala ola, anna, anna, anna, bankowa, A A, A A]

difernt order: AA, AA

Code:

getObtainedList(String css){
    ArrayList<String> obtainedList = new ArrayList<>(); 
    List<WebElement> elementList= driver.findElements(By.CssSelector(css));
     for(webElement we:elementList){
       obtainedList.add(we.getText);
     }
    }
    
    getSortedList(List list){
    ArrayList<String> sortedList = new ArrayList<>();   
     for(String s:list){
     sortedList.add(s);
     }
    }


    List<String> obtainedList = getObtainedList(cssSelector);
    
    List<String> sortedList = getSortedList(obtainedList);
    Collections.sort(sortedList)
    
    Assert.assertEquals(sortedList, obtainedList)

Please try the below code:

ArrayList<String> obtainedList = new ArrayList<>(); 
// Get all options
List<WebElement> elementList= driver.findElements(By.CssSelector(css));

// Creating a list to store drop down options
List options = new ArrayList();


for(WebElement optionElement : elementList)
{
    obtainedList.add(optionElement.getText());
}

  
System.out.println("Options in dropdown with Default order :"+obtainedList);

// Creating a temp list to sort
List tempList = new ArrayList(obtainedList);

Collections.sort(tempList);

System.out.println("Sorted List "+ tempList);

boolean ifSortedAscending = obtainedList.equals(tempList);

if(ifSortedAscending)
{
    System.out.println("List is sorted");
}
else
    System.out.println("List is not sorted.");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM