How to Check Drop down options are sorted or not in selenium with Java


In this post, I'm going to explain how to check Drop down options are sorted or not in selenium webdriver with java.
  • Open the browser and navigate to the webpage
  • Find the dropdown using the findElement method in selenium
  • Create a object to Select class and pass the dropdown element as the parameter to constructor
WebElement element = driver.findElement(By.xpath("//*[@id=\"animals\"]"));
Select se = new Select(element);
  • Using getOptions() method from Select class you can get all the options from the dropdown in the form of WebElement.
  • Using the loop we can retrive the values from the List of WebElement
  • Add all the values into a list called originalList that we have already created
ArrayList originalList = new ArrayList();

for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
}
  • The values we retrieved could be sorted or not sorted values [ we are not sure, we have to verify this]
  • Now lets create a temporary list alled tempList and get the values from originalList
  • Now sort the Either tempList or originalList and compare them, We can sort the list using the Collections.sort(list) method
ArrayList tempList = originalList;
Collections.sort(tempList); 
  • We can compare the list using conditional statement
Complete program for not working sorting

import java.util.ArrayList;
import java.util.Collections;
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 VerifyDropDownSortedOptions {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://testautomationpractice.blogspot.com/");

WebElement element = driver.findElement(By.xpath("//*[@id=\"animals\"]"));
Select se = new Select(element);

ArrayList originalList = new ArrayList();

for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
}
System.out.println("originalList:" + originalList);

ArrayList tempList = originalList;
Collections.sort(tempList); // When you change one list, it changes the other list as well.

System.out.println("originalList:" + originalList);
System.out.println("tempList:" + tempList);

/*So the test gets pass all the time because the sequence in the originalList
and tempList is going to be same.
If you are following above process then your test never fails, because When
you change one list, it changes the other list as well.*/

if (originalList == tempList) {
System.out.println("Dropdown sorted");
} else {
System.out.println("Dropdown NOT sorted");
}
driver.close();
}
}

So the test gets pass all the time because the sequence in the originalList and tempList is going to be same.

If you are following above process then your test never fails, because When you change one list, it changes the other list as well.

How to check the options in drop down are sorted order or not

Step1: Create a List tempList variable
Step2: While iterating the option in the dropdown, add values to tempList (along with originalList)
Step3: Now sort the tempList, sorting of tempList will not affect the originalList because we have created two different objects
Step4: Compare the two Lists

import java.util.ArrayList;
import java.util.Collections;
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 VerifyDropDownSortedOptions {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://testautomationpractice.blogspot.com/");

driver.manage().window().maximize();

WebElement element = driver.findElement(By.id("animals"));

Select se = new Select(element);

ArrayList originalList = new ArrayList();
ArrayList tempList = new ArrayList();

for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
tempList.add(e.getText());
}

System.out.println("this is originalList before Sorting" + originalList);
System.out.println("this is tempList before Sorting" + tempList);

Collections.sort(tempList);

System.out.println("this is originalList after Sorting" + originalList);
System.out.println("this is tempList after Sorting" + tempList);

if (originalList == tempList) {
System.out.println("Dropdown sorted");
} else {
System.out.println("Dropdown Not sorted");

}
driver.close();
}

}



Followers