Module 1 · Promise · async/await

await
Why It Matters

Without await, tests run in the wrong order — this deck makes it visible

async/await Promise Flaky Test Rules
AI with Rufat
PROBLEM — without await

Code Finishes First, Operations Come Later

Without await, functions start but don't wait for completion. The code below is broken — 6 await keywords are missing:

async function runTest() {
console.log('Test starting...');
fakePage.goto('/auth/login'); ← ❌ missing await
console.log('Navigated to login page');
fakePage.waitForSelector('#email'); ← ❌ missing await
fakePage.fill('#email', '...'); ← ❌ missing await
fakePage.fill('#password', '...'); ← ❌ missing await
fakePage.click('button[type=submit]'); ← ❌ missing await
console.log('Login form submitted');
fakePage.screenshot(); ← ❌ missing await
console.log('Test complete!');
}
AI with Rufat
Compare the Output
❌ WITHOUT await
Windows PowerShell
Test starting...
Navigated to login page
Login form submitted
Test complete!
Navigated to: /auth/login
Waited for: #email
Filled #email with: ...
Filled #password with: ...
Clicked: button[type=submit]
Screenshot taken
Method outputs arrived AFTER "Test complete!"
✅ WITH await
Windows PowerShell
Test starting...
Navigated to: /auth/login
Navigated to login page
Waited for: #email
Filled #email with: ...
Filled #password with: ...
Clicked: button[type=submit]
Login form submitted
Screenshot taken
Test complete!
Each action completes before the next begins
AI with Rufat
await — PAUSES AND WAITS

What Happens in Time

❌ WITHOUT await
goto()
fill()
click()
← operations are delayed ⚠
console.log lines — RACED AHEAD
Code lines → What actually happens: methods completed much later
✅ WITH await
await goto()
await fill()
await click()
✓ done
Each action completes before the next begins
AI with Rufat
❌ Broken — no await
fakePage.goto('/auth/login');
// (no await)
fakePage.waitForSelector('#email');
fakePage.fill('#email', '...');
fakePage.fill('#password', '...');
fakePage.click('button[type="submit"]');
fakePage.screenshot();
Execution moves to the next line without waiting. Test runs in the wrong order.
✅ Fixed — with await
await fakePage.goto('/auth/login');
await fakePage.waitForSelector('#email');
await fakePage.fill('#email', '...');
await fakePage.fill('#password', '...');
await fakePage.click('button[type="submit"]');
await fakePage.screenshot();
Each action completes fully before moving on. Test runs reliably.
AI with Rufat

Rule:
Never Skip await

Missing await is the #1 cause of test failures — flaky tests

🔍
Solve Exercise 4
6 missing awaits to find and fix
📖
Read Stack Traces
Exercise 5: understanding error messages
🎯
In Module 2
You'll use this knowledge in real Page Objects
AI with Rufat
← / → · space