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");
}
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 useequals().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");