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

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:



<strong>Select your language below:</strong>

<select id="SingleDD">
<option value="Choose">Select</option>
<option value="English">English</option>
<option value="Hindi">Hindi</option>
<option value="Kannada">Kannada</option>
<option value="Malaylam">Malaylam</option>
<option value="Tamil">Tamil</option>
<option value="Telgu">Telgu</option>
</select>

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

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:

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

Author: Amod Mahajan

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