diff --git a/docs/src/api/python.md b/docs/src/api/python.md index 358b12149a..c6029f9fc8 100644 --- a/docs/src/api/python.md +++ b/docs/src/api/python.md @@ -4,7 +4,7 @@ Terminates this instance of Playwright in case it was created bypassing the Python context manager. This is useful in REPL applications. ```py ->>> from playwright import sync_playwright +>>> from playwright.sync_api import sync_playwright >>> playwright = sync_playwright().start() diff --git a/docs/src/auth.md b/docs/src/auth.md index 536d67e1be..97a067f12e 100644 --- a/docs/src/auth.md +++ b/docs/src/auth.md @@ -219,7 +219,7 @@ const context = await chromium.launchPersistentContext(userDataDir, { headless: ```python async import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def main(): async with async_playwright() as p: @@ -231,7 +231,7 @@ asyncio.get_event_loop().run_until_complete(main()) ``` ```python sync -from playwright import sync_playwright +from playwright.sync_api import sync_playwright with sync_playwright() as p: user_data_dir = '/path/to/directory' diff --git a/docs/src/ci.md b/docs/src/ci.md index f6529aaa02..8c1ee56059 100644 --- a/docs/src/ci.md +++ b/docs/src/ci.md @@ -289,7 +289,7 @@ const browser = await chromium.launch({ headless: false }); ```python async import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def main(): async with async_playwright() as p: @@ -300,7 +300,7 @@ asyncio.get_event_loop().run_until_complete(main()) ``` ```python sync -from playwright import sync_playwright +from playwright.sync_api import sync_playwright with sync_playwright() as p: # Works across chromium, firefox and webkit diff --git a/docs/src/core-concepts.md b/docs/src/core-concepts.md index 6aa2bdcf36..19e5b77c1b 100644 --- a/docs/src/core-concepts.md +++ b/docs/src/core-concepts.md @@ -31,7 +31,7 @@ await browser.close(); ```python async import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def main(): async with async_playwright() as p: @@ -42,7 +42,7 @@ asyncio.get_event_loop().run_until_complete(main()) ``` ```python sync -from playwright import sync_playwright +from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch(headless=False) @@ -97,7 +97,7 @@ const context = await browser.newContext({ ```python async import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def main(): async with async_playwright() as p: @@ -117,7 +117,7 @@ asyncio.get_event_loop().run_until_complete(main()) ``` ```python sync -from playwright import sync_playwright +from playwright.sync_api import sync_playwright with sync_playwright() as p: iphone_11 = p.devices['iPhone 11 Pro'] diff --git a/docs/src/emulation.md b/docs/src/emulation.md index 691ff5b9c7..1216c2ab79 100644 --- a/docs/src/emulation.md +++ b/docs/src/emulation.md @@ -31,7 +31,7 @@ const context = await browser.newContext({ ```python async import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def main(): async with async_playwright() as p: @@ -45,7 +45,7 @@ asyncio.get_event_loop().run_until_complete(main()) ``` ```python sync -from playwright import sync_playwright +from playwright.sync_api import sync_playwright with sync_playwright() as p: pixel_2 = p.devices['Pixel 2'] diff --git a/docs/src/intro-python.md b/docs/src/intro-python.md index 6cf8ae390c..1642ed4c23 100644 --- a/docs/src/intro-python.md +++ b/docs/src/intro-python.md @@ -20,35 +20,40 @@ These commands download the Playwright package and install browser binaries for Once installed, you can `import` Playwright in a Python script, and launch any of the 3 browsers (`chromium`, `firefox` and `webkit`). -```python -from playwright import sync_playwright +```py +from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page() - # interact with UI elements, assert values + page.goto("http://webkit.org") + print(page.title()) browser.close() ``` -Playwright supports two variations of the API: synchronous are asynchronous. If your modern project uses -[asyncio](https://docs.python.org/3/library/asyncio.html), you should use async API: +Playwright supports two variations of the API: synchronous are asynchronous. If your modern project uses [asyncio](https://docs.python.org/3/library/asyncio.html), you should use async API: -```python -from playwright import async_playwright +```py +import asyncio +from playwright.async_api import async_playwright -with async_playwright() as p: - browser = await p.chromium.launch() - page = await browser.new_page() - # interact with UI elements, assert values - await browser.close() +async def main(): + async with async_playwright() as p: + browser = await p.chromium.launch() + page = await browser.new_page() + await page.goto("http://webkit.org") + print(await page.title()) + await browser.close() + +asyncio.run(main()) ``` ## First script In our first script, we will navigate to `whatsmyuseragent.org` and take a screenshot in WebKit. -```python -from playwright import sync_playwright +```py +from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.webkit.launch() diff --git a/src/cli/codegen/languages/python.ts b/src/cli/codegen/languages/python.ts index 835b1caedd..dadffc4d4e 100644 --- a/src/cli/codegen/languages/python.ts +++ b/src/cli/codegen/languages/python.ts @@ -151,14 +151,14 @@ export class PythonLanguageGenerator implements LanguageGenerator { if (this._isAsync) { formatter.add(` import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def run(playwright) { browser = await playwright.${browserName}.launch(${formatOptions(launchOptions, false)}) context = await browser.newContext(${formatContextOptions(contextOptions, deviceName)})`); } else { formatter.add(` -from playwright import sync_playwright +from playwright.sync_api import sync_playwright def run(playwright) { browser = playwright.${browserName}.launch(${formatOptions(launchOptions, false)}) diff --git a/test/cli/cli-codegen-python-async.spec.ts b/test/cli/cli-codegen-python-async.spec.ts index 32194abe0c..844987ce4f 100644 --- a/test/cli/cli-codegen-python-async.spec.ts +++ b/test/cli/cli-codegen-python-async.spec.ts @@ -25,7 +25,7 @@ const emptyHTML = new URL('file://' + path.join(__dirname, '..', 'assets', 'empt it('should print the correct imports and context options', async ({ runCLI }) => { const cli = runCLI(['codegen', '--target=python-async', emptyHTML]); const expectedResult = `import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def run(playwright): browser = await playwright.chromium.launch(headless=False) @@ -37,7 +37,7 @@ async def run(playwright): it('should print the correct context options for custom settings', async ({ runCLI }) => { const cli = runCLI(['--color-scheme=light', 'codegen', '--target=python-async', emptyHTML]); const expectedResult = `import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def run(playwright): browser = await playwright.chromium.launch(headless=False) @@ -49,7 +49,7 @@ async def run(playwright): it('should print the correct context options when using a device', async ({ runCLI }) => { const cli = runCLI(['--device=Pixel 2', 'codegen', '--target=python-async', emptyHTML]); const expectedResult = `import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def run(playwright): browser = await playwright.chromium.launch(headless=False) @@ -61,7 +61,7 @@ async def run(playwright): it('should print the correct context options when using a device and additional options', async ({ runCLI }) => { const cli = runCLI(['--color-scheme=light', '--device=Pixel 2', 'codegen', '--target=python-async', emptyHTML]); const expectedResult = `import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def run(playwright): browser = await playwright.chromium.launch(headless=False) @@ -76,7 +76,7 @@ it('should save the codegen output to a file if specified', async ({ runCLI, tes await cli.exited; const content = await fs.readFileSync(tmpFile); expect(content.toString()).toBe(`import asyncio -from playwright import async_playwright +from playwright.async_api import async_playwright async def run(playwright): browser = await playwright.chromium.launch(headless=False) @@ -107,7 +107,7 @@ it('should print load/save storageState', async ({ runCLI, testInfo }) => { await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8'); const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, 'codegen', '--target=python-async', emptyHTML]); const expectedResult = `import asyncio - from playwright import async_playwright + from playwright.async_api import async_playwright async def run(playwright): browser = await playwright.chromium.launch(headless=False) diff --git a/test/cli/cli-codegen-python.spec.ts b/test/cli/cli-codegen-python.spec.ts index f7c1426f0a..2638f556c3 100644 --- a/test/cli/cli-codegen-python.spec.ts +++ b/test/cli/cli-codegen-python.spec.ts @@ -24,7 +24,7 @@ const emptyHTML = new URL('file://' + path.join(__dirname, '..', 'assets', 'empt it('should print the correct imports and context options', async ({ runCLI }) => { const cli = runCLI(['codegen', '--target=python', emptyHTML]); - const expectedResult = `from playwright import sync_playwright + const expectedResult = `from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False) @@ -35,7 +35,7 @@ def run(playwright): it('should print the correct context options for custom settings', async ({ runCLI }) => { const cli = runCLI(['--color-scheme=light', 'codegen', '--target=python', emptyHTML]); - const expectedResult = `from playwright import sync_playwright + const expectedResult = `from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False) @@ -46,7 +46,7 @@ def run(playwright): it('should print the correct context options when using a device', async ({ runCLI }) => { const cli = runCLI(['--device=Pixel 2', 'codegen', '--target=python', emptyHTML]); - const expectedResult = `from playwright import sync_playwright + const expectedResult = `from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False) @@ -57,7 +57,7 @@ def run(playwright): it('should print the correct context options when using a device and additional options', async ({ runCLI }) => { const cli = runCLI(['--color-scheme=light', '--device=Pixel 2', 'codegen', '--target=python', emptyHTML]); - const expectedResult = `from playwright import sync_playwright + const expectedResult = `from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False) @@ -71,7 +71,7 @@ it('should save the codegen output to a file if specified', async ({ runCLI, tes const cli = runCLI(['codegen', '--target=python', '--output', tmpFile, emptyHTML]); await cli.exited; const content = fs.readFileSync(tmpFile); - expect(content.toString()).toBe(`from playwright import sync_playwright + expect(content.toString()).toBe(`from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False) @@ -99,7 +99,7 @@ it('should print load/save storageState', async ({ runCLI, testInfo }) => { 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}`, 'codegen', '--target=python', emptyHTML]); - const expectedResult = `from playwright import sync_playwright + const expectedResult = `from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False)