Skip to content

Commit

Permalink
cherry-pick(#18799): chore: allow py code blocks for python
Browse files Browse the repository at this point in the history
Turns out, we have some snippets that use `py` instead of `python`.
  • Loading branch information
dgozman authored and aslushnikov committed Nov 16, 2022
1 parent 5459be0 commit c3335e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
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
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

0 comments on commit c3335e4

Please sign in to comment.