Playwright: [Question] Testing an extension's content-script

Created on 22 Feb 2020  路  5Comments  路  Source: microsoft/playwright

I have an extension that I would like to test using playwright. I'm currently able to load up my extension on Chromium as shown in the examples, but that's about all I can do at this point.

Creating a new page defaults to creating it in incognito mode, regardless of whether a BrowserContext is created or not. By default, extensions are disabled in incognito mode.. ergo my extension cannot be tested.

What am I doing wrong?

Most helpful comment

I found a way to do that programmatically:

  await page.goto('chrome://extensions')

  const detailsButton = await page.evaluateHandle(() =>
    document
      .querySelector('body > extensions-manager')
      .shadowRoot.querySelector('#items-list')
      .shadowRoot.querySelector('extensions-item')
      .shadowRoot.querySelector('#detailsButton')
  )

  await detailsButton.click()

  const allowIncognitoButton = await page.evaluateHandle(() =>
    document
      .querySelector('body > extensions-manager')
      .shadowRoot.querySelector('#viewManager > extensions-detail-view')
      .shadowRoot.querySelector('#allow-incognito')
  )

  await allowIncognitoButton.click()

All 5 comments

Looks like this is not working yet: https://github.com/microsoft/playwright/blob/v0.11.1/docs/api.md#working-with-chrome-extensions last sentence.

I think you missed my point. An incognito context has no extensions enabled. Even if I don't want to _test_ my content script code, I can't even test for it's effects since it'll never run in an incognito context without me allowing my extension to run in incognito mode.
Is there a way to do that programmatically? If not, then you can't test extensions using playwright 馃槙

I found a way to do that programmatically:

  await page.goto('chrome://extensions')

  const detailsButton = await page.evaluateHandle(() =>
    document
      .querySelector('body > extensions-manager')
      .shadowRoot.querySelector('#items-list')
      .shadowRoot.querySelector('extensions-item')
      .shadowRoot.querySelector('#detailsButton')
  )

  await detailsButton.click()

  const allowIncognitoButton = await page.evaluateHandle(() =>
    document
      .querySelector('body > extensions-manager')
      .shadowRoot.querySelector('#viewManager > extensions-detail-view')
      .shadowRoot.querySelector('#allow-incognito')
  )

  await allowIncognitoButton.click()

You should use chromium.launchPersistent to access default profile and test extension content scripts.

Also, you can pierce shadow dom via:

await page.click('css=body > extensions-manager >> css=#items-list >> css=extensions-item >> css=#detailsButton');

(we'll get rid of those css= over time)

@pavelfeldman chromium.launchPersistent works fine, but this function returns BrowserContext object, not Browser. It is causing issue which you have no access to Browser object to make some operation on background tab. Thoughts? Do you have some idea how to hande it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JoelEinbinder picture JoelEinbinder  路  4Comments

KevinGrandon picture KevinGrandon  路  4Comments

osmenia picture osmenia  路  4Comments

juliomatcom picture juliomatcom  路  3Comments

andyricchuiti picture andyricchuiti  路  4Comments