Adding a Playwright test for offline access
Service workers tend to have a specific intent. In our case, the service worker enables the application to be used offline: loading the application causes it to be cached. If the network is no longer accessible, the next page load will be served from this cache, courtesy of the service worker.
Therefore, the Playwright test needs to test the application’s behavior when there’s no network connection.
At the time of writing, Playwright’s support for service worker events is experimental, so it needs to be enabled using the PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS
flag in your package.json
file:
{ "scripts": { ..., "test": "PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS=1 playwright test", ... } }
Once that’s done, we’re ready to write our tests. We need two helper functions...