chore: update snippets in python docs (#4976)

This commit is contained in:
Pavel Feldman 2021-01-11 17:04:24 -08:00 committed by GitHub
parent 7665a6ec7f
commit cb6e4a6657
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 44 additions and 39 deletions

View file

@ -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. Terminates this instance of Playwright in case it was created bypassing the Python context manager. This is useful in REPL applications.
```py ```py
>>> from playwright import sync_playwright >>> from playwright.sync_api import sync_playwright
>>> playwright = sync_playwright().start() >>> playwright = sync_playwright().start()

View file

@ -219,7 +219,7 @@ const context = await chromium.launchPersistentContext(userDataDir, { headless:
```python async ```python async
import asyncio import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def main(): async def main():
async with async_playwright() as p: async with async_playwright() as p:
@ -231,7 +231,7 @@ asyncio.get_event_loop().run_until_complete(main())
``` ```
```python sync ```python sync
from playwright import sync_playwright from playwright.sync_api import sync_playwright
with sync_playwright() as p: with sync_playwright() as p:
user_data_dir = '/path/to/directory' user_data_dir = '/path/to/directory'

View file

@ -289,7 +289,7 @@ const browser = await chromium.launch({ headless: false });
```python async ```python async
import asyncio import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def main(): async def main():
async with async_playwright() as p: async with async_playwright() as p:
@ -300,7 +300,7 @@ asyncio.get_event_loop().run_until_complete(main())
``` ```
```python sync ```python sync
from playwright import sync_playwright from playwright.sync_api import sync_playwright
with sync_playwright() as p: with sync_playwright() as p:
# Works across chromium, firefox and webkit # Works across chromium, firefox and webkit

View file

@ -31,7 +31,7 @@ await browser.close();
```python async ```python async
import asyncio import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def main(): async def main():
async with async_playwright() as p: async with async_playwright() as p:
@ -42,7 +42,7 @@ asyncio.get_event_loop().run_until_complete(main())
``` ```
```python sync ```python sync
from playwright import sync_playwright from playwright.sync_api import sync_playwright
with sync_playwright() as p: with sync_playwright() as p:
browser = p.chromium.launch(headless=False) browser = p.chromium.launch(headless=False)
@ -97,7 +97,7 @@ const context = await browser.newContext({
```python async ```python async
import asyncio import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def main(): async def main():
async with async_playwright() as p: async with async_playwright() as p:
@ -117,7 +117,7 @@ asyncio.get_event_loop().run_until_complete(main())
``` ```
```python sync ```python sync
from playwright import sync_playwright from playwright.sync_api import sync_playwright
with sync_playwright() as p: with sync_playwright() as p:
iphone_11 = p.devices['iPhone 11 Pro'] iphone_11 = p.devices['iPhone 11 Pro']

View file

@ -31,7 +31,7 @@ const context = await browser.newContext({
```python async ```python async
import asyncio import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def main(): async def main():
async with async_playwright() as p: async with async_playwright() as p:
@ -45,7 +45,7 @@ asyncio.get_event_loop().run_until_complete(main())
``` ```
```python sync ```python sync
from playwright import sync_playwright from playwright.sync_api import sync_playwright
with sync_playwright() as p: with sync_playwright() as p:
pixel_2 = p.devices['Pixel 2'] pixel_2 = p.devices['Pixel 2']

View file

@ -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`). Once installed, you can `import` Playwright in a Python script, and launch any of the 3 browsers (`chromium`, `firefox` and `webkit`).
```python ```py
from playwright import sync_playwright from playwright.sync_api import sync_playwright
with sync_playwright() as p: with sync_playwright() as p:
browser = p.chromium.launch() browser = p.chromium.launch()
page = browser.new_page() page = browser.new_page()
# interact with UI elements, assert values page.goto("http://webkit.org")
print(page.title())
browser.close() browser.close()
``` ```
Playwright supports two variations of the API: synchronous are asynchronous. If your modern project uses 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:
[asyncio](https://docs.python.org/3/library/asyncio.html), you should use async API:
```python ```py
from playwright import async_playwright import asyncio
from playwright.async_api import async_playwright
with async_playwright() as p: async def main():
async with async_playwright() as p:
browser = await p.chromium.launch() browser = await p.chromium.launch()
page = await browser.new_page() page = await browser.new_page()
# interact with UI elements, assert values await page.goto("http://webkit.org")
print(await page.title())
await browser.close() await browser.close()
asyncio.run(main())
``` ```
## First script ## First script
In our first script, we will navigate to `whatsmyuseragent.org` and take a screenshot in WebKit. In our first script, we will navigate to `whatsmyuseragent.org` and take a screenshot in WebKit.
```python ```py
from playwright import sync_playwright from playwright.sync_api import sync_playwright
with sync_playwright() as p: with sync_playwright() as p:
browser = p.webkit.launch() browser = p.webkit.launch()

View file

@ -151,14 +151,14 @@ export class PythonLanguageGenerator implements LanguageGenerator {
if (this._isAsync) { if (this._isAsync) {
formatter.add(` formatter.add(`
import asyncio import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def run(playwright) { async def run(playwright) {
browser = await playwright.${browserName}.launch(${formatOptions(launchOptions, false)}) browser = await playwright.${browserName}.launch(${formatOptions(launchOptions, false)})
context = await browser.newContext(${formatContextOptions(contextOptions, deviceName)})`); context = await browser.newContext(${formatContextOptions(contextOptions, deviceName)})`);
} else { } else {
formatter.add(` formatter.add(`
from playwright import sync_playwright from playwright.sync_api import sync_playwright
def run(playwright) { def run(playwright) {
browser = playwright.${browserName}.launch(${formatOptions(launchOptions, false)}) browser = playwright.${browserName}.launch(${formatOptions(launchOptions, false)})

View file

@ -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 }) => { it('should print the correct imports and context options', async ({ runCLI }) => {
const cli = runCLI(['codegen', '--target=python-async', emptyHTML]); const cli = runCLI(['codegen', '--target=python-async', emptyHTML]);
const expectedResult = `import asyncio const expectedResult = `import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def run(playwright): async def run(playwright):
browser = await playwright.chromium.launch(headless=False) 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 }) => { it('should print the correct context options for custom settings', async ({ runCLI }) => {
const cli = runCLI(['--color-scheme=light', 'codegen', '--target=python-async', emptyHTML]); const cli = runCLI(['--color-scheme=light', 'codegen', '--target=python-async', emptyHTML]);
const expectedResult = `import asyncio const expectedResult = `import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def run(playwright): async def run(playwright):
browser = await playwright.chromium.launch(headless=False) 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 }) => { 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 cli = runCLI(['--device=Pixel 2', 'codegen', '--target=python-async', emptyHTML]);
const expectedResult = `import asyncio const expectedResult = `import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def run(playwright): async def run(playwright):
browser = await playwright.chromium.launch(headless=False) 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 }) => { 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 cli = runCLI(['--color-scheme=light', '--device=Pixel 2', 'codegen', '--target=python-async', emptyHTML]);
const expectedResult = `import asyncio const expectedResult = `import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def run(playwright): async def run(playwright):
browser = await playwright.chromium.launch(headless=False) 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; await cli.exited;
const content = await fs.readFileSync(tmpFile); const content = await fs.readFileSync(tmpFile);
expect(content.toString()).toBe(`import asyncio expect(content.toString()).toBe(`import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def run(playwright): async def run(playwright):
browser = await playwright.chromium.launch(headless=False) 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'); 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 cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, 'codegen', '--target=python-async', emptyHTML]);
const expectedResult = `import asyncio const expectedResult = `import asyncio
from playwright import async_playwright from playwright.async_api import async_playwright
async def run(playwright): async def run(playwright):
browser = await playwright.chromium.launch(headless=False) browser = await playwright.chromium.launch(headless=False)

View file

@ -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 }) => { it('should print the correct imports and context options', async ({ runCLI }) => {
const cli = runCLI(['codegen', '--target=python', emptyHTML]); 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): def run(playwright):
browser = playwright.chromium.launch(headless=False) 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 }) => { it('should print the correct context options for custom settings', async ({ runCLI }) => {
const cli = runCLI(['--color-scheme=light', 'codegen', '--target=python', emptyHTML]); 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): def run(playwright):
browser = playwright.chromium.launch(headless=False) 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 }) => { it('should print the correct context options when using a device', async ({ runCLI }) => {
const cli = runCLI(['--device=Pixel 2', 'codegen', '--target=python', emptyHTML]); 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): def run(playwright):
browser = playwright.chromium.launch(headless=False) 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 }) => { 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 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): def run(playwright):
browser = playwright.chromium.launch(headless=False) 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]); const cli = runCLI(['codegen', '--target=python', '--output', tmpFile, emptyHTML]);
await cli.exited; await cli.exited;
const content = fs.readFileSync(tmpFile); 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): def run(playwright):
browser = playwright.chromium.launch(headless=False) 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'); const saveFileName = testInfo.outputPath('save.json');
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8'); await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, 'codegen', '--target=python', emptyHTML]); 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): def run(playwright):
browser = playwright.chromium.launch(headless=False) browser = playwright.chromium.launch(headless=False)