Skip to content

Commit 55380b3

Browse files
authored
docs(readme): update structure (#9)
1 parent 16b4575 commit 55380b3

File tree

2 files changed

+80
-73
lines changed

2 files changed

+80
-73
lines changed

CONTRIBUTING.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Contributing
2+
3+
This project welcomes contributions and suggestions. Most contributions require you to agree to a
4+
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
5+
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
6+
7+
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
8+
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
9+
provided by the bot. You will only need to do this once across all repos using our CLA.
10+
11+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
12+
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
13+
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

README.md

+67-73
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,80 @@
1-
# Pytest Playwright Plugin
1+
# Pytest plugin for Playwright [![PyPI](https://img.shields.io/pypi/v/pytest-playwright)](https://pypi.org/project/pytest-playwright/)
22

3-
![CI](https://github.com/microsoft/playwright-pytest/workflows/CI/badge.svg)
4-
[![PyPI](https://img.shields.io/pypi/v/pytest-playwright)](https://pypi.org/project/pytest-playwright/)
5-
[![black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)
3+
Write end-to-end tests for your web apps with [Playwright](https://github.com/microsoft/playwright-python) and [pytest](https://docs.pytest.org/en/stable/).
64

7-
> A Pytest wrapper for [Playwright](https://github.com/microsoft/playwright-python) to automate web browsers (Chromium, Firefox, WebKit).
5+
- Support for **all modern browsers** including Chromium, WebKit and Firefox.
6+
- Support for **headless and headful** execution.
7+
- **Built-in fixtures** that provide browser primitives to test functions.
88

9-
## Features
10-
11-
- Have a separate new page and context for each test with Pytest fixtures
12-
- Run your end-to-end tests on multiple browsers by a CLI argument
13-
- Run them headful with the `--headful` argument to debug them easily
14-
- Using [base-url](https://github.com/pytest-dev/pytest-base-url) to only use the relative URL in your `Page.goto` calls
15-
16-
## Installation
9+
## Usage
1710

1811
```
1912
pip install pytest-playwright
2013
```
2114

22-
Basic example for more see the [examples sections](#examples) as a reference.
15+
Use the `page` fixture to write a basic test. See [more examples](#examples).
2316

2417
```py
2518
def test_example_is_working(page):
2619
page.goto("https://example.com")
27-
page.waitForSelector("text=Example Domain")
20+
assert page.innerText('h1') == 'Example Domain'
2821
page.click("text=More information")
2922
```
3023

31-
## Fixtures
32-
33-
### `browser_name` - session scope
34-
35-
A string that contains the current browser name.
24+
To run your tests, use pytest CLI.
3625

37-
### `browser` - session scope
26+
```sh
27+
# Run tests (Chromium and headless by default)
28+
pytest
3829

39-
A Playwright browser instance for the session.
30+
# Run tests in headful mode
31+
pytest --headful
4032

41-
### `context` - function scope
33+
# Run tests in a different browser (chromium, firefox, webkit)
34+
pytest --browser firefox
4235

43-
A separate Playwright context instance for each new test.
44-
45-
### `page` - function scope
46-
47-
A separate Playwright page instance for each new test.
48-
49-
### `browser_type_launch_args` - session scope
36+
# Run tests in multiple browsers
37+
pytest --browser chromium --browser webkit
38+
```
5039

51-
A fixture that you can define to overwrite the launch arguments for [`launch()`](https://playwright.dev/#path=docs%2Fapi.md&q=browsertypelaunchoptions). It should return a Dict.
40+
## Fixtures
41+
This plugin configures Playwright-specific [fixtures for pytest](https://docs.pytest.org/en/latest/fixture.html). To use these fixtures, use the fixture name as an argument to the test function.
5242

53-
### `browser_context_args` - session scope
43+
```py
44+
def test_my_app_is_working(fixture_name):
45+
# Test using fixture_name
46+
# ...
47+
```
5448

55-
A fixture that you can define to overwrite the context arguments for [`newContext()`](https://playwright.dev/#path=docs%2Fapi.md&q=browsernewcontextoptions). It should return a Dict.
49+
**Function scope**: These fixtures are created when requested in a test function and destroyed when the test ends.
5650

57-
### `is_chromium`, `is_firefox`, `is_webkit` - session scope
51+
- `context`: New [browser context](https://playwright.dev/#path=docs%2Fcore-concepts.md&q=browser-contexts) for a test.
52+
- `page`: New [browser page](https://playwright.dev/#path=docs%2Fcore-concepts.md&q=pages-and-frames) for a test.
5853

59-
A fixture which is a boolean if a specific execution is made by the specified browser.
54+
**Session scope**: These fixtures are created when requested in a test function and destroyed when all tests end.
6055

61-
## CLI arguments
56+
- `browser`: Browser instance launched by Playwright.
57+
- `browser_name`: Browser name as string.
58+
- `is_chromium`, `is_webkit`, `is_firefox`: Booleans for the respective browser types.
6259

63-
### `--browser`
60+
**Customizing fixture options**: For `browser` and `context` fixtures, use the the following fixtures to define custom launch options.
6461

65-
By default, the tests run on the Chromium browser. You can pass multiple times the `--browser` flag to run it on different browsers or a single time to run it only on a specific browser.
62+
- `browser_type_launch_args`: Override launch arguments for [`browserType.launch()`](https://playwright.dev/#path=docs%2Fapi.md&q=browsertypelaunchoptions). It should return a Dict.
63+
- `browser_context_args`: Override the options for [`browser.newContext()`](https://playwright.dev/#path=docs%2Fapi.md&q=browsernewcontextoptions). It should return a Dict.
6664

67-
Possible values: `chromium`, `firefox`, `webkit`
65+
## Examples
6866

69-
### `--headful`
67+
#### Configure Mypy typings for auto-completion
7068

71-
By default, the tests run in headless mode. You can pass the `--headful` CLI flag to run the browser in headful mode.
69+
```py
70+
from playwright.sync_api import Page
7271

73-
## Examples
72+
def test_visit_admin_dashboard(page: Page):
73+
page.goto("/admin")
74+
# ...
75+
```
7476

75-
### Skipping by browser type
77+
#### Skip test by browser
7678

7779
```py
7880
import pytest
@@ -83,7 +85,7 @@ def test_visit_example(page):
8385
# ...
8486
```
8587

86-
### Running only on a specific browser
88+
#### Run on a specific browser
8789

8890
```py
8991
import pytest
@@ -94,37 +96,40 @@ def test_visit_example(page):
9496
# ...
9597
```
9698

97-
### Handle base-url
99+
#### Configure base-url
98100

99-
Start Pytest with the `base-url` argument. Example: `pytest --base-url http://localhost:8080`
101+
Start Pytest with the `base-url` argument.
102+
103+
```sh
104+
pytest --base-url http://localhost:8080
105+
```
100106

101107
```py
102108
def test_visit_example(page):
103109
page.goto("/admin")
104110
# -> Will result in http://localhost:8080/admin
105111
```
106112

107-
### Using Mypy types for auto completion
113+
## Debugging
108114

109-
```py
110-
from playwright.sync_api import Page
115+
#### Use with pdb
116+
Use the `breakpoint()` statement in your test code to pause execution and get a [pdb](https://docs.python.org/3/library/pdb.html) REPL.
111117

112-
def test_visit_admin_dashboard(page: Page):
113-
page.goto("/admin")
118+
```py
119+
def test_bing_is_working(page):
120+
page.goto("https://bing.com")
121+
breakpoint()
114122
# ...
115123
```
116124

117-
## Debugging
118-
119-
To pause the Pytest test execution and interact with the browser (if its launched with `headless=False`) via the developer tools or call Playwright interactively, you can use the `breakpoint()` statement in your code to get a [pdb](https://docs.python.org/3/library/pdb.html) inline REPL.
125+
#### Screenshot on test failure
120126

121-
### Create a screenshot if a test fails
127+
You can capture screenshots for failed tests with a [pytest runtest hook](https://docs.pytest.org/en/6.1.0/reference.html?highlight=pytest_runtest_makereport#test-running-runtest-hooks). Add this to your `conftest.py` file.
122128

123-
On the CI it's useful to have screenshots if a test is failing, this can be implemented by adding the following in your `conftest.py` which will store the screenshots in the `.playwright-screenshots` directory which can be uploaded e.g. in your CI as an artifact.
124-
125-
This snippet requires `slugify` to convert test names to file paths, which can be installed with `pip install python-slugify`.
129+
Note that this snippet uses `slugify` to convert test names to file paths, which can be installed with `pip install python-slugify`.
126130

127131
```py
132+
# In conftest.py
128133
from slugify import slugify
129134
from pathlib import Path
130135

@@ -137,20 +142,9 @@ def pytest_runtest_makereport(item, call) -> None:
137142
page.screenshot(path=str(screenshot_dir / f"{slugify(item.nodeid)}.png"))
138143
```
139144

140-
## Special thanks
145+
## Deploy to CI
146+
Use the [Playwright GitHub Action](https://github.com/microsoft/playwright-github-action) or [guides for other CI providers](https://playwright.dev/#path=docs%2Fci.md&q=) to deploy your tests to CI/CD
141147

142-
[Max Schmitt](https://github.com/mxschmitt) for creating and maintaining the Pytest Playwright plugin.
143-
144-
## Contributing
145-
146-
This project welcomes contributions and suggestions. Most contributions require you to agree to a
147-
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
148-
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
149-
150-
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
151-
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
152-
provided by the bot. You will only need to do this once across all repos using our CLA.
148+
## Special thanks
153149

154-
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
155-
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
156-
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
150+
Thanks to [Max Schmitt](https://github.com/mxschmitt) for creating and maintaining this project.

0 commit comments

Comments
 (0)