Using Stream API with Selenium WebDriver
Now that we have introduced Streams API and its various functions, let's see how we can use them in our tests.
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...