Reputation: 25
I am struggling to get the desired result when verifying to see if one list is contained in the other. I am not too sure how to handle it.
List 1:
List<WebElement> allValues = driver.findElement(By.xpath("//*[@id='allvalues']"));
List<String> list1 = new ArrayList<>();
for (WebElement all:allValues) {
String s = all.getText().trim();
list1.add(s);
}
System.out.println(list1)
output:
"demo1", "demo2", "demo3", "demo4", "demo5"
List 2:
List<WebElement> partValues= driver.findElement(By.xpath("//*[@id='partvalues']"));
List<String> list2= new ArrayList<>();
for (WebElement part: partValues) {
String t = part.getText().trim();
list2.add(t);
}
System.out.println(list2)
output:
"demo2", "demo3", "demo4"
I want to verify that list1 contains all of list 2. I tried a simple boolean verification method like:
System.out.println(list1.containsAll(list2));
and System.out.println(list2.containsAll(list1))
. Both returned false and that should not be te case.
What's the most reliable way to verify that list1
contains list2
?
Upvotes: 0
Views: 33
Reputation: 338795
List#containsAll
works as documented, and does what you want.
I copy-pasted your example data.
List< String > list1 = List.of( "demo1", "demo2", "demo3", "demo4", "demo5" ) ;
List< String > list2 = List.of( "demo2", "demo3", "demo4" ) ;
boolean contained = list1.containsAll( list2 ) ;
See code run live at Ideone.com.
contained = true
Upvotes: 1