Frankly, I donot wanted to write this kind of article but I have Read it in a blog about the verify whether option are sorted in the dropdown or not ? and it was completely wrong. So I wanted to provide teh right way(s) to do it.
Let's write a simple way (as mentioned in other blog):
WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
Select se = new Select(element);
originalList that we have already created
List<String;> originalList = new ArrayList();
for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
}
tempList and get the values from originalListtempList or originalList and compare them, We can sort the list using the
List<String;> tempList= originalList;
Collections.sort(tempList);
public class TestDrpdownSorted {
@Test
public void runTestOnDocker() throws Exception {
String driverPath = "D:\\PATH\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
WebDriver driver = new ChromeDriver();
driver.get("https://chercher.tech/practice/practice-dropdowns-selenium-webdriver");
WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
Select se = new Select(element);
List<String;> originalList = new ArrayList();
for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
}
//----logic block starts
List<String;> tempList= originalList;
Collections.sort(tempList);
Assert.assertEquals(tempList, originalList);
//----logic ends starts
}
}
Output of the program, Everything works fine
Below is the screenshot of the dropdown
Now, you might think, where did the mistake happened. Lets bring some print statements in logic block
System.out.println("\n this is originalList before Sorting tempList"+ originalList);
Collections.sort(tempList);
System.out.println("\n this is originalList after sorting tempList"+ originalList);
System.out.println("\n this is tempList"+ tempList);
Assert.assertEquals(tempList, originalList);
Output of the program:
If you notice above underlines in the screenshot, we see the change in the Original list i.e original list got sorted but at the same time we have not sorted the original list
So the test gets pass all tthe time because the sequcence 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.
We have few ways to verify whether options are sorted or not in dropdowns with webdriver.
tempList will not affect the originalList because we have created two different objects
WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
Select se = new Select(element);
List<String;> originalList = new ArrayList();
List<String;> tempList = new ArrayList();
for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
tempList.add(e.getText());
}
System.out.println("\n this is originalList before Sorting tempList"+ originalList);
Collections.sort(tempList);
System.out.println("\n this is originalList after sorting tempList"+ originalList);
System.out.println("\n this is tempList"+ tempList);
Assert.assertEquals(tempList, originalList);
Now the test result is failure, because dropdown options are not sorted
The objects of the TreeSet class are stored in ascending order
The TreeSet stores the objects baased on the comparator provided, if there is no comparator then it is stored in ascending order.
We can use the TreeSet for verification dropdown option order, we have to create an Treeset object using the list as parameter for it's constructor.
Compare the values of the treeSet and the List using Assert methods
WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
Select se = new Select(element);
List<String;> originalList = new ArrayList();
for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
}
System.out.println("\n this is originalList before Sorting tempList"+ originalList);
Set<String;> treeset = new TreeSet(originalList);
System.out.println("\n this is TreeSet "+ treeset);
System.out.println("\n this is originalList after sorting tempList"+ originalList);
Assert.assertEquals(treeset, originalList);
Output of the execution
We can use the stream() method along with collect() method to create new List object with elements
WebElement element = driver.findElement(By.xpath("//select[@id='animals']"));
Select se = new Select(element);
List<String;> originalList = new ArrayList();
for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
}
List<String;> tempList = originalList.stream().collect(Collectors.toList());
System.out.println("\n this is originalList before Sorting tempList"+ originalList);
System.out.println("\n this is tempList"+ tempList);
Collections.sort(tempList);
System.out.println("\n this is originalList after Sorting tempList"+ originalList);
System.out.println("\n this is tempList"+ tempList);
Assert.assertEquals(tempList, originalList);
Output of the program to verify dropdown option order in selenium
Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know.
Knowledge is the only thing that doubles when you spend it.
I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation