chore: update snippets in python docs (#4976)
This commit is contained in:
Родитель
7665a6ec7f
Коммит
cb6e4a6657
|
@ -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()
|
||||
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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']
|
||||
|
|
|
@ -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']
|
||||
|
|
|
@ -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:
|
||||
async def main():
|
||||
async with async_playwright() as p:
|
||||
browser = await p.chromium.launch()
|
||||
page = await browser.new_page()
|
||||
# interact with UI elements, assert values
|
||||
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()
|
||||
|
|
|
@ -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)})
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Загрузка…
Ссылка в новой задаче