Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow py code blocks for python #18799

Merged
merged 1 commit into from Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 28 additions & 18 deletions docs/src/chrome-extensions-js-python.md
Expand Up @@ -99,17 +99,20 @@ with sync_playwright() as playwright:

To have the extension loaded when running tests you can use a test fixture to set the context. You can also dynamically retrieve the extension id and use it to load and test the popup page for example.

First, add fixtures that will load the extension:

```ts
import { test as base, expect, BrowserContext } from "@playwright/test";
// fixtures.ts
dgozman marked this conversation as resolved.
Show resolved Hide resolved
import { test as base, expect, chromium, type BrowserContext } from "@playwright/test";
import path from "path";

export const test = base.extend<{
context: BrowserContext;
extensionId: string;
}>({
context: async ({ }, use) => {
const pathToExtension = path.join(__dirname, "my-extension");
const context = await chromium.launchPersistentContext("", {
const pathToExtension = path.join(__dirname, 'my-extension');
const context = await chromium.launchPersistentContext('', {
headless: false,
args: [
`--disable-extensions-except=${pathToExtension}`,
Expand All @@ -124,31 +127,22 @@ export const test = base.extend<{
// for manifest v2:
let [background] = context.backgroundPages()
if (!background)
background = await context.waitForEvent("backgroundpage")
background = await context.waitForEvent('backgroundpage')
*/

// for manifest v3:
let [background] = context.serviceWorkers();
if (!background)
background = await context.waitForEvent("serviceworker");
background = await context.waitForEvent('serviceworker');

const extensionId = background.url().split("/")[2];
const extensionId = background.url().split('/')[2];
await use(extensionId);
},
});

test("example test", async ({ page }) => {
await page.goto("https://example.com");
await expect(page.locator("body")).toHaveText("Changed by my-extension");
});

test("popup page", async ({ page, extensionId }) => {
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await expect(page.locator("body")).toHaveText("my-extension popup");
});
export const expect = test.expect;
```

```py
```python
# conftest.py
from typing import Generator
from pathlib import Path
Expand Down Expand Up @@ -188,7 +182,23 @@ def extension_id(context) -> Generator[str, None, None]:

```

```py
Then use these fixtures in a test:

```ts
import { test, expect } from "./fixtures";

test("example test", async ({ page }) => {
await page.goto("https://example.com");
await expect(page.locator("body")).toHaveText("Changed by my-extension");
});

test("popup page", async ({ page, extensionId }) => {
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await expect(page.locator("body")).toHaveText("my-extension popup");
});
```

```python
# test_foo.py
from playwright.sync_api import expect, Page

Expand Down
2 changes: 2 additions & 0 deletions utils/markdown.js
Expand Up @@ -492,6 +492,8 @@ function parseCodeLang(codeLang) {
if (!language) {
if (highlighter === 'ts')
language = 'js';
else if (highlighter === 'py')
language = 'python';
else if (['js', 'python', 'csharp', 'java'].includes(highlighter))
language = highlighter;
}
Expand Down