testing
testing

Reputation: 1796

How to compare the drop down options is matching with the UI options in Selenium WebDriver?

So How can I compare both values are matching. I need to get all the drop down options from property file i.e visualization.txt then need to check in the drop drop down in UI.

enter image description here

<select id="visualizationId" style="width: 120px; display: none;" name="visualization">
<option value="day">Day</option>
<option value="week">Week</option>
<option selected="" value="month">Month</option>
<option value="quarter">Quarter</option>
<option value="semester">Semester</option>
<option value="year">Year</option>
<option value="techgroup">RD Tech Group</option>
<option value="icc">ICC</option>
<option value="center">Center</option>
<option value="softwarepack">Software Pack</option>
<option value="product">Product</option>
<option value="project">Project</option>
<option value="customer">Customer PRs</option>
<option value="severity">Severity</option>
<option value="priority">Priority</option>
</select>

Upvotes: 0

Views: 7531

Answers (2)

Sitam Jana
Sitam Jana

Reputation: 3129

Try the following piece of code. Should help:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

BufferedReader in = new BufferedReader(new FileReader("C:\\visualization.txt"));
String line;
line = in.readLine();
in.close();

String[] expectedDropDownItemsInArray = line.split("=")[1].split(",");

// Create expected list :: This will contain expected drop-down values
ArrayList expectedDropDownItems = new ArrayList();
for(int i=0; i<expectedDropDownItemsInArray.length; i++)
    expectedDropDownItems.add(expectedDropDownItemsInArray[i]);

// Create a webelement for the drop-down
WebElement visualizationDropDownElement = driver.findElement(By.id("visualizationId"));

// Instantiate Select class with the drop-down webelement
Select visualizationDropDown = new Select(visualizationDropDownElement);

// Retrieve all drop-down values and store in actual list
List<WebElement> valuesUnderVisualizationDropDown  = visualizationDropDown.getOptions();

List<String> actualDropDownItems = new ArrayList();

for(WebElement value : valuesUnderVisualizationDropDown){
    actualDropDownItems.add(value.getText());
}

// Print expected and actual list
System.out.println("expectedDropDownItems : " +expectedDropDownItems);       
System.out.println("actualDropDownItems : " +actualDropDownItems);

// Verify both the lists having same size
if(actualDropDownItems.size() != expectedDropDownItems.size())
  System.out.println("Property file is NOT updated with the drop-down values");

// Compare expected and actual list
for (int i = 0; i < actualDropDownItems.size(); i++) {
    if (!expectedDropDownItems.get(i).equals(actualDropDownItems.get(i)))
    System.out.println("Drop-down values are NOT in correct order");
}

Upvotes: 1

Santoshsarma
Santoshsarma

Reputation: 5667

//Read the data from property file

String options = property.getProperty("visualizationId");

//read the options from drop down in UI
List<WebElement> dropdownOptions = new Select(driver.findElement(By.id("visualizationId"))).getOptions();
String uiOptions="";
for(WebElement eachOption : dropdownOptions ) {
   uiOptions+=eachOption.getText()+","; 
   if(!options.contains(eachOption.getText())) {
         System.out.println(eachOption.getText()+" is present in UI but not in property file");
   }
}


for(String eachOptionFromPropertyFile : options.split(",")){
   if(!uiOptions.contains(eachOptionFromPropertyFile)) {
        System.out.println(eachOptionFromPropertyFile+" is present in property file but not in UI");
   }
}

Upvotes: 0

Related Questions