π₯ Playwright Interview Challenge: Wait for Dynamic Data Without Sleeps?
Youβre testing a high-throughput dashboard that loads 150+ rows in batches. Tests fail intermittently due to race conditions. How do you reliably wait for full data rendering?
π Problem Statement
Tests proceed to assertions before final data loads, causing flaky failures. Simple waitForSelector() isnβt enough for dynamic, batched API responses.
π‘ Solution & Code Walkthrough
Approach 1: UI Count Assertion (UI-Centric)
const expectedCount = 150;
const dataRows = page.locator('.data-row');
// Playwrightβs expect() auto-retries until count matches or times out
await expect(dataRows).toHaveCount(expectedCount, { timeout: 30000 });
Approach 2: Network Event Listener (API-Centric)
// Wait for all API calls to complete before proceeding
const [response] = await Promise.all([
page.waitForResponse(resp => resp.url().includes('/api/data') && resp.status() === 200),
page.click('#load-data-btn') // Trigger data load
]);
π Key Takeaways
β’ Replace sleep() with condition-based waits (e.g., toHaveCount(), waitForResponse()).
β’ Use timeout to define acceptable wait bounds.
β’ Monitor API/network events for backend-driven completion signals.
β Quick Summary Q&A
Q: Whatβs the best way to wait for dynamic data in Playwright?
A: Use expect().toHaveCount() for UI stability or waitForResponse() for API-driven flows.
TAGS: playwright, testing, automation, javascript
ββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββ
π² π
πππ ππππππ πππ β πππ+ ππππ π&ππ¬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:
π€ ππ¨π¨π π₯π ππ₯ππ² (ππ§ππ«π¨π’π):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20260906
π ππ©π© πππ¨π«π (π’ππ):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20260906&mt=8
ββββββββββββββββββββββββββββββββββββββββ
Top comments (0)