test: cover Service Worker update (#15537)
This commit is contained in:
parent
2ee235ff1b
commit
6219e945d5
|
|
@ -233,6 +233,116 @@ test('should intercept only serviceworker request, not page', async ({ context,
|
||||||
expect(response).toBe('from sw');
|
expect(response).toBe('from sw');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should emit new service worker on update', async ({ context, page, server }) => {
|
||||||
|
let version = 0;
|
||||||
|
server.setRoute('/worker.js', (req, res) => {
|
||||||
|
res.writeHead(200, 'OK', { 'Content-Type': 'text/javascript' });
|
||||||
|
res.write(`self.PW_VERSION = ${version++};`);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
server.setRoute('/home', (req, res) => {
|
||||||
|
res.write(`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Service Worker Update Demo</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<button id="update" disabled>update service worker</button>
|
||||||
|
<script>
|
||||||
|
const updateBtn = document.getElementById('update');
|
||||||
|
updateBtn.addEventListener('click', evt => {
|
||||||
|
evt.preventDefault();
|
||||||
|
registration.then(r => r.update());
|
||||||
|
});
|
||||||
|
|
||||||
|
const registration = new Promise(r => navigator.serviceWorker.register('/worker.js').then(r));
|
||||||
|
registration.then(() => updateBtn.disabled = false);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
const [ sw ] = await Promise.all([
|
||||||
|
context.waitForEvent('serviceworker'),
|
||||||
|
page.goto(server.PREFIX + '/home'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await expect.poll(() => sw.evaluate(() => self['PW_VERSION'])).toBe(0);
|
||||||
|
|
||||||
|
const [ updatedSW ] = await Promise.all([
|
||||||
|
context.waitForEvent('serviceworker'),
|
||||||
|
page.click('#update'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await expect.poll(() => updatedSW.evaluate(() => self['PW_VERSION'])).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should intercept service worker update requests', async ({ context, page, server }) => {
|
||||||
|
test.fixme();
|
||||||
|
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/14711' });
|
||||||
|
|
||||||
|
let version = 0;
|
||||||
|
server.setRoute('/worker.js', (req, res) => {
|
||||||
|
res.writeHead(200, 'OK', { 'Content-Type': 'text/javascript' });
|
||||||
|
res.write(`self.PW_VERSION = ${version++};`);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
server.setRoute('/home', (req, res) => {
|
||||||
|
res.write(`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Service Worker Update Demo</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<button id="update" disabled>update service worker</button>
|
||||||
|
<script>
|
||||||
|
const updateBtn = document.getElementById('update');
|
||||||
|
updateBtn.addEventListener('click', evt => {
|
||||||
|
evt.preventDefault();
|
||||||
|
registration.then(r => r.update());
|
||||||
|
});
|
||||||
|
|
||||||
|
const registration = new Promise(r => navigator.serviceWorker.register('/worker.js').then(r));
|
||||||
|
registration.then(() => updateBtn.disabled = false);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
const [ sw ] = await Promise.all([
|
||||||
|
context.waitForEvent('serviceworker'),
|
||||||
|
page.goto(server.PREFIX + '/home'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await expect.poll(() => sw.evaluate(() => self['PW_VERSION'])).toBe(0);
|
||||||
|
|
||||||
|
// Before triggering, let's intercept the update request
|
||||||
|
await context.route('**/worker.js', async route => {
|
||||||
|
await route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
body: `self.PW_VERSION = "intercepted";`,
|
||||||
|
contentType: 'text/javascript',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const [ updatedSW ] = await Promise.all([
|
||||||
|
context.waitForEvent('serviceworker'),
|
||||||
|
// currently times out here
|
||||||
|
context.waitForEvent('request', r => r.url().endsWith('worker.js')),
|
||||||
|
page.click('#update'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await expect.poll(() => updatedSW.evaluate(() => self['PW_VERSION'])).toBe('intercepted');
|
||||||
|
});
|
||||||
|
|
||||||
test('setOffline', async ({ context, page, server }) => {
|
test('setOffline', async ({ context, page, server }) => {
|
||||||
const [worker] = await Promise.all([
|
const [worker] = await Promise.all([
|
||||||
context.waitForEvent('serviceworker'),
|
context.waitForEvent('serviceworker'),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue