Filtering and counting WebElements 

Let's start with a simple test to determine the links displayed on the home page of the sample application. We get all the links from the home page and print their count, followed by the count of links that are visible on the page, as shown in the following code:

@Test
public void linksTest() {

List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Total Links : " + links.size());

long count = links.stream().filter(item -> item.isDisplayed()).count();
System.out.println("Total Link visible " + count);
}

In the preceding code, we used the findElements() method along with By.tagName to get all the links from the home page. However, for finding out the visible links out of them, we used the filter() function with a predicate to test whether the links are displayed. This is done by calling the isDisplayed() method of the WebElement interface. The isDisplayed method will return true if the link is displayed; otherwise it will return false. Finally, we called the count() method to get the count of links returned by the filter() function. This will show the following output on the console:

Total Links : 88
Total Link visible 37
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.64.128