How to Use Java Streams in Selenium Automation

 How to Use Java Streams in Selenium Automation

✅ What Are Java Streams?

Java Streams are a feature introduced in Java 8 that allow you to process collections (like List, Set) in a functional and readable way.


They help you perform operations like:


Filtering elements


Mapping (transforming) values


Collecting results


Reducing (combining) values


✅ Why Use Streams in Selenium?

In Selenium, you often deal with lists of web elements (e.g., List<WebElement>).

Streams help to:


Make the code cleaner and shorter


Avoid writing loops manually


Process elements efficiently


✅ Common Use Cases in Selenium

1. Get Text from a List of WebElements

java

Copy

Edit

List<WebElement> elements = driver.findElements(By.className("item"));

List<String> itemTexts = elements.stream()

                                 .map(WebElement::getText)

                                 .collect(Collectors.toList());

✅ Explanation:


map(WebElement::getText) gets the text of each element.


collect(Collectors.toList()) collects them into a list.


2. Filter Elements Based on Text

java

Copy

Edit

List<WebElement> elements = driver.findElements(By.tagName("a"));

List<String> linksWithLogin = elements.stream()

                                      .map(WebElement::getText)

                                      .filter(text -> text.contains("Login"))

                                      .collect(Collectors.toList());

✅ Filters only links that contain the word "Login".


3. Click the First Matching Element

java

Copy

Edit

elements.stream()

        .filter(e -> e.getText().equals("Submit"))

        .findFirst()

        .ifPresent(WebElement::click);

✅ Clicks the first element whose text is exactly "Submit".


4. Check if Any Element Matches a Condition

java

Copy

Edit

boolean hasContactLink = elements.stream()

                                 .anyMatch(e -> e.getText().contains("Contact"));

✅ Returns true if at least one element contains "Contact".


5. Count Matching Elements

java

Copy

Edit

long count = elements.stream()

                     .filter(e -> e.getText().startsWith("Item"))

                     .count();

✅ Counts how many elements start with "Item".


✅ Best Practices

Avoid long stream chains if they reduce readability.


Always handle NoSuchElementException or StaleElementReferenceException.


Use findFirst().ifPresent() to avoid NullPointerException.


✅ Conclusion

Java Streams in Selenium automation help make your code cleaner, shorter, and more functional. They are especially useful when working with lists of WebElements and performing operations like filtering, mapping, or collecting.

Learn Selenium JAVA Course in Hyderabad

Read More

Reading Data from Properties Files in Java

Writing Your First Selenium Test Case Using Java

Creating Utility Classes for Common Selenium Functions

Java OOP Concepts for Selenium Testers

Visit Our IHUB Talent Training in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Tosca for API Testing: A Step-by-Step Tutorial

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing