mirror of
https://github.com/microsoft/playwright.git
synced 2025-01-05 19:04:43 +03:00
test: can register service workers in an iframe (#30045)
References #29267.
This commit is contained in:
parent
a9fc4de37e
commit
348d0c2bfa
@ -350,3 +350,52 @@ it('service worker should cover the iframe', async ({ page, server }) => {
|
||||
|
||||
await expect(page.frameLocator('iframe').locator('div')).toHaveText('from the service worker');
|
||||
});
|
||||
|
||||
it('service worker should register in an iframe', async ({ page, server }) => {
|
||||
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/29267' });
|
||||
|
||||
server.setRoute('/main.html', (req, res) => {
|
||||
res.writeHead(200, { 'content-type': 'text/html' }).end(`
|
||||
<iframe src='/dir/iframe.html'></iframe>
|
||||
`);
|
||||
});
|
||||
|
||||
server.setRoute('/dir/iframe.html', (req, res) => {
|
||||
res.writeHead(200, { 'content-type': 'text/html' }).end(`
|
||||
<script>
|
||||
window.registrationPromise = navigator.serviceWorker.register('sw.js');
|
||||
window.activationPromise = new Promise(resolve => navigator.serviceWorker.oncontrollerchange = resolve);
|
||||
</script>
|
||||
`);
|
||||
});
|
||||
|
||||
server.setRoute('/dir/sw.js', (req, res) => {
|
||||
res.writeHead(200, { 'content-type': 'application/javascript' }).end(`
|
||||
const kIframeHtml = "<div>from the service worker</div>";
|
||||
|
||||
self.addEventListener('fetch', event => {
|
||||
if (event.request.url.endsWith('html')) {
|
||||
event.respondWith(fetch(event.request));
|
||||
return;
|
||||
}
|
||||
const blob = new Blob(['responseFromServiceWorker'], { type: 'text/plain' });
|
||||
const response = new Response(blob, { status: 200 , statusText: 'OK' });
|
||||
event.respondWith(response);
|
||||
});
|
||||
|
||||
self.addEventListener('activate', event => {
|
||||
event.waitUntil(clients.claim());
|
||||
});
|
||||
`);
|
||||
});
|
||||
|
||||
await page.goto(server.PREFIX + '/main.html');
|
||||
const iframe = page.frames()[1];
|
||||
await iframe.evaluate(() => window['activationPromise']);
|
||||
|
||||
const response = await iframe.evaluate(async () => {
|
||||
const response = await fetch('foo.txt');
|
||||
return response.text();
|
||||
});
|
||||
expect(response).toBe('responseFromServiceWorker');
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user