Using Java Collections in Selenium Test Automation
- swathiperiasamy
- Oct 6
- 3 min read

Java Collections are standard data structures (List, Set, Map, Queue, etc.) that help you store, organize, and operate on groups of objects. In test automation, Collections are everywhere — from storing test data and expected results to keeping locators and aggregating test outcomes. Using the right collection makes your test scripts simpler, faster, and easier to maintain.
Why Collections Matter in Test Automation
Collections are essential because they help testers:
Manage test data cleanly: Store multiple usernames, input values, or API payloads in a List instead of hardcoding them.
Remove duplicates: Use Set to ensure only unique values like IDs or product names.
Map related values: Use Map to associate keys with values, such as locatorName → xpath or testCaseId → expectedResult.
Preserve order or provide FIFO behavior: Use List when order matters; Queue for tasks that should run in sequence.
Easier validation & comparisons: Convert results into collections and use built-in APIs for comparisons.
Core Collection Types in Java
The Java Collections Framework has four main types: List, Set, Map, and Queue. Let’s see how each can be used in test automation.
List – Ordered Collection (Allows Duplicates)
A List stores elements in order and allows duplicates. Ideal for test data that needs to be executed sequentially, such as usernames, URLs, or input values.
Example – Reading Login Test Data
List<String> usernames = new ArrayList<>();
usernames.add("Alice");
usernames.add("Bob");
usernames.add("Charlie");
for (String user : usernames) {
System.out.println("Running login test for: " + user);
// driver.findElement(By.id("username")).sendKeys(user);
// driver.findElement(By.id("loginBtn")).click();
}
When to Use:
You care about the order of items.
Duplicates are allowed (e.g., repeated test data).
Set – Unique Collection (No Duplicates)
A Set ensures all elements are unique. Perfect for storing unique product names, IDs, or page titles.
Example – Removing Duplicate Product Names
List<String> productNames = Arrays.asList("Shirt", "Shoes", "Shirt", "Watch");
Set<String> uniqueProducts = new HashSet<>(productNames);
System.out.println("Unique Products: " + uniqueProducts);
When to Use:
You need uniqueness.
Order doesn’t matter (or use LinkedHashSet to preserve insertion order).
Map – Key-Value Pairs
A Map stores data as key-value pairs, which is useful for linking test-related information, such as locators or expected results.
Example – Managing Locators
Map<String, By> loginLocators = new HashMap<>();
loginLocators.put("username", By.id("user"));
loginLocators.put("password", By.id("pass"));
loginLocators.put("loginButton", By.id("loginBtn"));
// Using Map in tests:
driver.findElement(loginLocators.get("username")).sendKeys("admin");
driver.findElement(loginLocators.get("password")).sendKeys("pass123");
driver.findElement(loginLocators.get("loginButton")).click();
When to Use:
You need a key-value relationship.
You want quick lookups for organized data.
Queue – FIFO (First-In-First-Out)
A Queue stores elements in the order they should be processed. Useful for managing sequential test execution or retrying failed tests.
Example – Scheduling Test Cases
Queue<String> testQueue = new LinkedList<>();
testQueue.add("Login Test");
testQueue.add("Search Test");
testQueue.add("Checkout Test");
while (!testQueue.isEmpty()) {
String testName = testQueue.poll();
System.out.println("Executing: " + testName);
// executeTest(testName);
}
When to Use:
Sequential execution is needed.
Managing tasks dynamically (like retries).
Example: Using Collections in a Selenium Test Framework
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
// List of user credentials
List<Map<String, String>> users = new ArrayList<>();
Map<String, String> user1 = new HashMap<>();
user1.put("username", "Alice"); user1.put("password", "password123");
Map<String, String> user2 = new HashMap<>();
user2.put("username", "Bob"); user2.put("password", "pass456");
users.add(user1); users.add(user2);
// Set – Unique products
Set<String> uniqueProducts = new HashSet<>();
// Queue – Test execution order
Queue<String> testCases = new LinkedList<>();
testCases.add("Login Test"); testCases.add("Product Search Test"); testCases.add("Result Validation Test");
// Map – Test results
Map<String, String> testResults = new LinkedHashMap<>();
// Test workflow
while (!testCases.isEmpty()) {
String testName = testCases.poll();
System.out.println("\nRunning: " + testName);
switch (testName) {
case "Login Test":
for (Map<String, String> user : users) {
System.out.println("Logging in as: " + user.get("username"));
// Selenium actions here
}
testResults.put(testName, "PASS");
break;
case "Product Search Test":
List<String> products = Arrays.asList("Shoes", "Watch", "Shoes", "Bag");
uniqueProducts.addAll(products);
System.out.println("Unique products found: " + uniqueProducts);
testResults.put(testName, "PASS");
break;
case "Result Validation Test":
for (String product : uniqueProducts) {
System.out.println("Validating product: " + product);
// Selenium actions here
}
testResults.put(testName, "PASS");
break;
}
}
// Print summary
System.out.println("\n--- TEST EXECUTION SUMMARY ---");
testResults.forEach((k, v) -> System.out.println(k + " → " + v));
driver.quit();
Pro Tips for Beginners
Choose the right collection: List for order, Set for uniqueness, Map for key-value, Queue for sequential tasks.
Use generics: Like List<String> or Map<String, String> for type safety.
Combine collections: Example: List<Map<String, String>> to manage complex test data.
Keep collections relevant: Clear unused data to save memory.
Integrate with reports & data sources: Store results in collections, then generate reports or Excel files.
Conclusion
Java Collections are essential for automation testers. They help organize test data, manage workflows, and make scripts reusable and maintainable. By learning List, Set, Map, and Queue with real examples, beginners can build structured and robust Selenium test frameworks without hardcoding values or losing track of data.


