如何使用Java验证与selenium WebDriver中的UI选项匹配的下拉选项?

时间:2014-03-27 06:01:11

标签: java selenium selenium-webdriver webdriver

我已经编写了如何从属性文件中读取选项的代码。但我不知道如何验证UI下拉中的相同值。

请任何人帮我一样的代码:

@Test()
public void Filterselection_1() throws Exception{

try {
FileInputStream fstream = new FileInputStream("C:/FilterSection/visualization.txt");
      // Get the object of DataInputStream
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
    //Read File Line By Line
      while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine);
      }
      //Close the input stream
      in.close();
        }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
      }

1 个答案:

答案 0 :(得分:0)

尝试下面给出的代码

  1. 我们找到下拉元素
  2. 获取列表的所有选项
  3. 从选项列表
  4. 创建值列表
  5. 逐行阅读属性文件
  6. 检查值列表中是否存在每一行
  7. 如果存在,请从值列表中删除该值
  8. 如果不存在,则打印值不存在
  9. 如果值列表不为空,即它包含属性文件中不存在的值,则打印消息说明相同。

     @Test()
     public void Filterselection_1() throws Exception{
     try {
        //Find the drop down
        Select select  = new Select(DropDownElement);
    
        //Get all options to a List
        List<WebElement> options = select.getOptions(); 
    
        //Iterate through the list and create a list of String containing all the values
        List<String> valueList = new ArrayList<String>();
        for (Webelement option : options) {
            valueList.add(option.getText());
        }
    
        FileInputStream fstream = new FileInputStream("C:/FilterSection/visualization.txt");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
            //find whether the value is present in the value list
            if (valueList.contains(strLine)) {
                valueList.remove(strLine);
            } else {
               System.out.println(strLine + "not found in the drop down");
            }
        }
        //Close the input stream
        in.close();
    
        //check if valueList is empty (it will be empty if all the values 
        //in the dropdown are present in the property file)
        if (!valueList.isEmpty()) {
            Ststem.out.println("The following values present in the dropdown are not present in the property file : ");
            for (String s : valueList) {
                System.out.prinyln(s);
            }
        }
    
    }catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    } 
    

    }

  10. 如果这有助于您,请告诉我。