Rayrun
← Back to Discord Forum

Test service worker in chrome extension with playwright

Hello,

I'm trying to test the code that is in the service worker of my chrome extension (manifest v3). The goal is to basically, open a web page, open the extension and test that the messaging that goes through the service worker is okay by writing some tests against it.

This is my code:

const {test, chromium} = require('@playwright/test');
import path from 'path';

export const createBrowserContext = async () => {
    const pathToExtension = path.join(__dirname, '../build');
    return await chromium.launchPersistentContext('', {
        headless: false,
        args: [
            `--disable-extensions-except=${pathToExtension}`,
            `--load-extension=${pathToExtension}`
                        `--auto-open-devtools-for-tabs`
        ],
        ignoreDefaultArgs: ['--disable-component-extensions-with-background-pages']
    });
};

test('test serviceworker', async ({}) => {
    const browserContext = await createBrowserContext();
    let [background] = browserContext.serviceWorkers();


    if (!background)
        background = await browserContext.waitForEvent('serviceworker');

  // here I'm trying to intercept the logs
  background.on('console', message => {
    console.log(message.text())
  });
    const extensionId = background.url().split('/')[2];

  // this is the page I want to test my extension against
    const page = await browserContext.newPage();
    await page.goto(`wwww.some-url.com`);

    browserContext.close();
});

In the code above I never get to console.log(message.text()) even though i see the logs in chrome://serviceworker-internals.

Any idea ?

Thanks!

This thread is trying to answer question "Why am I unable to intercept the logs in my service worker testing code using Playwright?"

0 replies

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].