Chapter 07: Conditional statements
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
Conditional statements
An if statement chooses a block based on a condition. else if adds alternatives and else handles all remaining cases. Conditions can combine comparisons. In tests, do not hide failed assertions by branching around them.
How it works
Step-by-step walkthrough
- Set a status code
- Compare against an expected value
- Print the outcome
Example
const status = 200;
if (status === 200) {
console.log('Success');
} else {
console.log('Unexpected status');
}Use assertions rather than conditionals to fail automated tests.
✎ Your practice task
Change status to 404 and observe the branch.
Knowledge check
Which block runs when if is false and no else if matches?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.