Use WebWorker.close() method from new Puppeteer version (#1085)

This commit is contained in:
Oliver Dunk
2024-02-21 12:58:18 +00:00
committed by GitHub
parent 3f67a327c8
commit 4ce29a4682
3 changed files with 343 additions and 267 deletions

View File

@@ -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 () => {
@@ -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');

File diff suppressed because it is too large Load Diff

View File

@@ -3,7 +3,7 @@
"version": "1.0",
"dependencies": {
"jest": "^29.7.0",
"puppeteer": "^21.3.6"
"puppeteer": "^22.1.0"
},
"scripts": {
"start": "jest ."