2. Control Flow & Looping Structures in Test Automation

Conditional Decision Making in Tests

int statusCode = 200;
if (statusCode == 200) {
    System.out.println("API Success");
} else if (statusCode == 404) {
    System.out.println("Endpoint Not Found");
} else {
    System.out.println("Unhandled error code: " + statusCode);
}

Retry Loops & Iterations

// While loop retry pattern:
int attempts = 0;
boolean isElementReady = false;
while (attempts < 3 && !isElementReady) {
    attempts++;
    // poll element state
}

Task

  • Implement evaluateStatusCode:
  • 1. If statusCode is 200, return "PASS".
  • 2. If statusCode is 404, return "FAIL: Not Found".
  • 3. For any other status code, return "FAIL: Unexpected Status".
πŸ’‘Common doubts
Did you like the problem?
Lesson 2 of 38
Java
Loading code editor…
Next