简体   繁体   English

如何比较字符串列表中的元素与字符串列表?

[英]How to compare elements inside List of strings with Array List of string?

I have added products to my basket and listed their URL in a List and want to verify these products against given String[] of products the items are stored backwards in z String[] so the last item here is the 1st item in the List .. the number of items is 3 and below code works for 2 items and throw invoker exception at the assert method in the third item 我已经将产品添加到我的购物篮中并在列表中列出了他们的URL,并希望根据产品的String []验证这些产品,这些产品在z String []中向后存储,因此这里的最后一项是列表中的第一项。 。项目数为3,下面的代码适用于2个项目,并在第三个项目的assert方法中抛出调用异常

public void verifyBag(String[] goods) {
    actions.clickOn(By.xpath(bagLocator));
    Arrays.sort(goods);
    List<WebElement> listItems = actions.driver.findElements(By.xpath(bagItems));
    List <String> actualItems = new ArrayList<String>();
    for(int i=0;i<listItems.size();i++)
    {
        actualItems.add(listItems.get(i).getAttribute("href"));
    }
    int j = goods.length-1;
    for(int i=0;i<goods.length;i++) 
    { 

        String actualItem = actualItems.get(i);
        String product = goods[j];
        System.out.println(product);
        //assertTrue(actualItems.get(i).contains(goods[j]));
        assertTrue(actualItem.equals(product));
            j--;        
        } 
        assertEquals(listItems.size(), goods.length,"Assert Number of Items in the Bag");
    }

If you don't care about the order, but about the match between provided list of goods and actualItems , you can do this: 如果您不关心订单,但关于goods列表与actualItems之间的匹配,您可以这样做:

  1. Convert input array String[] goods into some collection, for example List . 将输入数组String[] goods转换为某个集合,例如List Lets call it goodsList . 让我们称之为goodsList
  2. From goodsList , remove all items that are also in actualItems . goodsList ,删除同样在actualItems中的所有项目。

    • If resulting set is empty, it means all items from goodsList are also in actualItems . 如果结果集为空,则表示goodsList中的所有项目也在actualItems
    • If resulting set is not empty, it will contain list of items that are missing in actualItems comparing to goodsList 如果结果集是不是空的,这将包含缺少的产品清单actualItems比较goodsList
  3. You can also do the reverse: from actualItems , remove all items that are also contained in goodsList . 您也可以执行相反的操作:从actualItems中删除goodsList包含的所有项目。 That gives you list of items that were not present in provided list. 这为您提供了提供列表中不存在的项目列表。

Code: 码:

public void verifyBag(String[] goods) {
    actions.clickOn(By.xpath(bagLocator));
    List<WebElement> listItems = actions.driver.findElements(By.xpath(bagItems));
    List <String> actualItems = new ArrayList<String>();
    for(int i=0;i<listItems.size();i++)
    {
        actualItems.add(listItems.get(i).getAttribute("href"));
    }
    List<String> goodsList = new ArrayList(Arrays.asList(goods));
    goodsList.removeAll(actualItems);
    if(goodsList.size() == 0) {
        // All goods from provided goods list are also in actualItems
    }
    else {
        // Some items didn't match
    }
  1. You have to check the size of goods and actualItems before doing the loop. 在进行循环之前,您必须检查goodsactualItems的大小。 Make sure that array and list have the same size and both of them is not null or empty. 确保数组和列表具有相同的大小,并且它们都不为null或为空。

  2. Function listItems.get(i) and getAttribute("href") can return a null value, please check it before add to list. 函数listItems.get(i)getAttribute("href")可以返回null值,请在添加到列表之前检查它。

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

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