You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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/).
6
4
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.
8
8
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
17
10
18
11
```
19
12
pip install pytest-playwright
20
13
```
21
14
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).
23
16
24
17
```py
25
18
deftest_example_is_working(page):
26
19
page.goto("https://example.com")
27
-
page.waitForSelector("text=Example Domain")
20
+
assertpage.innerText('h1') =='Example Domain'
28
21
page.click("text=More information")
29
22
```
30
23
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.
36
25
37
-
### `browser` - session scope
26
+
```sh
27
+
# Run tests (Chromium and headless by default)
28
+
pytest
38
29
39
-
A Playwright browser instance for the session.
30
+
# Run tests in headful mode
31
+
pytest --headful
40
32
41
-
### `context` - function scope
33
+
# Run tests in a different browser (chromium, firefox, webkit)
34
+
pytest --browser firefox
42
35
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
+
```
50
39
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.
52
42
53
-
### `browser_context_args` - session scope
43
+
```py
44
+
deftest_my_app_is_working(fixture_name):
45
+
# Test using fixture_name
46
+
# ...
47
+
```
54
48
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.
-`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.
58
53
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.
60
55
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.
62
59
63
-
### `--browser`
60
+
**Customizing fixture options**: For `browser` and `context` fixtures, use the the following fixtures to define custom launch options.
64
61
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.
66
64
67
-
Possible values: `chromium`, `firefox`, `webkit`
65
+
## Examples
68
66
69
-
###`--headful`
67
+
#### Configure Mypy typings for auto-completion
70
68
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
72
71
73
-
## Examples
72
+
deftest_visit_admin_dashboard(page: Page):
73
+
page.goto("/admin")
74
+
# ...
75
+
```
74
76
75
-
###Skipping by browser type
77
+
#### Skip test by browser
76
78
77
79
```py
78
80
import pytest
@@ -83,7 +85,7 @@ def test_visit_example(page):
83
85
# ...
84
86
```
85
87
86
-
###Running only on a specific browser
88
+
#### Run on a specific browser
87
89
88
90
```py
89
91
import pytest
@@ -94,37 +96,40 @@ def test_visit_example(page):
94
96
# ...
95
97
```
96
98
97
-
###Handle base-url
99
+
#### Configure base-url
98
100
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
+
```
100
106
101
107
```py
102
108
deftest_visit_example(page):
103
109
page.goto("/admin")
104
110
# -> Will result in http://localhost:8080/admin
105
111
```
106
112
107
-
### Using Mypy types for auto completion
113
+
##Debugging
108
114
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.
111
117
112
-
deftest_visit_admin_dashboard(page: Page):
113
-
page.goto("/admin")
118
+
```py
119
+
deftest_bing_is_working(page):
120
+
page.goto("https://bing.com")
121
+
breakpoint()
114
122
# ...
115
123
```
116
124
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
120
126
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.
122
128
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`.
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
141
147
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
153
149
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
0 commit comments