0% found this document useful (0 votes)
53 views12 pages

Collections in Automation Testing Guide

The document outlines key concepts of Java Collections, including interfaces, classes, and utility classes relevant to automation testing. It provides common interview questions and answers related to the use of collections in Selenium and API testing, emphasizing their importance in managing test data, ensuring uniqueness, and facilitating dynamic request generation. Additionally, it addresses high-impact interview questions regarding HashMap behavior, the distinction between Map and Collection interfaces, and the differences between equals() and hashCode().

Uploaded by

Amk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views12 pages

Collections in Automation Testing Guide

The document outlines key concepts of Java Collections, including interfaces, classes, and utility classes relevant to automation testing. It provides common interview questions and answers related to the use of collections in Selenium and API testing, emphasizing their importance in managing test data, ensuring uniqueness, and facilitating dynamic request generation. Additionally, it addresses high-impact interview questions regarding HashMap behavior, the distinction between Map and Collection interfaces, and the differences between equals() and hashCode().

Uploaded by

Amk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Collections Concepts (Important for Automation)

🔹 Interfaces:
List
Set
Map
Queue

🔹 Classes:
ArrayList, LinkedList
HashSet, LinkedHashSet, TreeSet
HashMap, LinkedHashMap, TreeMap
PriorityQueue, Deque

🔹 Utility Classes:
Collections (sorting, searching, synchronizing)
Arrays (for array-to-list conversions, sorting)
Common Collections Interview Questions (with
Automation Context)
1. What is the difference between List and Set?
List: Allows duplicates, maintains insertion order
Set: No duplicates, unordered (unless
LinkedHashSet or TreeSet)
Used In Automation: Store test data (List),
remove duplicates (Set)
2. Why do we use ArrayList in Selenium
frameworks?
Dynamic sizing
Fast access using index
Common for storing List<WebElement> from
findElements()
3. How do you store key-value data in automation
tests?
Use HashMap<String, String>
Example: Map<String, String> testData = new
HashMap<>();
Store config values, test data, API headers,
etc.
Common Collections Interview Questions (with
Automation Context)
4. How do you handle duplicate test data in your
framework?
Use Set<String> to store data
Automatically removes duplicates (e.g.,
dropdown values)

5. Where have you used Maps in your automation


framework?
Store element locators
Store input data from Excel/JSON
Store expected vs actual results

6. What’s a real use case of TreeMap or TreeSet in


automation?
TreeMap: When sorted keys are needed (e.g.,
module-wise test data)
TreeSet: To store sorted, unique test
identifiers or names
Common Collections Interview Questions (with
Automation Context)
7. How do you remove duplicates from a List?
List<String> unique = new ArrayList<>(new
HashSet<>(originalList));

8. How do you convert:

List to Set: Set<T> set = new HashSet<>(list);


Set to List: List<T> list = new ArrayList<>
(set);
Map keys to List: new ArrayList<>
([Link]());
9. How do you ensure thread safety in collections?

use [Link](),
synchronizedMap()
Apply in parallel test executions
Collections in Automation Testing – Mostly asked
Use Cases in Selenium
1. How do you store WebElements returned by
findElements()?
Use: List<WebElement>
[Link]([Link](...)) returns a list
of matching elements.
You can loop through this list to validate
dropdown items, menu links, etc.

2. How do you manage test data in a data-driven


framework?

Map<String, String> for a single row of test data


(column name →value)
List<Map<String, String>> for multiple rows of
test data
Works well when reading data from Excel,
JSON, or CSV
Allows easy mapping of test case ID →
corresponding inputs and expected values
Collections in Automation Testing – Mostly asked
Use Cases in Selenium
3. How do you store test steps or reusable locators?
Use: HashMap<String, By> or Map<String, String>
Map helps manage element locators by logical
names
Reusable across tests → improves
maintainability of Page Object Models

4. Where do you use Set in Selenium frameworks?


Useful for storing unique values like:
All window handles ([Link]())
Unique dropdown items to avoid duplicates
Prevents duplication without needing manual
checks
Java Collections in API Testing – Key Interview
Questions
1. How do you pass headers in an API request using
Collections?
Use Map<String, String> to store headers
Pass it to .headers() in RestAssured

Example:
Map<String, String> headers = new HashMap<>();
[Link]("Authorization", "Bearer token");
[Link]("Content-Type", "application/json");
given().headers(headers);
2. How do you build dynamic JSON payloads using
Collections?

Use Map<String, Object> for flexible JSON body


Add keys and values dynamically
Works well with POST and PUT requests
Example:
Map<String, Object> body = new HashMap<>();
[Link]("name", "John");
[Link]("age", 30);
given().body(body).post("/users");
Java Collections in API Testing – Key Interview
Questions
3. How do you represent a JSON array of objects
in Java?
Use List<Map<String, Object>>
Each Map is one object in the array
Useful for batch POST or validation

Example:
List<Map<String, Object>> payload = new
ArrayList<>();
Map<String, Object> item1 = new HashMap<>();
[Link]("id", 1); [Link]("name", "A");

Map<String, Object> item2 = new HashMap<>();


[Link]("id", 2); [Link]("name", "B");

[Link](item1);
[Link](item2);
Java Collections in API Testing – Key Interview
Questions
4. How do you extract multiple values from an API
response?
Use List<String> to collect values using
jsonPath()

Example:
List<String> ids =
[Link]().getList("[Link]");
5. How do you validate uniqueness in API responses?
Use Set<String> to store values
Compare size of Set and List to ensure no
duplicates

Example:
List<String> emails =
[Link]().getList("[Link]");
Set<String> uniqueEmails = new HashSet<>
(emails);

assert [Link]() == [Link]();


Java Collections in API Testing – Key Interview
Questions
6. How do you manage multiple responses in a test
flow?
Use Map<String, Response>
Helps store and reuse responses from different
endpoints

Example:
Map<String, Response> apiResponses = new
HashMap<>();
[Link]("createUser", res1);
[Link]("getUser", res2);
7. Why are Collections important in API automation
frameworks?
Enable dynamic request generation
Help in building data-driven tests
Useful for parsing and validating complex JSON
responses
Support clean and reusable code
High-Impact Interview Questions
1. What happens if two keys have the same
hashcode in a HashMap?
Both keys go to the same bucket.
HashMap uses equals() to resolve the key
collision.
If keys are equal, value is updated.
If keys are different, a linked list or tree stores
them.

2. Why is Map not a part of the Collection


interface?

Collection represents a group of elements


(single values).
Map represents key-value pairs, which is a
different structure.
Therefore, Map doesn’t extend Collection.
High-Impact Interview Questions

3. What is the difference between equals() and


hashCode()?
🔹 equals()
Used to compare the actual content (or state) of
two objects.
Example:
String a = new String("test");
String b = new String("test");
[Link]([Link](b)); // true

🔹 hashCode()
Returns an integer value representing the object’s
memory location or calculated hash.
Example:
[Link]([Link]()); // e.g., 3556498
[Link]([Link]()); // same
hashCode as `a`

You might also like