diff --git a/packages/playwright-core/src/server/recorder/python.ts b/packages/playwright-core/src/server/recorder/python.ts index caf74dd57d..d9ddc0fd9c 100644 --- a/packages/playwright-core/src/server/recorder/python.ts +++ b/packages/playwright-core/src/server/recorder/python.ts @@ -164,7 +164,7 @@ def browser_context_args(browser_context_args, playwright) { return {${contextOptions}} } ` : ''; - formatter.add(`${options.deviceName ? 'import pytest\n' : ''} + formatter.add(`${options.deviceName ? 'import pytest\n' : ''}import re from playwright.sync_api import Page, expect ${fixture} @@ -172,7 +172,7 @@ def test_example(page: Page) -> None {`); } else if (this._isAsync) { formatter.add(` import asyncio - +import re from playwright.async_api import Playwright, async_playwright, expect @@ -181,6 +181,7 @@ async def run(playwright: Playwright) -> None { context = await browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`); } else { formatter.add(` +import re from playwright.sync_api import Playwright, sync_playwright, expect diff --git a/tests/library/inspector/cli-codegen-pytest.spec.ts b/tests/library/inspector/cli-codegen-pytest.spec.ts index 42a1eefa2e..e1bd608ef5 100644 --- a/tests/library/inspector/cli-codegen-pytest.spec.ts +++ b/tests/library/inspector/cli-codegen-pytest.spec.ts @@ -22,7 +22,8 @@ const emptyHTML = new URL('file://' + path.join(__dirname, '..', '..', 'assets', test('should print the correct imports and context options', async ({ runCLI }) => { const cli = runCLI(['--target=python-pytest', emptyHTML]); - const expectedResult = `from playwright.sync_api import Page, expect + const expectedResult = `import re +from playwright.sync_api import Page, expect def test_example(page: Page) -> None:`; @@ -39,7 +40,7 @@ test('should print the correct context options when using a device and lang', as await cli.waitForCleanExit(); const content = fs.readFileSync(tmpFile); expect(content.toString()).toBe(`import pytest - +import re from playwright.sync_api import Page, expect @@ -60,7 +61,8 @@ test('should save the codegen output to a file if specified', async ({ runCLI }, }); await cli.waitForCleanExit(); const content = fs.readFileSync(tmpFile); - expect(content.toString()).toBe(`from playwright.sync_api import Page, expect + expect(content.toString()).toBe(`import re +from playwright.sync_api import Page, expect def test_example(page: Page) -> None: diff --git a/tests/library/inspector/cli-codegen-python-async.spec.ts b/tests/library/inspector/cli-codegen-python-async.spec.ts index d765d55d9c..02647c5508 100644 --- a/tests/library/inspector/cli-codegen-python-async.spec.ts +++ b/tests/library/inspector/cli-codegen-python-async.spec.ts @@ -26,7 +26,7 @@ const launchOptions = (channel: string) => { test('should print the correct imports and context options', async ({ browserName, channel, runCLI }) => { const cli = runCLI(['--target=python-async', emptyHTML]); const expectedResult = `import asyncio - +import re from playwright.async_api import Playwright, async_playwright, expect @@ -39,7 +39,7 @@ async def run(playwright: Playwright) -> None: test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI }) => { const cli = runCLI(['--color-scheme=light', '--target=python-async', emptyHTML]); const expectedResult = `import asyncio - +import re from playwright.async_api import Playwright, async_playwright, expect @@ -54,7 +54,7 @@ test('should print the correct context options when using a device', async ({ br const cli = runCLI(['--device=Pixel 2', '--target=python-async', emptyHTML]); const expectedResult = `import asyncio - +import re from playwright.async_api import Playwright, async_playwright, expect @@ -69,7 +69,7 @@ test('should print the correct context options when using a device and additiona const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=python-async', emptyHTML]); const expectedResult = `import asyncio - +import re from playwright.async_api import Playwright, async_playwright, expect @@ -87,7 +87,7 @@ test('should save the codegen output to a file if specified', async ({ browserNa await cli.waitForCleanExit(); const content = fs.readFileSync(tmpFile); expect(content.toString()).toBe(`import asyncio - +import re from playwright.async_api import Playwright, async_playwright, expect @@ -118,7 +118,7 @@ test('should print load/save storage_state', async ({ browserName, channel, runC await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8'); const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=python-async', emptyHTML]); const expectedResult1 = `import asyncio - +import re from playwright.async_api import Playwright, async_playwright, expect diff --git a/tests/library/inspector/cli-codegen-python.spec.ts b/tests/library/inspector/cli-codegen-python.spec.ts index 5daa340a28..2bccbf3d93 100644 --- a/tests/library/inspector/cli-codegen-python.spec.ts +++ b/tests/library/inspector/cli-codegen-python.spec.ts @@ -25,7 +25,8 @@ const launchOptions = (channel: string) => { test('should print the correct imports and context options', async ({ runCLI, channel, browserName }) => { const cli = runCLI(['--target=python', emptyHTML]); - const expectedResult = `from playwright.sync_api import Playwright, sync_playwright, expect + const expectedResult = `import re +from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: @@ -36,7 +37,8 @@ def run(playwright: Playwright) -> None: test('should print the correct context options for custom settings', async ({ runCLI, channel, browserName }) => { const cli = runCLI(['--color-scheme=light', '--target=python', emptyHTML]); - const expectedResult = `from playwright.sync_api import Playwright, sync_playwright, expect + const expectedResult = `import re +from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: @@ -49,7 +51,8 @@ test('should print the correct context options when using a device', async ({ br test.skip(browserName !== 'chromium'); const cli = runCLI(['--device=Pixel 2', '--target=python', emptyHTML]); - const expectedResult = `from playwright.sync_api import Playwright, sync_playwright, expect + const expectedResult = `import re +from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: @@ -62,7 +65,8 @@ test('should print the correct context options when using a device and additiona test.skip(browserName !== 'webkit'); const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=python', emptyHTML]); - const expectedResult = `from playwright.sync_api import Playwright, sync_playwright, expect + const expectedResult = `import re +from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: @@ -78,7 +82,8 @@ test('should save the codegen output to a file if specified', async ({ runCLI, c }); await cli.waitForCleanExit(); const content = fs.readFileSync(tmpFile); - expect(content.toString()).toBe(`from playwright.sync_api import Playwright, sync_playwright, expect + expect(content.toString()).toBe(`import re +from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: @@ -103,7 +108,8 @@ test('should print load/save storage_state', async ({ runCLI, channel, browserNa const saveFileName = testInfo.outputPath('save.json'); await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8'); const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=python', emptyHTML]); - const expectedResult1 = `from playwright.sync_api import Playwright, sync_playwright, expect + const expectedResult1 = `import re +from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: