Skip to content

Commit ab5935f

Browse files
Finalise documentation
1 parent df0f656 commit ab5935f

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"codegen",
1010
"pytest",
1111
"utilise",
12+
"utilised",
1213
"Utilising"
1314
]
1415
}

docs/getting-started/1_Understanding_Playwright_Python.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This guide outlines how Playwright works in Python, and how to start writing tes
1111
- [Executing Tests](#executing-tests)
1212
- [Using pytest Logic](#using-pytest-logic)
1313
- [Utilising Playwright `codegen`](#utilising-playwright-codegen)
14+
- [Utilising Playwright `show-trace`](#utilising-playwright-show-trace)
15+
- [Further Reading](#further-reading)
1416
- [Appendix](#appendix)
1517
- [Info: What Is Chromium](#info-what-is-chromium)
1618

@@ -29,7 +31,7 @@ where by default it looks for any files in the format of `test_*.py` or `*_test.
2931
functions in the file starting with `test_` and if found, will execute that function as a test and collect the result.
3032

3133
pytest will spin up any utilities it needs to execute tests (including any we choose to define), which for this framework includes a number of
32-
Playwright-based objects we would likely want to utilise, including:
34+
Playwright-specific objects we would likely want to utilise, including:
3335

3436
- `page`: The Playwright page object, which we use to interact with a browser page during tests. You'll likely use this object for every test.
3537
- `browser`: The Playwright browser object, which we use to create and manage the browser directly and create new pages if required. It's unlikely you'll need to include this unless you have a very specific browser test.
@@ -67,10 +69,35 @@ arguments for the test like so:
6769
page.goto("https://github.com/nhs-england-tools/playwright-python-blueprint")
6870

6971
As you can see from the example, the only setup for the `page` object here is in the function arguments, and we can use it as needed in the test.
72+
We can also use this logic to create utilities and resources that can be utilised by our tests, and easily pass them in to be used as needed.
73+
74+
The `page` object is an example of a fixture, which are actions that can be run before or after any tests we want to execute (these are known as
75+
hooks in other test automation frameworks). A fixture is normally defined at the start of a set of tests, in a format like so:
76+
77+
# Doing an import like this for the page object isn't required, but is considered good practice
78+
from playwright.sync_api import Page
79+
80+
# An example fixture, which runs before the start of every test in this module
81+
@pytest.fixture(autouse=True)
82+
def go_to_page(page: Page) -> None:
83+
page.goto("https://github.com/nhs-england-tools/playwright-python-blueprint")
84+
85+
# An example test which continues on from the fixture above
86+
def test_example(page: Page) -> None:
87+
page.get_by_placeholder("Go to file").fill("test_example.py")
88+
page.get_by_label("tests/test_example.").click()
89+
expect(page.locator("#file-name-id-wide")).to_contain_text("test_example.py")
90+
91+
This allows for easy reuse of steps and reducing overall test maintenance going forward. Fixtures can play a powerful part in how you design
92+
your tests, including creating utilities available for global use, or just adding repeatable actions in a way that can overall reduce the
93+
maintenance effort of your tests going forward.
94+
95+
Further reading on fixtures can be found in the [Playwright documentation](https://playwright.dev/python/docs/test-runners#fixtures).
7096

7197
## Utilising Playwright `codegen`
7298

73-
If you're new to Playwright, Python or automating tests generally, then Playwright provides a code generation tool that allows you to manually navigate
99+
If you're new to Playwright, Python or automating tests generally, then
100+
[Playwright provides a code generation tool](https://playwright.dev/python/docs/codegen#recording-a-test) that allows you to manually navigate
74101
through a browser to generate the code for a test. You can access the `codegen` tool by using the following command:
75102

76103
# Load a empty browser window
@@ -104,6 +131,44 @@ Whilst the `codegen` tool will provide you with the basic code to get started, i
104131
code that has been provided and refine as needed. Having the ability to generate the code in this fashion allows you to create tests quickly and build up
105132
understanding of how to construct tests using Playwright Python, but you will soon discover that they may not be the most efficient in their raw state!
106133

134+
## Utilising Playwright `show-trace`
135+
136+
If you encounter issues when trying to execute your newly created tests,
137+
[Playwright also provides a trace functionality](https://playwright.dev/python/docs/trace-viewer-intro) that records each action a test
138+
undertakes and outlines where any issues or errors have occurred. This can be useful for a number of reasons, including:
139+
140+
- Easily pinpointing problems within a test, including functional and performance concerns
141+
- Generating evidence to show stakeholders what a test actually does during execution
142+
- The ZIP file generated can be opened on any machine, or via a [browser utility provided by Playwright](https://trace.playwright.dev/)
143+
144+
To open a trace file, use the following command (replacing `<path-to-file>` with the actual path to the trace.zip file generated):
145+
146+
# Opens a trace file
147+
playwright show-trace <path-to-file>
148+
149+
A trace file when opened looks like this:
150+
151+
<!-- vale off -->
152+
![An image of the Playwright trace file](./img/3-show_trace.png "Playwright trace file view")
153+
<!-- vale on -->
154+
155+
The primary information provided within the trace is:
156+
157+
- A timeline of events with screenshots (at the top of the report)
158+
- A summary of each action undertaken (on the left side of the report)
159+
- A screenshot of the selected action (on the right side of the report)
160+
- The network and test activity (at the bottom of the report)
161+
162+
As you can see, it provides a lot of information to work with to help demonstrate what a test is doing, and diagnosing
163+
any issues when something goes wrong.
164+
165+
## Further Reading
166+
167+
Here are some useful links for further reading beyond these guides:
168+
169+
- [Playwright Python documentation](https://playwright.dev/python/docs/intro)
170+
- [pytest documentation](https://docs.pytest.org/en/stable/index.html)
171+
107172
## Appendix
108173

109174
### Info: What Is Chromium

docs/getting-started/2_Blueprint_File_Breakdown.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ This file outlines the packages required from the Python Package Index (PyPI) to
2727
This file outlines the configuration of pytest, and ultimately how Playwright also executes. A couple of things to note:
2828

2929
- The `log_cli` section covers default logging provided by pytest - we have defaulted this to on at INFO level, but this can be amended as needed.
30-
- The `addopts` section will run any commands you want to run by default for each execution and can be overwritten using the appropriate options via the command line. For example, you can override the `--tracing` level to on by executing pytest using: `pytest --tracing=on`.
30+
- The `addopts` section will run any commands you want to run by default for each execution and can be overwritten using the appropriate options via the command line. For example, you can override the `--tracing` level to on by executing pytest using: `pytest --tracing=on`. The options we have turned on by default are:
31+
- Do not run the tests marked utils by default (these are the unit tests for this project and do not use Playwright)
32+
- Generate a HTML report in a single file, and output it in the `test-results` directory with the name `report.html`
33+
- Generate a JSON report, omitting some collection data and then output it in the `test-results` directory with the name `results.json`
34+
- Only generate Playwright stack-trace files when a test fails
3135
- The `markers` section is for organizing any marks (or tags) you want to apply to your tests, for example by a business area or a testing type. If you don't include your marks in this list, pytest will give you a warning until they have either been added here or programmatically within the code.
3236

3337
Any configuration you want to apply to all of your test executions should be placed in this file where possible, to ensure easy maintenance.
124 KB
Loading

0 commit comments

Comments
 (0)