0

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]

Different 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]

Different order:

A A, A A

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)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Typ
  • 29
  • 3
  • How did you retrieve data from website ? Would you share your code for that ? – cruisepandey May 07 '21 at 15:40
  • Hi I added code. Code similar to 3th answer from: – Typ May 07 '21 at 16:34
  • [link](https://stackoverflow.com/questions/36950061/how-to-check-webelements-in-webtable-is-sorted-alphabetically-using-selenium-web) – Typ May 07 '21 at 16:40
  • if you want them to match, just run the gathered list through your collections.sort method. Seems like they are doing a toLower or toUpper in their comparator... on the web site. That might be by design, but seems like a bug to me. – pcalkins May 07 '21 at 16:56
  • Hi, I did that too is in the code in my post. – Typ May 07 '21 at 17:05

1 Answers1

0

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.");
arpita biswas
  • 144
  • 1
  • 6
  • Hello, thanks for answer, @arpita biswas i think we have similar method, I use assert, you use boolean. – Typ May 10 '21 at 09:22
  • So it means in the website-it is not maintaining a sorted order list. So I think you are able to verify whether the list is sorted or not. So what you are looking for now? – arpita biswas May 10 '21 at 12:05
  • I thought my sort was bad compared to page sort and I wanted to use e.g. comparator to equals them – Typ May 10 '21 at 14:59