Reputation: 6397
I'm using playwright to test a PWA (Progressive Web App) and I want to test the offline behavior.
browserContext has a setOffline method.
When calling playwright as a library, I can set browserContext
like this:
// example.js
const playwright = require('playwright');
(async () => {
const browser = await playwright.chromium.launch({
channel: 'msedge',
});
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://www.microsoft.com/edge');
await context.setOffline(true);
await page.screenshot({ path: 'example.png' });
await browser.close();
})();
But for my PWA, I'm using the bundled playwright test runner.
I have configured my test runner to use 5 devices-- desktop Chrome, Firefox, and Webkit, as well as mobile Webkit and mobile Chrome. I want to enter and exit offline mode in all of them within the test to confirm my PWA's behavior.
How can I set browserContext
in the test runner? It doesn't seem to be available within the test.
Upvotes: 7
Views: 15578
Reputation: 69
This should be even possible in config itself, something like that :
testDir: 'testStaging',
use: {
screenshot: 'only-on-failure',
baseURL: process.env.SITE,
contextOptions: {offline: true},
httpCredentials: {
username: ???,
password: ???,
},
},
For example, this is my solution. After using this, you have probably defined all projects, so your five devices/browsers and this will be valid for all of them.
Upvotes: 1
Reputation: 677
Never actually tried this, but as I see it, just taking the context instance into the async function should be fine:
test('context', async({ page, context }) => {
await page.goto('https://www.microsoft.com/edge');
await context.setOffline(true);
await page.screenshot({ path: 'example.png' });
});
Upvotes: 12