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 {
   @Test
   public void dropDownListSortedOrNot() throws InterruptedException {
      WebDriver driver = new FirefoxDriver();
      driver.get("http://www.ebay.in/");
      Thread.sleep(3000);  
      WebElement element = driver.findElement(By.id("gh-cat"));
      element.click();
      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());
         listValues.add(value.getText());
      }
      boolean sortedOrNot = sortedOrNot(listValues);
      assertEquals(true, sortedOrNot);
      driver.close();

   }
   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 softwaretestingo.com@gmail.com, 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

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

      Reply

Leave a Comment