Skip to content

Commit 965df5b

Browse files
authored
docs: updated for the Pytest usage (microsoft#135)
1 parent 4b4962c commit 965df5b

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

README.md

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 🎭 Playwright for Python
1+
# 🎭 [Playwright](https://github.com/microsoft/playwright) for Python
22

33
[![PyPI version](https://badge.fury.io/py/playwright.svg)](https://pypi.python.org/pypi/playwright/) [![Join Slack](https://img.shields.io/badge/join-slack-infomational)](https://join.slack.com/t/playwright/shared_invite/enQtOTEyMTUxMzgxMjIwLThjMDUxZmIyNTRiMTJjNjIyMzdmZDA3MTQxZWUwZTFjZjQwNGYxZGM5MzRmNzZlMWI5ZWUyOTkzMjE5Njg1NDg) <!-- GEN:chromium-version-badge -->[![Chromium version](https://img.shields.io/badge/chromium-86.0.4217.0-blue.svg?logo=google-chrome)](https://www.chromium.org/Home)<!-- GEN:stop --> <!-- GEN:firefox-version-badge -->[![Firefox version](https://img.shields.io/badge/firefox-78.0b5-blue.svg?logo=mozilla-firefox)](https://www.mozilla.org/en-US/firefox/new/)<!-- GEN:stop --> [![WebKit version](https://img.shields.io/badge/webkit-14.0-blue.svg?logo=safari)](https://webkit.org/)
44
[![Coverage Status](https://coveralls.io/repos/github/microsoft/playwright-python/badge.svg?branch=master)](https://coveralls.io/github/microsoft/playwright-python?branch=master)
@@ -15,12 +15,11 @@ Playwright is a Python library to automate [Chromium](https://www.chromium.org/H
1515

1616
Headless execution is supported for all the browsers on all platforms.
1717

18-
This is a Python 3 version of the [https://github.com/microsoft/playwright](https://github.com/microsoft/playwright) project.
19-
20-
## Usage
18+
## Installation
2119

2220
```
2321
pip install playwright
22+
python -m playwright install
2423
```
2524

2625
This installs Playwright and browser binaries for Chromium, Firefox and WebKit. Once installed, you can `import` Playwright in a Python script and automate web browser interactions.
@@ -41,6 +40,57 @@ Playwright is built to automate the broad and growing set of web browser capabil
4140
* Native input events for mouse and keyboard
4241
* Upload and download files
4342

43+
## Usage
44+
45+
### Pytest
46+
47+
For writing end-to-end tests we recommend to use the official [Pytest plugin](https://github.com/microsoft/playwright-pytest#readme) for Playwright. It contains utilities for running it on multiple browsers, having a new page instance on every test or base-url support via a command-line argument. This will in the end look like that:
48+
49+
```py
50+
def test_playwright_is_visible_on_google(page):
51+
page.goto("https://www.google.com")
52+
page.type("input[name=q]", "Playwright GitHub")
53+
page.click("input[type=submit]")
54+
page.waitForSelector("text=microsoft/Playwright")
55+
```
56+
57+
For more information checkout the project on [GitHub](https://github.com/microsoft/playwright-pytest#readme).
58+
59+
### Standalone
60+
61+
For using Playwright standalone, you can either use the sync version or the async variant (async/await). In most cases the sync variant is the right choice to automate the web browsers e.g. for writing end-to-end tests. Both will get initialized with a context manager.
62+
63+
#### Sync variant
64+
65+
```py
66+
from playwright import sync_playwright
67+
68+
with sync_playwright() as p:
69+
for browser_type in [p.chromium, p.firefox, p.webkit]:
70+
browser = browser_type.launch()
71+
page = browser.newPage()
72+
page.goto('http://whatsmyuseragent.org/')
73+
page.screenshot(path=f'example-{browser_type.name}.png')
74+
browser.close()
75+
```
76+
77+
#### Async variant
78+
79+
```py
80+
import asyncio
81+
from playwright import async_playwright
82+
83+
async def main():
84+
async with async_playwright() as p:
85+
browser = await p.webkit.launch()
86+
page = await browser.newPage()
87+
await page.goto('http://whatsmyuseragent.org/')
88+
await page.screenshot(path=f'example-{browser_type.name}.png')
89+
await browser.close()
90+
91+
asyncio.get_event_loop().run_until_complete(main())
92+
```
93+
4494
## Examples
4595

4696
#### Page screenshot
@@ -83,7 +133,7 @@ with sync_playwright() as p:
83133
browser.close()
84134
```
85135

86-
... or, if you are comfortable using asyncio, you can do the following:
136+
The asyncio variant:
87137

88138
```py
89139
import asyncio
@@ -131,7 +181,7 @@ with sync_playwright() as p:
131181
browser.close()
132182
```
133183

134-
... and again, async version:
184+
The asyncio variant:
135185

136186
```py
137187
import asyncio
@@ -177,7 +227,7 @@ with sync_playwright() as p:
177227
browser.close()
178228
```
179229

180-
... async version:
230+
The asyncio variant:
181231

182232
```py
183233
import asyncio
@@ -201,9 +251,9 @@ async def main():
201251
asyncio.get_event_loop().run_until_complete(main())
202252
```
203253

204-
# Is Playwright for Python ready?
254+
## Is Playwright for Python ready?
205255

206-
We are ready for your feedback, but we are still covering Playwright Python with the tests, so expect a bumpy ride and don't use for production.
256+
We are ready for your feedback, but we are still covering Playwright Python with the tests, so expect some API changes and don't use for production.
207257

208258
## Resources
209259

0 commit comments

Comments
 (0)