Using Loops and Conditions to Control Test Flow
Loops and conditions help you repeat actions, control decision-making, and manage dynamic scenarios in automated testing. They make your test scripts flexible, efficient, and capable of handling real-world variations.
๐ท 1. Using Conditions (if, else, switch) in Tests
Conditions allow your tests to react to different outcomes.
✔ Example: Java if / else in testing
if (response.getStatusCode() == 200) {
System.out.println("API is working fine");
} else {
System.out.println("API failed with status: " + response.getStatusCode());
}
✔ Example: Selenium conditional
if (driver.findElement(By.id("promoBanner")).isDisplayed()) {
driver.findElement(By.id("closeBanner")).click();
}
Why use conditions?
Handle optional pop-ups
Validate dynamic values
Stop a test when a critical failure occurs
Execute alternative paths in a workflow
๐ท 2. Using Loops (for, while, do-while) in Tests
Loops allow you to repeat test steps—useful for data-driven testing, iterating UI elements, or retrying failed assertions.
✔ For Loop Example
Iterating through a list of UI elements:
List<WebElement> items = driver.findElements(By.className("product"));
for (int i = 0; i < items.size(); i++) {
System.out.println(items.get(i).getText());
}
✔ Enhanced For Loop (For-Each)
For cleaner and faster iteration:
for (String username : usernamesList) {
login(username, "Password123");
}
✔ While Loop Example
Retry mechanism:
int retries = 0;
while (retries < 3) {
if (checkServerStatus()) {
System.out.println("Server is up!");
break;
}
retries++;
}
✔ Do-While Example
Used when you want the block to run at least once:
int count = 0;
do {
System.out.println("Attempt #" + count);
count++;
} while (count < 5);
๐ท 3. Using Loops + Conditions Together
Combining both controls allows powerful test logic.
✔ Example: Searching for a Product in a List
List<WebElement> products = driver.findElements(By.cssSelector(".product"));
boolean found = false;
for (WebElement product : products) {
if (product.getText().contains("Laptop")) {
product.click();
found = true;
break;
}
}
if (!found) {
System.out.println("Product not found in the list!");
}
✔ Example: Data-Driven Test Using Conditions
for (User user : users) {
if (user.isActive()) {
runLoginTest(user);
} else {
System.out.println("Skipping inactive user: " + user.getName());
}
}
✔ Example: Retrying a Failed Step with Conditions
boolean success = false;
for (int i = 0; i < 3; i++) {
success = clickButtonSafely();
if (success) {
break;
}
}
if (!success) {
Assert.fail("Button click failed after retries.");
}
๐ท 4. When to Use Loops in Testing
Iterating through form fields
Checking lists of buttons, menus, or records
Running the same test with multiple data sets
Implementing retry logic
Validating records in API responses
Testing pagination
๐ท 5. When to Use Conditions in Testing
Deciding whether to continue or stop the test
Handling dynamic content
Checking optional fields or pop-ups
Selecting different test paths
Validating expected vs. actual results
๐ฏ Summary
Feature Purpose
if/else Handle decisions, branches, optional elements
switch Clean decision-making based on fixed values
for loop Repeat actions with known count
while loop Repeat until a condition is met
do-while loop Ensure at least one execution
Combination Flexible, powerful test flow control
Learn Selenium with JAVA Training in Hyderabad
Read More
Java Basics for Selenium Testers
Introduction to TestNG Annotations
How to Use Assertions in Selenium Tests
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments