12. Orchestrating Async Browser Workflows

The Golden Rule: Always await Browser Actions

// ❌ WRONG: Fires click asynchronously and immediately tries to fill before page is ready!
page.click("#login-btn");
page.fill("#code", "1234");

// ✅ CORRECT: Awaits click completion and actionability:
await page.click("#login-btn");
await page.fill("#code", "1234");

Sequential vs Parallel Automation

// Sequential: Step 2 requires Step 1
await page.goto("/checkout");
await page.fill("#address", "123 Main St");
await page.click("#pay-button");

// Parallel: Preparing independent test preconditions
const [userProfile, cartItems] = await Promise.all([
fetchUserProfile(userId),
fetchCartContents(userId)
]);

Task

  • Implement pollUntilCondition:
  • 1. Accept predicate async function and timeoutMs (default 5000).
  • 2. Repeatedly call predicate() until it returns true.
  • 3. Throw an Error if timeoutMs expires before passing.
💡Common doubts
Did you like the problem?
Lesson 12 of 35
JavaScript
Loading code editor…
Next