Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How to Verify if Options in Dropdown are Sorted as Expected In Selenium WebDriver – Java | Make Selenium Easy

Posted on 11/30/2018 By admin

Hello Guys,

As I say always, your automation script is ineffective if you do not include logic to validate to make sure the desired action has been performed by Selenium script. For Example – You have a dropdown in your application. Manually you verify if dropdown contains desired options or it s sorted. Generally while automating same feature, we just select a value in dropdown and leave validating options in dropdown or if it is sorted. We will learn how can we achieve in Selenium-Java.

Infact, verifying dropdown contains sorted element require java code. Selenium can give only options in dropdown. What you need to validate on extracted options requires Java knowledge. As I say always, you can write better script if you are good in Java.

HTML for dropdown:



Select your language below:


Select
English
Hindi
Kannada
Malaylam
Tamil
Telgu

Java code to verify if options are sorted in ascending order:

package HandlingDropdown;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;


public class ValidationOfSortedDropdownAscending {

        public static void main(String[] args) throws InterruptedException {
                
                // Launching browser
                System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");
                WebDriver driver= new ChromeDriver();
                
                // Launching URL
                driver.get("file:///C:/Users/amodm/Desktop/SortedDropdown.html");
                
                // Locating select tag web element
                WebElement singleSelectTagDropdownWebElement= driver.findElement(By.id("SingleDD"));
                Select dropdown = new Select(singleSelectTagDropdownWebElement);
                
                // Get all options
                List allOptionsElement = dropdown.getOptions();
                
                // Creating a list to store drop down options
                List options = new ArrayList();
                
                // Storing options in list
                for(WebElement optionElement : allOptionsElement)
                {
                        options.add(optionElement.getText());
                }
                
                // Removing "Select" option as it is not actual option
                options.remove("Select");
                
                // Default order of option in drop down
                System.out.println("Options in dropdown with Default order :"+options);
                
                // Creating a temp list to sort
                List tempList = new ArrayList(options);
                
                
                // Sort list ascending
                Collections.sort(tempList);
                
                
                System.out.println("Sorted List "+ tempList);
                
                
                // equals() method checks for two lists if they contain the same elements in the same order.
                boolean ifSortedAscending = options.equals(tempList);
                
                if(ifSortedAscending)
                {
                        System.out.println("List is sorted");
                }
                else
                        System.out.println("List is not sorted.");
                
                
                driver.quit();
                
        }
}

Output:

Options in dropdown with Default order :[English, Hindi, Kannada, Malaylam, Tamil, Telgu]
Sorted List [English, Hindi, Kannada, Malaylam, Tamil, Telgu]
List is sorted

Java code to verify if options are sorted in descending order:

package HandlingDropdown;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;


public class ValidationOfSortedDropdownDescending  {

        public static void main(String[] args) throws InterruptedException {
                
                // Launching browser
                System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");
                WebDriver driver= new ChromeDriver();
                
                // Launching URL
                driver.get("file:///C:/Users/amodm/Desktop/SortedDropdown.html");
                
                // Locating select tag web element
                WebElement singleSelectTagDropdownWebElement= driver.findElement(By.id("SingleDD"));
                Select dropdown = new Select(singleSelectTagDropdownWebElement);
                
                // Get all options
                List allOptionsElement = dropdown.getOptions();
                
                // Creating a list to store drop down options
                List options = new ArrayList();
                
                // Storing options in list
                for(WebElement optionElement : allOptionsElement)
                {
                        options.add(optionElement.getText());
                }
                
                // Removing "Select" option as it is not actual option
                options.remove("Select");
                
                // Default order of option in drop down
                System.out.println("Options in dropdown with Default order :"+options);
                
                // Creating a temp list to sort
                List tempList = new ArrayList(options);
                
                
                // Sort list descending 
                Collections.sort(tempList, Collections.reverseOrder());
                
                
                System.out.println("Sorted List "+ tempList);
                
                
                // equals() method checks for two lists if they contain the same elements in the same order.
                boolean ifSortedAscending = options.equals(tempList);
                
                if(ifSortedAscending)
                {
                        System.out.println("List is sorted");
                }
                else
                        System.out.println("List is not sorted.");
                
                
                driver.quit();
                
        }
}

Output:

Options in dropdown with Default order :[English, Hindi, Kannada, Malaylam, Tamil, Telgu]
Sorted List [Telgu, Tamil, Malaylam, Kannada, Hindi, English]
List is not sorted.

If you have any doubt, feel free to comment below. If you like my posts, please like, comment, share and subscribe. #ThanksForReading

#HappySelenium

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Uncategorized

Post navigation

Previous Post: Launch of Firefox Browser through Selenium 3: | Make Selenium Easy
Next Post: TestNG Tutorials 1: Why Do We Require TestNG In Selenium? | Make Selenium Easy

Related Posts

java Uncategorized
How To Launch Firefox Browser in Selenium WebDriver – Java Uncategorized
console Error – Make Selenium Easy Uncategorized
findElement Uncategorized
REST Assured Tutorial 30 – How To Create POJO Classes Of A JSON Array Payload Uncategorized
TestNG Tutorials 2: Installation Of TestNG In Eclipse ==> Download And Add To Build Path Way | Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark