Kaustubh :
I have an application view, where there are list of items. There are say 10 pages with 200 records, 20 records per page. I wish to have the unqiue user count assigned. From the below code I'am able to get the unique records per page. But I wish to get unique user entry throughout the records. So for example while execution if user "Jack" is found, so if the same user is found in later pages I want it to get skipped. Thanks in advance!
Script:
List<WebElement> efirstpagecount = driver.findElements(By.xpath("//*[@id='usersList']/tbody/tr/td[3]"));
Set<String> uniqueUsers = efirstpagecount.stream().map(WebElement::getText).map(String::trim).distinct().collect(Collectors.toSet());
System.out.println("First page count: "+uniqueUsers.size());
Andrei Suvorkov :
You can merge your sets like this:
set1.addAll(set2);
It will add distinct all elements from set2 to a set1. The code sample:
List<WebElement> efirstpagecount = driver.findElements(By.xpath("//*[@id='usersList']/tbody/tr/td[3]"));
Set<String> uniqueUsers = efirstpagecount.stream().map(WebElement::getText).map(String::trim).distinct().collect(Collectors.toSet());
System.out.println("First page count: "+uniqueUsers.size());
List<WebElement> esecondpagecount = driver.findElements(By.xpath("//*[@id='usersList']/tbody/tr/td[3]"));
Set<String> uniqueUsers2 = esecondpagecount.stream().map(WebElement::getText).map(String::trim).distinct().collect(Collectors.toSet());
uniqueUsers.addAll(uniqueUsers2); // merge two sets
System.out.println("First and second page count: "+uniqueUsers.size());
or this:
Set<String> allUsers = new HashSet();
while(true){
List<WebElement> users = driver.findElements(By.xpath("xpath"));
allUsers.addAll(users.stream().map(WebElement::getText).map(String::trim).distinct().collect(Collectors.toSet()));
// move to next page if exists, else break loop
}
System.out.println("All pages count: " + allUsers.size());
PS performance of addAll()
is O(N):
// implementation of addAll() method
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}