question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Testing an extension's content-script

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
k15acommented, Feb 25, 2020

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()
1reaction
pavelfeldmancommented, Feb 27, 2020

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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How do i test content-scripts in chrome extensions ...
I created test extension for this. It navigates to the predefined list of pages and checked for expected modifications on each page ...
Read more >
Content scripts - MDN Web Docs - Mozilla
A content script is a part of your extension that runs in the context of a particular web page (as opposed to background...
Read more >
Chrome Extensions: Content Scripts - Programming with Text
In this video, I cover " content scripts " for chrome extensions. The content script is a JavaScript file that runs in the...
Read more >
Advanced chrome extension development - Educative.io
Learn the 24 patterns to solve any coding interview question without getting lost in a maze ... sendMessage() api to the content script...
Read more >
Chrome Extensions getting started guides
Overview of all Chrome Extensions getting started guides. ... Table of contents ... choosing a project structure, and using Typescript.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found