3

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

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");
    }
3
  • and throw invoker exception at the assert method in the third item ... please share the stacktrace and details of the line where it throws the exception Commented Nov 30, 2018 at 2:27
  • Firstly, I think you should check for quantity before you do the looping. Secondly, there is list.contains() available, which you might be able to use. Thirdly, if you just want to know if everything is correct, you can convert the array into a list, then sort both, and use equals(). Commented Nov 30, 2018 at 2:31
  • By the way, after the first three lines, your code can be well optimized for readability as List<String> actualItems = listItems.stream() .map(listItem -> listItem.getAttribute("href")) .collect(Collectors.toList()); IntStream.range(0, goods.length).forEach(i -> assertEquals(goods[goods.length - i - 1], actualItems.get(i))); assertEquals(listItems.size(), goods.length, "Assert Number of Items in the Bag"); Commented Nov 30, 2018 at 2:34

2 Answers 2

1

If you don't care about the order, but about the match between provided list of goods and actualItems, you can do this:

  1. Convert input array String[] goods into some collection, for example List. Lets call it goodsList.
  2. From goodsList, remove all items that are also in actualItems.

    • If resulting set is empty, it means all items from goodsList are also in actualItems.
    • If resulting set is not empty, it will contain list of items that are missing in actualItems comparing to goodsList
  3. You can also do the reverse: from actualItems, remove all items that are also contained in 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
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your cod @Kiril but it threw the below exception java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:161) at java.util.AbstractList$Itr.remove(AbstractList.java:374) at java.util.AbstractCollection.removeAll(AbstractCollection.java:376) at POM.NisnassProduct.verifyBag(NisnassProduct.java:72)
apparently it's a known issue with Arrays.asList. I updated a code to work around it
0
  1. You have to check the size of goods and actualItems before doing the loop. Make sure that array and list have the same size and both of them is not null or empty.

  2. Function listItems.get(i) and getAttribute("href") can return a null value, please check it before add to list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.