简体   繁体   English

如何与清单比较 <WebElement> 在webdriver中?

[英]How to compare to List<WebElement> in webdriver?

I am trying to automate Dual list Box testing. 我正在尝试自动化双重列表框测试。 I want to compare the selected values(left side) vs moved values (into right side list). 我想比较所选值(左侧)与移动值(进入右侧列表)。 Here is the code. 这是代码。

Select allFromListData = new Select(listData);
    // values selection
    allFromListData.selectByIndex(0);
    allFromListData.selectByVisibleText("Helena");
    List<WebElement> selectedList=allFromListData.getAllSelectedOptions();
    //clicking on add button

    for(int i=0;i<selectedList.size();i++)
        System.out.println(selectedList.get(i).getText());
    WebElement addButton = driver.findElement(By.xpath("//*[@id='pickList']/div/div[2]/button[1]"));
    addButton.click();

    //Verification of selected content... 
    WebElement toListData=driver.findElement(By.xpath("//*[@id='pickList']/div/div[3]/select"));
    Select allToListData = new Select(toListData);
    List<WebElement> movedData=allToListData.getOptions();

Question is how to compare the data between List<WebElement> selectedList=allFromListData.getAllSelectedOptions(); 问题是如何比较List<WebElement> selectedList=allFromListData.getAllSelectedOptions();之间的数据List<WebElement> selectedList=allFromListData.getAllSelectedOptions(); and List<WebElement> movedData=allToListData.getOptions(); List<WebElement> movedData=allToListData.getOptions();

I assume, you want to compare the list of selected items string not the list of web elements as getOptions() method will return list of web elements. 我假设,您要比较所选项目字符串的列表而不是Web元素的列表,因为getOptions()方法将返回Web元素的列表。 Logic is simple, first take the list of values from the list before and after move. 逻辑很简单,首先从移动前后的列表中获取值列表。 Then sort the both of the list of values and compare/assert for equality as given below. 然后对这两个值列表进行排序,并比较/确定是否相等,如下所示。

 Select allFromListData = new Select(listData);
    // values selection
    allFromListData.selectByIndex(0);
    allFromListData.selectByVisibleText("Helena");
    List<WebElement> selectedList=allFromListData.getAllSelectedOptions();

    //add selected Items to list
    List<String> lstSelectedItem=new ArrayList<String>();
    for(int i=0;i<selectedList.size();i++){
        System.out.println(selectedList.get(i).getText());
        lstSelectedItem.add(selectedList.get(i).getText());
    }

    //clicking on add button
    WebElement addButton = driver.findElement(By.xpath("//*[@id='pickList']/div/div[2]/button[1]"));
    addButton.click();

    //Verification of selected content... 
    WebElement toListData=driver.findElement(By.xpath("//*[@id='pickList']/div/div[3]/select"));
    Select allToListData = new Select(toListData);
    List<WebElement> movedData=allToListData.getOptions();

    //add moved Items to list
    List<String> lstMovedItem=new ArrayList<String>();
    for(int i=0;i<movedData.size();i++){
        System.out.println(movedData.get(i).getText());
        lstMovedItem.add(movedData.get(i).getText());
    }

    //sort the items
    Collections.sort(lstSelectedItem);
    Collections.sort(lstMovedItem);

    //verify the lists are equal
    Assert.assertEquals(lstSelectedItem, lstMovedItem);

You can try intersection() and subtract() methods from CollectionUtils( java.lang.object) . 您可以尝试从CollectionUtils(java.lang.object)中使用交集()和减法()方法。

subtract(Collection a, Collection b)

Returns a new Collection containing a - b. 返回一个包含a-b的新Collection。

intersection(Collection a, Collection b)

Returns a Collection containing the intersection of the given Collections. 返回包含给定Collections的交集的Collection。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM