【问题标题】:How to compare the drop down options is matching with the UI options in Selenium WebDriver?如何比较下拉选项是否与 Selenium WebDriver 中的 UI 选项匹配?
【发布时间】:2014-05-04 06:22:24
【问题描述】:
  • 目前正在开发 Selenium WebDriver 并使用 Java 编写脚本。

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

  • C: 目录下的 visualization.txt 包含以下选项 visualizationId=Day,Week,Month,Quarter,Semester,Year,RD Tech Group,ICC,Center,Software Pack,Product,Project,Customer PRs,Severity,Priority

那么我如何比较两个值是否匹配。我需要从属性文件(即可视化.txt)中获取所有下拉选项,然后需要检查 UI 中的下拉列表。

<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>

【问题讨论】:

  • 你给出的例子是不同的,你能推荐几个确切的例子吗
  • 你能发布提到的下拉列表的 html sn-p 吗??
  • 我已经发布了上面的 HTML。请给我一些解决方案
  • 在我看来,mfsi_sitamj 提供的链接是您要求的解决方案。没有人会拥有一个精确的解决方案,因为没有其他人在测试同一个应用程序。至少,描述一下你不理解解决方案的哪一部分。

标签: java selenium selenium-webdriver webdriver


【解决方案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");
}

【讨论】:

  • 如果将visualization.txt放在C盘下,可以通过如下方式初始化:BufferedReader in = new BufferedReader(new FileReader("C:\\visualization.txt"));。编辑了我的答案。
  • 如果假设在 UI 中添加了一个不在属性文件中的新选项,在这种情况下对于相同的代码会发生什么
  • 你能打印两个列表(expectedDropDownItems 和actualDropDownItems)并将输出粘贴到这里吗?
  • 如果您不确定属性文件没有使用下拉项更新,那么您可以首先确保两个列表具有相同的大小,如果是,则只进行比较,否则,在那里测试失败。请参阅更新的代码。
  • 这样我就可以在结果中显示它是如何打印的
【解决方案2】:
//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");
   }
}

【讨论】:

  • 我在 if(!options.contains(eachOption.getText())) { 我得到了 java.lang.NullPointerException 所以我改成 !uiOptions 然后我得到了此行中的相同错误 for(String eachOptionFromPropertyFile : options.split(",")){
  • 只需检查以下命令是否从属性文件中获取选项。如果它在属性文件中找不到给定的键,它会返回 null 作为值,这可能是 NPE 的原因。字符串选项 = property.getProperty("visualizationId");
猜你喜欢
  • 1970-01-01
  • 2014-02-26
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多