Playwright + Java · Lesson 2 of 38

0/38 done

Module 2: Conditional Statements & Loops

2. Control Flow & Looping Structures in Test Automation

15 minlesson

Your 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".

Write it in the editor, then press Check answer (Ctrl + Enter).

Conditional Decision Making in Tests

java
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

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

Key Takeaways & Walkthrough Summary

In test automation, you constantly check HTTP status codes, retry failed locator checks, and iterate through table rows. In this lesson, you will master conditional branching and loop structures in Java. Let's dive in!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

When should I use switch over if-else in test frameworks?

Use switch statements when selecting browser engines (e.g., 'chromium', 'firefox', 'webkit') or running tests on specific environment flags.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip