fix(codegen): import re in python (#30026)

Fixes https://github.com/microsoft/playwright/issues/30019
This commit is contained in:
Yury Semikhatsky 2024-03-20 16:38:28 -07:00 committed by GitHub
parent 8a1ff34578
commit 69e90f38c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 17 deletions

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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: