如何比较下拉选项是否与Selenium WebDriver中的UI选项相匹配?

时间:2014-03-26 03:36:32

标签: java selenium selenium-webdriver webdriver

  • 目前正在使用 Selenium WebDriver 并使用 Java 进行编写脚本。

  • 我已经在属性文件中存储了db的所有下拉值,并且想要比较相同的值,无论它们是否在UI中,如同在DropDown选项中一样。

  • C:目录中的 visualization.txt 包含以下选项 visualizationId =日,周,月,季,学期,年,RD Tech Group,ICC,Center,Software Pack,Product,Project,Customer PRs,Severity,Priority

那么如何比较两个值是否匹配。我需要从属性文件中获取所有下拉选项,即visualization.txt,然后需要检查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>

2 个答案:

答案 0 :(得分:1)

尝试以下代码。应该有所帮助:

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");
}

答案 1 :(得分:0)

//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");
   }
}