In Java 8 there is a new concept of Java Streams. Not to be confused with the existing I/O Stream like FileInputStream, streams are wrappers around a data source, allowing us to operate with that data source and making bulk processing convenient and fast.

This functionality – java.util.stream – supports functional-style operations on streams of elements, such as map-reduce transformations on collections.

Note: A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source.

If you want to read about Java Streams, you can read this awesome tutorial with examples. You can also view this awesome playlist on Java Streams

Filter Method

The filter() method in Java is a method provided by the Stream interface that allows you to filter elements from a stream based on a certain condition.

This is the syntax

The filter() method takes a Predicate as an argument, which is a functional interface that represents a boolean-valued function that takes an object of type T as input and returns a boolean. The Predicate function should return true for elements that should be included in the filtered stream, and false for elements that should be excluded.

The filter() method returns a new stream that contains only the elements that satisfy the given predicate. The new stream is lazily evaluated, which means that it won’t actually process the elements until a terminal operation is called on it.

Let’s take an example to understand – Suppose you want to find out the even numbers from a list. You can use the filter method with the condition that the modular division with 2 equals 0 and use it to filter out the numbers

Now as you can see, the original data source ( numbers) is not modified and instead a new stream (evenNumbers) is created with the filter method, which is what is the property of Java Streams.

Uses in Test Automation

Now the pertinent question – how to use this in test automation code. Let’s face it, not everyday would you be required to filter out the even numbers from a list – we need practical examples to demonstrate how we can use the concept of filter in test automation code.

Note that most of the examples are something that I’ve implemented in my own test automation framework – the code snippet might vary but the concept remains the same.

  • Filtering web elements based on any condition

The first scenario that I’ve used in my codebase is to filter out some web elements based on some condition. You can use the filter method to filter out elements from a list that do not meet a certain condition.

For example, if you have a list of web elements and you want to filter out all elements that are not displayed on the page, you can use the filter method to achieve this.

In this code we first find all input elements on a page using the findElements() method. We then use the filter() method to create a new list that only contains elements that are displayed.

  • Filtering data from API response

Another use case in test automation is the api testing. You may have a scenario where you need to filter out certain keys from your response based on some condition. You can do that using filter method.

Let say if you are testing an API that returns a large amount of data, you can use the filter method to filter out the data that you are interested in. For example, if you are only interested in the data for a particular user, you can use the filter method to filter out all the data for other users.

In this example, I want to get all the users whose are assigned a department with id dept_id. After we fetch the response, we use the jsonPath() method to extract the list of users from the response. We then use the filter() method to create a new list that only contains the user with whose department id matches the dept_id passed in the method.

  • Filtering data from a database query

This is something that we can use with DB queries as well. So instead of using a code snippet, you can use DB queries too. If you are testing a database-driven application, you can use the filter method to filter out data that you do not need. For example, if you want to retrieve all the records for a particular customer, you can use the filter method to filter out all the records for other customers.

We execute a SELECT query on a database to retrieve all customer records. We iterate through the ResultSet to create a list of Customer objects. We then use the filter() method to create a new list that only contains the customer with first name “Rahul”. Now this can be done also easily using a simple db query with WHERE clause. This is just to give an example that you can do it in this way too.

  • Filtering test cases based on tags

If you have a large number of test cases and you want to run only a subset of them, you can use the filter method to filter out the test cases that you do not want to run. For example, if you have test cases tagged as smoke and regression, you can use the filter method to run only the smoke or the regression test cases.

In this particular scenario, there is a method getTestCases() that can be used to get all test cases. we retrieve a list of all test cases using the getTestCases() method and then use the filter() method to create a new list that only contains test cases with the smoke tag.

Again, no need to going this long way if you’re using frameworks like TestNG or Cucumber or Mocha as they give the ability to run the test cases based on tags only like

  • Filtering logs for specific messages

If you are debugging a test case and you want to filter out log messages that are not relevant to the issue you are investigating, you can use the filter method to filter out the log messages that you do not need. For example, if you are looking for an error message in a log file, you can use the filter method to filter out all the other messages.

  • Filtering elements from a web table

Suppose you have a table on a web page, and you want to filter out only the rows that contain a specific text value. You can use the filter() method to create a new list that only contains the matching rows:

In this example, first we find all rows of a table on a web page using the findElements() method, using the xpath. We then use the filter() method to create a new list that only contains rows that contain the text John Doe. Not that the condition may be different based on your need or test case, but this is the way you can do that.

  • Filtering elements from Drop down

Suppose you have a dropdown on a web page, and you want to filter out only the options that are enabled. You can use the filter() method to create a new list that only contains the enabled options:

In this example, we’re getting the drop down element with the id locator and then using the getOptions() method to get all the select options for the drop down. Then we use the filter method to get a new list that contains only those elements which are enabled.

As you can see from above code snippets, you can use the filter method in many ways to selectively get data in your test automation code. Now there are some scenarios like the db query one or the tags one that can easily be done via the existing functionality of the db or the test framework like TestNG, so using the filter might be an overkill, unless you have a very specific scenario.

Let me know in the comments in the post about your experience and if you’re using the filter method else where in your test automation codebase.