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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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
Author: Amod Mahajan
A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.
how can i remove duplicate values from dropdown
list webElement
How can I used same code for List unable to figure please help