Join us on :

Dropdown values are sorted or not in selenium

Frankly, I donot wanted to write this kind of article but I have Read it in a blog about the verify whether option are sorted in the dropdown or not ? and it was completely wrong. So I wanted to provide teh right way(s) to do it.

Let's write a simple way (as mentioned in other blog):

Collections.sort() || Read completely

  • Open the browser and navigate to the webpage
  • Find the dropdown using the findElement method in selenium
  • Create a object to Select class and pass the dropdown element as the parameter to constructor
  • 
    WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
    Select se = new Select(element);
    

  • Using getOptions() method from Select class you can get all the options from the dropdown in the form of WebElement.
  • Using the loop we can retrive the values from the List of WebElement
  • Add all the values into a list called originalList that we have already created
  • 
    List<String> originalList = new ArrayList();
    for (WebElement e : se.getOptions()) {
    	originalList.add(e.getText());
    }
    

  • The values we retrieved could be sorted or not sorted values [ we are not sure, we have to verify this]
  • Now lets create a temporary list alled tempList and get the values from originalList
  • Now sort the Either tempList or originalList and compare them, We can sort the list using the Collections.sort(list) method
  • 
    List<String> tempList= originalList;
    Collections.sort(tempList);
    

  • We can compare the list using the Assertion class in TestNG
Complete program for not working sorting

public class TestDrpdownSorted {
    @Test
    public void runTestOnDocker() throws Exception {
			String driverPath = "D:\\PATH\\chromedriver.exe";
			System.setProperty("webdriver.chrome.driver", driverPath);  

			WebDriver driver = new ChromeDriver();
			driver.get("https://chercher.tech/practice/practice-dropdowns-selenium-webdriver");

			WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
			Select se = new Select(element);
			List<String> originalList = new ArrayList();
			for (WebElement e : se.getOptions()) {
				originalList.add(e.getText());
			}
			//----logic block starts
			List<String> tempList= originalList;
			Collections.sort(tempList);   
			Assert.assertEquals(tempList, originalList);
			//----logic ends starts
    }
}

Output of the program, Everything works fine dropdown-sorted-options-selenium

This was the explanation/solution given in the other blog

Lets bring the twist :

Below is the screenshot of the dropdown dropdown-not-sorted-list-selenium

Now, you might think, where did the mistake happened. Lets bring some print statements in logic block


System.out.println("\n this is originalList before Sorting tempList"+ originalList);
Collections.sort(tempList);
System.out.println("\n this is originalList after sorting tempList"+ originalList);
System.out.println("\n this is tempList"+ tempList);
Assert.assertEquals(tempList, originalList);

Output of the program: shallow-copy-dropdown-selenium

If you notice above underlines in the screenshot, we see the change in the Original list i.e original list got sorted but at the same time we have not sorted the original list

So the test gets pass all tthe time because the sequcence in the originalList and tempList is going to be same.

If you are following above process then your test never fails, because When you change one list, it changes the other list as well.

Dropdown Options are sorted or Not ?

Subscribe to my youtube channel :


how do you check the elements in dropdown are sorted in ascending order or not

We have few ways to verify whether options are sorted or not in dropdowns with webdriver.

  • For loop [ basic]
  • addAll()
  • TreeSet
  • stream().collect()
1. For loop

  • Create a List tempList variable
  • While iterating the option in the dropdown, add values to tempList (along with originalList)
  • Now sort the tempList, sorting of tempList will not affect the originalList because we have created two different objects
  • Compare the two Lists

	WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
	Select se = new Select(element);
	List<String> originalList = new ArrayList();
	List<String> tempList = new ArrayList();
	for (WebElement e : se.getOptions()) {
		originalList.add(e.getText());
		tempList.add(e.getText());
	}
	
	System.out.println("\n this is originalList before Sorting tempList"+ originalList);
	Collections.sort(tempList);
	System.out.println("\n this is originalList after sorting tempList"+ originalList);
	System.out.println("\n this is tempList"+ tempList);
	Assert.assertEquals(tempList, originalList);

Now the test result is failure, because dropdown options are not sorted different-values-in-dropdown-webdriver
If you are thinking like, why cannot I copy the originalList elements using another loop, then surely you can do but you will be using two loops in the place of one loop. This method is not recommended
2. addAll()
I want you guys, to give a try to use addAll() on a newly created list and achieve the same above results [leave comment if you know/donot know the answer]
3. TreeSet
Before we proceeed with te process lets see the trait of Treelist:
The objects of the TreeSet class are stored in ascending order

The TreeSet stores the objects baased on the comparator provided, if there is no comparator then it is stored in ascending order.

We can use the TreeSet for verification dropdown option order, we have to create an Treeset object using the list as parameter for it's constructor.

Compare the values of the treeSet and the List using Assert methods

Assert methods accepts Iteratable objects, so now you shoud stop worrying about how you can compare Set and List

WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
Select se = new Select(element);
List<String> originalList = new ArrayList();
for (WebElement e : se.getOptions()) {
	originalList.add(e.getText());
}

System.out.println("\n this is originalList before Sorting tempList"+ originalList);

Set<String> treeset = new TreeSet(originalList);
System.out.println("\n this is TreeSet "+ treeset);
System.out.println("\n this is originalList after sorting tempList"+ originalList);

Assert.assertEquals(treeset, originalList);

Output of the execution treeset-list-compare-dropdown-options
4. stream().collect()

We can use the stream() method along with collect() method to create new List object with elements


WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
Select se = new Select(element);
List<String> originalList = new ArrayList();
for (WebElement e : se.getOptions()) {
	originalList.add(e.getText());
}

List<String> tempList = originalList.stream().collect(Collectors.toList());
System.out.println("\n this is originalList before Sorting tempList"+ originalList);
System.out.println("\n this is tempList"+ tempList);
Collections.sort(tempList);
System.out.println("\n this is originalList after Sorting tempList"+ originalList);
System.out.println("\n this is tempList"+ tempList);
Assert.assertEquals(tempList, originalList);

Output of the program to verify dropdown option order in selenium stream-collect-java-selenium-dropdown

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions