One of our readers has asked, how to compare values of dropdowns ?, Let's see how we are going to compare the dropdown values in selenium.
I hope you are aware of the Select class in selenium, which handles the standard dropdowns; if not, please read Select class in selenium.
In the below-given page, the dropdowns are going to be in the same order, but there could be a missing option. (below dropdowns have all option same as one another)
WebElement originalDropdown = driver.findElement(By.cssSelector("select#first"));
Select original = new Select(originalDropdown);
getOptions() methods from Select class objectgetOptions() method will return the list of options/values present in the dropdown. The order of the dropdown purely depends on the developer of that dropdownoriginalListElementsList<WebElement> originalListElements = original.getOptions();
originalListList<String> originalList = new ArrayList<String>();
for (WebElement webElement : originalListElements) {
originalList.add(webElement.getText());
}
targetListoriginalList and targetList using assertions present in TestNGAssert.assertEquals(originalList, targetList);
The complete code for comparing dropdowns
@Test
public void verifyDropdowns() {
System.setProperty("webdriver.chrome.driver", "D:PATHchromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://chercher.tech/practice/dropdowns");
// original dropdown
WebElement originalDropdown = driver.findElement(By.cssSelector("select#first"));
Select original = new Select(originalDropdown);
List<WebElement> originalListElements = original.getOptions();
List<String> originalList = new ArrayList<String>();
for (WebElement webElement : originalListElements) {
originalList.add(webElement.getText());
}
// target dropdown
WebElement targetDropdown = driver.findElement(By.id("order-same"));
Select target = new Select(targetDropdown);
List<WebElement> targetListElements = target.getOptions();
List<String> targetList = new ArrayList<String>();
for (WebElement webElement : targetListElements) {
targetList.add(webElement.getText());
}
Assert.assertEquals(originalList, targetList);
}
Yes, I know you guys are looking for real things. In a practical application, you cannot see ideal things. So most of the people might have come across dropdowns that have the same values but in different/random order.
Content in the below dropdowns are going to be the same but in a different order
Let me use the above-written code for verifying this scenario,
@Test
public void verifyDropdowns() {
System.setProperty("webdriver.chrome.driver", "D:PATHchromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://chercher.tech/practice/dropdowns");
// original dropdown
WebElement originalDropdown = driver.findElement(By.cssSelector("select#first"));
Select original = new Select(originalDropdown);
List<WebElement> originalListElements = original.getOptions();
List<String> originalList = new ArrayList<String>();
for (WebElement webElement : originalListElements) {
originalList.add(webElement.getText());
}
// target dropdown
WebElement targetDropdown = driver.findElement(By.id("order-changed"));
Select target = new Select(targetDropdown);
List<WebElement> targetListElements = target.getOptions();
List<String> targetList = new ArrayList<String>();
for (WebElement webElement : targetListElements) {
targetList.add(webElement.getText());
}
Assert.assertEquals(originalList, targetList);
}
TestNG is not intelligent enough to correct it, so we got to do it in hard way
In this case, the above solution will not be helpful as it passes just both lists possess the same elements irrespective of order.
getOptions() methods from the Select class objectgetOptions() method will return the list of options/values present in the dropdown. The order of the dropdown purely depends on the developer of that dropdownoriginalListElementsoriginalListtargetListThe complete code to compare two randomly ordered dropdowns.
@Test
public void verifyDropdowns() {
System.setProperty("webdriver.chrome.driver", "D:PATHchromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://chercher.tech/practice/dropdowns");
// original dropdown
WebElement originalDropdown = driver.findElement(By.cssSelector("select#first"));
Select original = new Select(originalDropdown);
List<WebElement> originalListElements = original.getOptions();
List<String> originalList = new ArrayList<String>();
for (WebElement webElement : originalListElements) {
originalList.add(webElement.getText());
}
// target dropdown
WebElement targetDropdown = driver.findElement(By.id("order-changed"));
Select target = new Select(targetDropdown);
List<WebElement> targetListElements = target.getOptions();
List<String> targetList = new ArrayList<String>();
for (WebElement webElement : targetListElements) {
targetList.add(webElement.getText());
}
Assert.assertEquals(originalList.size(), targetList.size());
for (String string : originalList) {
targetList.remove(string);
}
Assert.assertTrue(targetList.isEmpty());
}
Let me end this article with simple Questions.
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.