top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Selenium dream with Java Streams


What are Streams?

Streams API is a new feature available from Java SE 8. A stream is a sequence of objects that supports various methods which can be pipelined to produce desired result. By using streams we can perform various aggregate operations on the data returned from collection classes. Which drastically reduce the complexity of code.



Before we jump into the topic of Streams let’s get an insight of few methods we use.


Lamda is a ‘->’ operator. It divides the lamda expression into 2. The left side specifies parameters required by the expression. Which could also be empty if no parameters are required. The right side is the lamda body which specifies the actions of the lamda expression.


Filter gives subset of results and can perform another operation on result obtained.Terminal operations are count(), limit(), sorted().

There is no life for intermediate operation if there is no terminal operation.It operartes only if the filter returns TRUE.


Map: map() produces a new stream after applying a function to each element of the original stream. The new stream could be of different type.


forEach(): This method is used to iterate through every element of the stream


collect(): This method is used to return the result of the intermediate operations performed on

the stream. Helps in converting stream back to a List /Set/ Map.


distinct(): This method is for finding distinct elements from the stream.


Sorted(): returns a stream sorted in a natural order ( A to Z or 1 to 10)


Regular java algorithm to get the count of names start with ‘A’: (Eclipse IDE)

Java stream creation:

Let’s first obtain a stream from an existing array and get the count of names begin with letter ‘A’. The lines of code like looping and branching has been eliminated to two lines of code.

Note that Java 8 added a new stream() method to the Collection interface.

output :

count of names start with 'A' : 3

ANDREA ANTHONY ALEX



Let’s See How we can incorporate Streams into Selenium code:


Q: Write selenium script using streams to

  1. Find total number of links present in a webpage and print link text from all links (Regular way)?

  2. Using Lambda expression to print all the links present?

  3. Check how many links are broken?

  4. Trying to open each link in a separate tab?



Below is the screenshot of links opening in different tabs.



Benefits of using Streams in Selenium:

· We can write functions at more abstract level

· Which can reduce code bugs

· More readable lines of code

· Less mutability


Try these methods of java streams in your selenium code and relish !










138 views1 comment

Recent Posts

See All

In the early stages of a visualization project, we often start with two interrelated questions: Where can I find reliable data? What does this data truly represent? Information does not magically appe

bottom of page