docs(chrome-extensions-js-python.md) add headless testing docs (#19812)

See https://github.com/microsoft/playwright/issues/19233
This commit is contained in:
Boris Osipov 2023-01-05 22:08:16 +03:00 committed by GitHub
parent 9f7b0e4e01
commit 90af7a7ee0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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}",
],
)
```