Skip to content

Commit

Permalink
chore: update snippets in python docs (#4976)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Jan 12, 2021
1 parent 7665a6e commit cb6e4a6
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion docs/src/api/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions docs/src/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions docs/src/ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions docs/src/core-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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']
Expand Down
4 changes: 2 additions & 2 deletions docs/src/emulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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']
Expand Down
33 changes: 19 additions & 14 deletions docs/src/intro-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/cli/codegen/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down
12 changes: 6 additions & 6 deletions test/cli/cli-codegen-python-async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions test/cli/cli-codegen-python.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cb6e4a6

Please sign in to comment.