DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

How do you reliably wait for the completion of a large, dynamically loaded data set in Playwright without using arbitrar

πŸ”₯ 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 });  
Enter fullscreen mode Exit fullscreen mode

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  
]);  
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ 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)