How To Check Dropdown Options are Sorted or not in Selenium

In this post, we will learn How to check dropdown options are sorted or not in Selenium Webdriver, also we will see the concept of Verify dropdown empty or not in selenium etc.

In this post, we will learn How to check dropdown options are sorted or not in Selenium Webdriver, also we will see the concept of Verify dropdown empty or not in selenium etc.

How To Check Dropdown Options are Sorted or not

Steps: To check dropdown options sorted or not:

  1. Open browser and navigate to URL.
  2. Create object of Select class and pass dropdown locator.
  3. Using getOptions() method get all dropdown options and store into List.
  4. Using for-each loop fetch all options one by one and get text using getText() method.
  5. Create empty List and store copy original list into it.
  6. Sort Original list using sort() method of Collections class.
  7. Compare sorted Original list with temp list using equals() method.

Code:

package SeleniumExp;

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 CheckDropdownSorted {
    
    public static void main(String[] args) {
        
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.twoplugs.com/newsearchserviceneed");
        driver.manage().window().maximize();
        
        Select options = new Select(driver.findElement(By.name("category_id")));
        
        // Get all options from category dropdown
        List<WebElement> lists = options.getOptions();
        
        //Creating a List to store all dropdown values
        List OriginalList = new ArrayList();
        
        //Using for-each loop fetch all options one by one and get their values
        for(WebElement ele : lists) {
            
            OriginalList.add(ele.getText());
        }
        
        System.out.println("Options in dropdown: " +OriginalList);
        
        //Creating temp List - same copy of Original for comparing purpose
        List templist = new ArrayList(OriginalList);
        
        System.out.println("Temp List: " + templist);
        
        //Sorting the Original List 
        Collections.sort(OriginalList);
        System.out.println("Sorted List:" + OriginalList );
        
        
        // In IF Condition we will compare both List
        if(OriginalList.equals(templist)) {
            
            System.out.println("List is Sorted");
            
        }else {
            
            System.out.println("List is unsorted");
        }
        
        driver.quit();
        
    }

}

Output:

Dropdown Options are Sorted or not


How To check dropdown is empty or not in selenium

Steps:

  1. Open browser and navigate to URL.
  2. Create object of Select class and pass dropdown locator.
  3. Using getOptions() method get all dropdown options and store into List.
  4. Using isEmpty() method, In IF condition we check List is empty or not.

Code:

package SeleniumExp;

import java.util.List;

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

public class CheckDropdownEmpty {
    
    public static void main(String[] args) {
        
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.twoplugs.com/newsearchserviceneed");
        driver.manage().window().maximize();
        
        Select options = new Select(driver.findElement(By.name("category_id")));
        
        List Alloptions = options.getOptions();
        
        if(Alloptions.isEmpty()) {
        	
        	System.out.println("Dropdown is empty");
        	
        }else {
        	
        	System.out.println("Dropdown has options");
        }
        
    }

}



FAQ in Interviews

Q1. How to you validate a dropdown value?

Ans. First we will select the dropdown option using selectByValue() or selectByVisibleText() or selectByIndex() method. After this we will get text of first selected option using below code and compare both.

String getText = select.getFirstSelectedOption().getText();

Note: getFirstSelectedOption() is used to get first selected option in dropdown.

 Q2. How to get all the options in the dropdown in Selenium?

Ans. Using getOptions() method of Select class we can get all dropdown options from dropdown. It will return a list of web elements.

Q3. Which option in the dropdown is selected in Selenium?

Ans. getFirstSelectedOption() is used to get selected option in dropdown.

You should also read this article – How to Select dropdown without using Select class

I hope you enjoyed this post. Happy Learning 🙂

 

Leave a Comment