Collections
Collections for Automation Testing (Handling Multiple Elements & Data)
Context (Why this topic exists NOW)β
After stabilizing tests with exception handling, the next real-world problem appears:
- One page has multiple matching elements
- Tests need to handle lists of items
- Windows, tabs, and dynamic data must be managed
- Test data grows beyond single values
Selenium can find multiple things, but it cannot organize, manage, or reason about them.
Collections exist to solve this exact problem.
What Selenium Cannot Do (The Gap)β
Selenium can:
- return multiple elements using
findElements() - switch between windows
Selenium cannot:
- decide how to store multiple values
- ensure uniqueness
- map keys to values (test data, headers)
- iterate safely and cleanly
This leads to messy loops and unreadable code.
What Java Collections Add (The Fix)β
Java Collections provide:
Listβ ordered collectionsSetβ unique collectionsMapβ keyβvalue associations
In simple words:
Selenium finds many things.
Java decides how to manage them.
Collection Types Used in Automation (Only What Matters)β
1. List (MOST USED)β
Automation Problemβ
Handling multiple matching elements on a page.
Java Fixβ
List<WebElement> rows = driver.findElements(By.cssSelector(".row"));
Why List?
- Maintains order
- Allows duplicates
- Easy iteration
Common List Operations (Automation)β
rows.size();
rows.get(0);
rows.forEach(row -> {
// validation
});
Used in:
- tables
- menus
- search results
2. Set (Uniqueness Handling)β
Automation Problemβ
Handling windows, tabs, or unique values.
Java Fixβ
Set<String> windowHandles = driver.getWindowHandles();
Why Set?
- No duplicates
- Ideal for windows and tabs
Window Handling Exampleβ
String parent = driver.getWindowHandle();
for (String window : driver.getWindowHandles()) {
if (!window.equals(parent)) {
driver.switchTo().window(window);
break;
}
}
Selenium gives handles. Java ensures uniqueness.
3. Map (Test Data & Key-Value Data)β
Automation Problemβ
Managing structured test data.
Examples:
- API headers
- User credentials
- Environment-specific data
Java Fixβ
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer token");
headers.put("Content-Type", "application/json");
Why Map?
- Fast lookup
- Clear structure
- Easy extension
Iteration (Processing Collections)β
Automation Problemβ
Messy loops everywhere.
Java Fixβ
for (WebElement element : rows) {
if (element.getText().contains("Active")) {
element.click();
break;
}
}
Enhanced for-loop keeps code readable.
Collections vs Arrays (Clear Rule)β
| Arrays | Collections |
|---|---|
| Fixed size | Dynamic size |
| Limited methods | Rich API |
| Rarely used | Heavily used |
Rule:
Use arrays only for fixed, simple data.
Use collections for automation.
Real Automation Exampleβ
List<WebElement> prices = driver.findElements(By.className("price"));
for (WebElement price : prices) {
String value = price.getText().replace("$", "");
int amount = Integer.parseInt(value);
Assert.assertTrue(amount > 0);
}
Selenium fetched elements. Java collections enabled validation.
Common Mistakes (Very Common)β
- Using arrays for dynamic data
- Assuming List order without checking UI
- Ignoring empty collections
- Modifying collection while iterating
- Using wrong collection type
Best Practices (Automation-Approved)β
- Use List for UI elements
- Use Set for windows and unique values
- Use Map for test data and headers
- Check collection size before accessing
- Keep iteration logic readable
Interview Notesβ
- Difference between List, Set, and Map
- Why
findElements()returns a List - How window handling works internally
- When to use Map in automation frameworks
- Arrays vs Collections
Summary (Human Understanding)β
Selenium can find multiple things. Without Java Collections, automation becomes chaos.
Collections bring:
- structure
- clarity
- control
Good collection usage = scalable automation.