Assertions and Auto-Wait
Learn how assertions wait for the right state before they pass.
What you'll learn
- Understand auto-wait in assertions.
- Use expect on visible state.
- Avoid fixed sleep when possible.
Playwright AutomationlessonsJump to another lesson
Assertions and Auto-Wait
Learn how assertions wait for the right state before they pass.
Big Picture
One small picture can make this idea easier to hold.
Smart check
- Waits for the real state.
- Fails with a clear reason.
- Handles small delays well.
- Reads like user intent.
Fixed sleep
- Pauses for a guessed time.
- Can be too short or too long.
- Hides timing bugs.
- Makes tests slow.
VerdictAssertions are safer than hard sleeps. They wait for the app, not for a guess.
How It Moves
Short steps make the flow easier to see.
Find the target
Locate the element or value you want.
Set the expect
Say what state should be true.
Let it wait
The check retries for a short time.
Confirm pass
The test moves on when the state matches.
Step By Step
This is the same idea, stretched across time.
- 1
Trigger the action
Click, type, or load the page state.
- 2
Run the assertion
Check the expected result.
- 3
Wait if needed
Let the assertion retry for a short time.
- 4
Finish the test
Continue only when the state is right.
One Small Model
Think of this like a tiny card you can keep in your pocket.
Assertion loop
5 fieldsQuick Check
What does an auto-waiting assertion do?
Map It
One more picture helps you see where this lesson matters most.
Wait strategy map
Best choice
expect(locator).toBeVisible()
Okay sometimes
Short timeout with a reason
Weak choice
Long fixed sleep
Avoid
Sleep before every step