mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: allow py code blocks for python (#18799)
Turns out, we have some snippets that use `py` instead of `python`.
This commit is contained in:
parent
bc6617b4ca
commit
1b0a8122ba
@ -99,8 +99,11 @@ 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.
|
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
|
```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";
|
import path from "path";
|
||||||
|
|
||||||
export const test = base.extend<{
|
export const test = base.extend<{
|
||||||
@ -108,8 +111,8 @@ export const test = base.extend<{
|
|||||||
extensionId: string;
|
extensionId: string;
|
||||||
}>({
|
}>({
|
||||||
context: async ({ }, use) => {
|
context: async ({ }, use) => {
|
||||||
const pathToExtension = path.join(__dirname, "my-extension");
|
const pathToExtension = path.join(__dirname, 'my-extension');
|
||||||
const context = await chromium.launchPersistentContext("", {
|
const context = await chromium.launchPersistentContext('', {
|
||||||
headless: false,
|
headless: false,
|
||||||
args: [
|
args: [
|
||||||
`--disable-extensions-except=${pathToExtension}`,
|
`--disable-extensions-except=${pathToExtension}`,
|
||||||
@ -124,31 +127,22 @@ export const test = base.extend<{
|
|||||||
// for manifest v2:
|
// for manifest v2:
|
||||||
let [background] = context.backgroundPages()
|
let [background] = context.backgroundPages()
|
||||||
if (!background)
|
if (!background)
|
||||||
background = await context.waitForEvent("backgroundpage")
|
background = await context.waitForEvent('backgroundpage')
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// for manifest v3:
|
// for manifest v3:
|
||||||
let [background] = context.serviceWorkers();
|
let [background] = context.serviceWorkers();
|
||||||
if (!background)
|
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);
|
await use(extensionId);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
export const expect = test.expect;
|
||||||
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");
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```py
|
```python
|
||||||
# conftest.py
|
# conftest.py
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -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
|
# test_foo.py
|
||||||
from playwright.sync_api import expect, Page
|
from playwright.sync_api import expect, Page
|
||||||
|
|
||||||
|
|||||||
@ -492,6 +492,8 @@ function parseCodeLang(codeLang) {
|
|||||||
if (!language) {
|
if (!language) {
|
||||||
if (highlighter === 'ts')
|
if (highlighter === 'ts')
|
||||||
language = 'js';
|
language = 'js';
|
||||||
|
else if (highlighter === 'py')
|
||||||
|
language = 'python';
|
||||||
else if (['js', 'python', 'csharp', 'java'].includes(highlighter))
|
else if (['js', 'python', 'csharp', 'java'].includes(highlighter))
|
||||||
language = highlighter;
|
language = highlighter;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user