Playwright + Java · Lesson 10 of 38

0/38 done

Module 10: Java Modern Features for Automation

10. Modern Java: Streams, Lambdas & Optional for QA

16 minlesson

Your task

Implement getPassingTests:

  1. Accept List<String> testResults (e.g. ["testLogin:PASS", "testCheckout:FAIL", "testSearch:PASS"]).
  2. Use Streams to filter only strings that endWith(":PASS").
  3. Collect and return as a List<String>.

Write it in the editor, then press Check answer (Ctrl + Enter).

Streams & Lambdas for Test Automation

java
import java.util.*;
import java.util.stream.Collectors;

List<String> rawTexts = Arrays.asList("  Product A ", " Product B", "   ");

// Clean and filter empty items in one pipeline:
List<String> cleanTitles = rawTexts.stream()
    .map(String::trim)
    .filter(t -> !t.isEmpty())
    .collect(Collectors.toList());

// Find first matching element safely with Optional:
Optional<String> match = cleanTitles.stream()
    .filter(t -> t.contains("Product A"))
    .findFirst();

if (match.isPresent()) {
    System.out.println("Found target product: " + match.get());
}

Key Takeaways & Walkthrough Summary

Modern Java introduces Lambdas, Streams, and Optional. These features let you filter tables, transform element text, and verify test assertions in concise, declarative lines of code. Let's master modern Java features for automation!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why use Streams instead of classic for loops?

Streams make data transformations declarative, readable, and less error-prone when filtering UI rows or extracting column values.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip