🔥 Service Worker Test Challenge: Is your PWA truly offline-proof with Playwright?
📌 Problem Statement
Service Workers (SWs) are vital for PWA offline capabilities, but testing their separate execution context and lifecycle events is complex. Simulating network disconnections and verifying cached content in CI/CD requires precise browser automation.
💡 Solution & Code Walkthrough
Playwright offers powerful APIs to interact with Service Workers, enabling robust testing of offline functionality. The core strategy involves waiting for SW activation, simulating network offline, then asserting that content is served from cache.
✅ Playwright Offline SW Test Strategy:
• Navigate to your PWA.
• Wait for the Service Worker to register and activate.
• Cut off network access using Playwright.
• Reload or navigate to the PWA; SW should serve cached assets.
• Assert the presence of expected offline content.
import { test, expect } from '@playwright/test'; // Your PWA's URL (e.g., served by a static server) const PWA_URL = 'http://localhost:8080'; test('should cache and serve PWA content offline', async ({ page, context }) => { await page.goto(PWA_URL); // 1. Get and wait for the Service Worker to activate const serviceWorker = await context.waitForEvent('serviceworker'); await serviceWorker.waitForActivated(); console.log(`SW ready: ${serviceWorker.url()}`); // 2. Simulate offline network conditions await context.setOffline(true); // 3. Navigate again while offline; content should be cached await page.goto(PWA_URL); // 4. Verify cached content (user-visible) expect(await page.title()).toContain('My Offline App'); // Check title expect(await page.textContent('h1')).toContain('Welcome Back Offline!'); // Check heading }); 🔑 Key Takeaways
• context.waitForEvent('serviceworker'): Essential for accessing the SW instance.
• serviceWorker.waitForActivated(): Ensures the SW is active and intercepting requests.
• context.setOffline(true): Playwright's powerful way to simulate network disconnection.
• Assert user-visible content (page.title(), page.textContent()) after going offline to confirm SW success.
❓ Quick Summary Q&A
Q: How do you get a Service Worker instance?
A: Use context.waitForEvent('serviceworker') after navigation.
Q: How do you simulate offline in Playwright?
A: Call context.setOffline(true).
TAGS: playwright, service worker, pwa, testing, automation, typescript, e2e, offline, webdev
────────────────────────────────────────
Download our app for more advanced Playwright tutorials!
────────────────────────────────────────
📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
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_20261107
🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261107&mt=8
────────────────────────────────────────
────────────────────────────────────────

