How to Verify DropDown Values Are In Sorting Order Using Java Selenium?

Verify DropDown Values Using Selenium

How to Verify Drop Down Values Are In Sorting Order Using Java Selenium?

package com.selenium.mix;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DropDownListSortedOrNot {
   public void dropDownListSortedOrNot() throws InterruptedException {
      WebDriver driver = new FirefoxDriver();
      WebElement element = driver.findElement("gh-cat"));;
      List<WebElement> dropDownvalues = element.findElements(By.tagName("option"));
      ArrayList<String> listValues = new ArrayList<String>();
      for(WebElement value : dropDownvalues) {
         System.out.println("values are"+ value.getText());
      boolean sortedOrNot = sortedOrNot(listValues);
      assertEquals(true, sortedOrNot);

   public boolean sortedOrNot(ArrayList<String> dropDownValues) {
      System.out.println("number of values "+ dropDownValues.size());
      for(int i=0; i<dropDownValues.size();i++) {
         int temp = dropDownValues.get(i).compareTo(dropDownValues.get(i+1));
         if(temp>1) {
            System.out.println("i value"+i);
            return false;
      return true; 

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to, or You can join me on Linkedin.

2 thoughts on “How to Verify DropDown Values Are In Sorting Order Using Java Selenium?”

  1. Good to see the programs on different concepts of selenium. But some explanation in comments would help to understand the logic with more understanding

    • Hi Swapna, Thanks For Your suggestion we will definitely try to add them in the program for better understanding the programs.


Leave a Comment