Skip to content

Commit

Permalink
Use WebWorker.close() method from new Puppeteer version (GoogleChrome…
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverdunk authored Feb 21, 2024
1 parent 3f67a32 commit 4ce29a4
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 267 deletions.
28 changes: 7 additions & 21 deletions functional-samples/tutorial.terminate-sw/puppeteer/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,18 @@ afterEach(async () => {
* by creating a new Chrome DevTools Protocol session, finding the target ID
* associated with the worker and running the Target.closeTarget command.
*
* @param {Page} page Puppeteer page that CDP session can be started from
* @param {Page} browser Browser instance
* @param {string} extensionId Extension ID of worker to terminate
*/
async function stopServiceWorker(page, extensionId) {
async function stopServiceWorker(browser, extensionId) {
const host = `chrome-extension://${extensionId}`;

// Create a new CDP session
const client = await page.target().createCDPSession();

// Find the extension service worker
const targets = await client.send('Target.getTargets');
const worker = targets.targetInfos.find(
(t) => t.type === 'service_worker' && t.url.startsWith(host)
);

if (!worker) {
throw new Error(`No worker found for ${host}`);
}

// Terminate the service worker
await client.send('Target.closeTarget', {
targetId: worker.targetId
const target = await browser.waitForTarget((t) => {
return t.type() === 'service_worker' && t.url().startsWith(host);
});

// End the CDP session
await client.detach();
const worker = await target.worker();
await worker.close();
}

test('can message service worker when terminated', async () => {
Expand All @@ -78,7 +64,7 @@ test('can message service worker when terminated', async () => {
await page.waitForSelector('#response-0');

// Terminate service worker
await stopServiceWorker(page, EXTENSION_ID);
await stopServiceWorker(browser, EXTENSION_ID);

// Try to send another message
await page.click('button');
Expand Down
Loading

0 comments on commit 4ce29a4

Please sign in to comment.