Why Collections?
Collections are used whenever we need to store more than one value together.
Examples include storing:
- Multiple Usernames
- Multiple Web Elements
- Multiple Browser Windows
- Multiple Test Results
Memory Formula
- Many Values → List
- Unique Values → Set
- Key–Value Data → Map
List – Ordered, Duplicates Allowed
A List:
- Maintains insertion order.
- Allows duplicate values.
- Uses index-based access (
0, 1, 2...).
It is commonly used when working with multiple items.
Typical framework usage includes:
- Multiple Web Elements
- Test Data
- Failed Test Cases
Example 1: Multiple Web Elements
List<WebElement> buttons = driver.findElements(By.tagName("button"));
for (WebElement btn : buttons) {
System.out.println(btn.getText());
}
When a page contains multiple buttons, Selenium stores them in a List.
The framework loops through the collection to perform validations or actions.
Used for UI validation.
Example 2: Test Data
List<String> usernames = new ArrayList<>();
usernames.add("admin");
usernames.add("user");
usernames.add("manager");
for (String user : usernames) {
System.out.println(user);
}
Used when the same test executes with multiple data sets.
Example 3: Failed Test Cases
List<String> failedTests = new ArrayList<>();
failedTests.add("LoginTest");
failedTests.add("PaymentTest");
Used in:
- Retry Analyzer
- Custom Reports
Interview Line
"I used List in my framework to store multiple web elements, test data, and failed test cases where order is required."
Set – Unique Values Only
A Set stores only unique values.
Characteristics:
- No duplicate values
- Order is not guaranteed
It is commonly used where uniqueness is required.
Example 1: Window Handling
Set<String> windowHandles = driver.getWindowHandles();
for (String window : windowHandles) {
driver.switchTo().window(window);
}
Every browser window has a unique Window ID.
Selenium returns all window handles as a Set<String>.
Set is compulsory for window handling.
Example 2: Removing Duplicate Values
Set<String> productNames = new HashSet<>();
productNames.add("Mobile");
productNames.add("Laptop");
productNames.add("Mobile"); // duplicate
Output:
Mobile
Laptop
The duplicate value is automatically removed.
Interview Line
"I used Set in my framework mainly for window handling because window handles are unique and duplicates are not allowed."
Map – Key–Value Data
A Map stores information as Key–Value pairs.
Think of it like a dictionary:
- Word → Meaning
Framework configuration and test data naturally follow a key-value structure.
Example 1: Login Test Data
Map<String, String> loginData = new HashMap<>();
loginData.put("username", "admin");
loginData.put("password", "admin123");
driver.findElement(By.id("user")).sendKeys(loginData.get("username"));
Using keys makes retrieving data simple and flexible.
Example 2: Configuration Data
Map<String, String> config = new HashMap<>();
config.put("browser", "chrome");
config.put("env", "qa");
config.put("url", "https://qa.app.com");
This allows easy switching between environments.
Interview Line
"I used Map in my framework to store test data and configuration in key-value format, which makes data handling flexible."
List + Map Together (Data-Driven)
A common real-project structure combines List and Map.
List<Map<String, String>> testData = new ArrayList<>();
Meaning
A List contains multiple test records.
Each test record is stored as a Map.
Example
| Test Case | Username | Password |
|---|---|---|
| TC1 | admin | admin123 |
| TC2 | user | user123 |
This structure is commonly used in a Data-Driven Framework.
Where Else Collections Are Used
| Framework Area | Collection Used |
|---|---|
| Multiple Web Elements | List |
| Multiple Browser Windows | Set |
| Test Data | Map |
| API Response | List / Map |
| Failed Test Cases | List |
Perfect Interview Answer
"In my automation framework, I used Java Collections at multiple places. I used List to store multiple web elements and test data, Set to handle window handles because they are unique, and Map to store test data and configuration in key-value format. Collections helped me make the framework dynamic and maintainable."
STAR Answer (Simple)
Situation
The application contained multiple web elements and browser windows.
Task
Handle them dynamically.
Action
Used:
- List
- Set
- Map
appropriately throughout the framework.
Result
The framework became:
- Stable
- Dynamic
- Reusable
- Maintainable
FAQs
Where Did You Use List, Set, and Map in Your Automation Framework?
- List → Multiple Web Elements, Test Data, Failed Test Cases
- Set → Window Handles
- Map → Test Data and Configuration
Why Is Set Used for Window Handles?
Each browser window has a unique Window ID.
driver.getWindowHandles() therefore returns:
Set<String>
to prevent duplicate window identifiers.
Why Is Map Used for Test Data?
Test data and configuration are naturally stored as:
- Name → Value
Using a Map makes retrieval simple.
Example:
loginData.get("username");
What Is List<Map<String, String>>?
It represents:
- A List of test records
- Each test record stored as a Map
This is a common structure in Data-Driven Frameworks.
What Is the One-Line Memory Formula?
- Many Values → List
- Unique Values → Set
- Key–Value Data → Map