如何与webdriver中的List <webelement>进行比较?

时间:2017-07-06 09:51:18

标签: java selenium selenium-webdriver

我正在尝试自动化双列表框测试。我想比较所选值(左侧)与移动值(右侧列表)。这是代码。

CountDownLatch

问题是如何比较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(); List<WebElement> selectedList=allFromListData.getAllSelectedOptions();

之间的数据

2 个答案:

答案 0 :(得分:2)

我假设,您要比较所选项目列表而不是Web元素列表,因为getOptions()方法将返回Web元素列表。逻辑很简单,首先从移动前后的列表中取值。然后对两个值列表进行排序,并比较/断言相等,如下所示。

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

答案 1 :(得分:1)

您可以尝试 CollectionUtils(java.lang.object)中的intersection()和subtract()方法。

subtract(Collection a, Collection b)

返回包含-b。

的新Collection
intersection(Collection a, Collection b)

返回包含给定集合的交集的Collection。