From 90af7a7ee0d086c84960a9837d96db646a7701dd Mon Sep 17 00:00:00 2001 From: Boris Osipov Date: Thu, 5 Jan 2023 22:08:16 +0300 Subject: [PATCH] docs(chrome-extensions-js-python.md) add headless testing docs (#19812) See https://github.com/microsoft/playwright/issues/19233 --- docs/src/chrome-extensions-js-python.md | 35 ++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/src/chrome-extensions-js-python.md b/docs/src/chrome-extensions-js-python.md index 648d4b3b85..94918417d4 100644 --- a/docs/src/chrome-extensions-js-python.md +++ b/docs/src/chrome-extensions-js-python.md @@ -4,7 +4,7 @@ title: "Chrome Extensions" --- :::note -Extensions only work in Chrome / Chromium in non-headless mode, launched with a persistent context. +Extensions only work in Chrome / Chromium launched with a persistent context. ::: The following is code for getting a handle to the [background page](https://developer.chrome.com/extensions/background_pages) of a [Manifest v2](https://developer.chrome.com/docs/extensions/mv2/) extension whose source is located in `./my-extension`: @@ -212,3 +212,36 @@ def test_popup_page(page: Page, extension_id: str) -> None: page.goto(f"chrome-extension://{extension_id}/popup.html") expect(page.locator("body")).to_have_text("my-extension popup") ``` + +## Headless mode + +By default, Chrome's headless mode in Playwright does not support Chrome extensions. To overcome this limitation, you can run Chrome's persistent context with a new headless mode by using the following code: +```ts +// fixtures.ts +// ... + +const pathToExtension = path.join(__dirname, 'my-extension'); +const context = await chromium.launchPersistentContext('', { + headless: false, + args: [ + `--headless=chrome`, // the new headless arg + `--disable-extensions-except=${pathToExtension}`, + `--load-extension=${pathToExtension}`, + ], +}); +// ... +``` + +```python +# conftest.py +path_to_extension = Path(__file__).parent.joinpath("my-extension") +context = playwright.chromium.launch_persistent_context( + "", + headless=False, + args=[ + "--headless=chrome", # the new headless arg + f"--disable-extensions-except={path_to_extension}", + f"--load-extension={path_to_extension}", + ], +) +``` \ No newline at end of file