Playwright + Java · Lesson 7 of 38

0/38 done

Module 7: Collections

7. Lists, Maps & Sets for Dynamic Test Data

16 minlesson

Your task

Implement filterAdminUsers:

  1. Accept a Map<String, String> userRoles where key is username and value is role.
  2. Return a List<String> containing only usernames whose role is "ADMIN".

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

Collections in Automation

  1. List / ArrayList: Ordered collections with duplicates. Great for lists of search results and table rows.
  2. Set / HashSet: Unordered collections of unique items. Ideal for window handles and verifying unique IDs.
  3. Map / HashMap: Key-value pairs. Standard for headers, query parameters, and row data.
java
import java.util.*;

// List of harvested UI texts:
List<String> productTitles = new ArrayList<>();
productTitles.add("Wireless Mouse");
productTitles.add("Mechanical Keyboard");

// Map for API headers:
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer eyJhbGci...");
headers.put("Content-Type", "application/json");

// Iterate with for-each:
for (Map.Entry<String, String> entry : headers.entrySet()) {
    System.out.println(entry.getKey() + " -> " + entry.getValue());
}

Key Takeaways & Walkthrough Summary

Unlike fixed-length arrays, Java Collections dynamically grow as you harvest web elements, query databases, or parse API responses. In this lesson, we master List, ArrayList, Set, HashSet, and Map to handle dynamic test data. Let's dive in!

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

What is the difference between List and Set?

List maintains insertion order and allows duplicates (e.g. items in a dropdown). Set guarantees uniqueness and disallows duplicates.

Ready? Solve the task in the editor

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

Java
Skip