-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPIKE] Testing with Playwright #3064
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a84b8ad
to
52286c1
Compare
19 tasks
3288bc6
to
2dc8d40
Compare
Following basic installation via `npm init playwright@latest`
Basic port just moving the tests across without rewriting as API is compatible with Jest Puppeteer. Removed the difference of timeouts as both headed and headless tests ran OK Updated the test using Jest spy for a simple boolean check. We could always use [sinon.js](https://sinonjs.org/releases/v15/spies/) should we need actual spies.
Includes setting up axe to check that there is no violation, but leaves the rest of the test based on Nunjucks renders and Cheerio, for future comparison
All tests are ported except those requiring snapshot matching. Those use `htmlWithClassName` to isolate the HTML in the snapshot to match only specific parts. Best to port that separately if we decide to.
c289194
to
36bc04f
Compare
cd82ff0
to
7d2b739
Compare
7d2b739
to
c851b51
Compare
Closing the spike PR. Future work will be tracked by this epic: #3278 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Explores the use of Playwright as a potential replacement for Puppeteer to run our in-browser tests.
This should offer us a wider range of browsers to test in, increasing our confidence that things run OK. It should offer a better debugging experience locally (viewing browser and pausing when querying its content), as well as remotely
Changes
The PR adds Playwrigh to the project and ports two tests to it:
The PR also sets up Github workflows to compare the tests running:
a. using Jest
b. using Playwright, in a single run targeting all 3 available browsers
c. using Playwright, in parallel runs for each of the 3 available browsers
The workflows are set up to run either each file individually or both files in a single run. This is in the hope to highlight the cost of moving from Jest+Cheerio template tests to Playwright ones.
Findings
Running tests
Puppeteer is only about controlling the browser, we bind it to jest using jest-puppeteer. Playwright provides both the API for controlling the browser and the test runner. It can be bound to jest, but that's not the route recommended by the project.
The test runner API offers similar concept as Jest, but in a slightly different API:
test
instead ofit
, and thedescribe
,beforeAll
,beforeEach
methods are accessed on thattest
object rather than as argument. It also uses a slightly different API for configuring the use of JavaScriptEach test gets its own isolated "BrowserContext", which includes a fresh page, so that ensures tests don't leak from one to another as far as I understand. Gotcha is that the
page
is not accessible intest.beforeAll
, though, as it is created for each test, so to be reserved for non in-browser initialisation. If some initialisation need to happen on the page, it'll need to be intest.beforeEach
(or manually creating a shared page, which is also a possibility, but less ideal).There's no global
page
variable. You get reference to the page through the arguments of the test function. This makes things quite neat and make sure test logic runs on the relevant page.Runner configuration
I've mostly stuck to the pre-configured default form the initialisation. They did the job to get looking at how things would work. They also configure some decent defaults, especially retrying and capturing trace for debugging on CI (more later).
The runner can be configured to automatically start a server, after checking if one is already running. It gives flexibility to
npm start
or not before running the tests. When configured to run a server, the API to control the browser will use it as base for resolving URLs, allowing us to not have to juggle with the server's port, which is nice.Working locally
The runner doesn't have a watch mode, which is a major pain point for a test runner 😭 Nothing that nodemon cannot fix though
PWDEBUG=0 PW_REPORTER_HTML_OPEN=never npx nodemon --watch "src/**/*.spec.{cjs,js,mjs}" --exec "npx playwright test --project chromium"
. We'll probably want that as an npm script though.Unless told otherwise, the runner will automatically open an HTML report of the result if they fail when ran locally. I added some configuration to get some flexibility on that, as it can be a bit annoying and open lots of browser windows. The HTML reports are saved anyway and can be open with
npx playwright show-report
.The test runner offers a debug mode (
PWDEBUG=1
or--debug
) which will pause at each interaction with the browser (locating an element or actual interaction), allowing to go step by step locally.The test runner can capture traces of the interactions with the browser to replay them afterwards. Not necessarily useful locally thanks to the debug mode, outside of sharing with another dev maybe. It's more something useful on CI to investigate test failures.
Writing tests
The API for controlling browser is very very close to Puppeteer's. To the point that porting the tests could get started by [moving them across and replacing the test runner API only, mostly][port-tests].
The API is able to control 3 different browser engines: Chromium, Firefox and Webkit. Note the mention of Chromium and Webkit, not Chrome (though if Chrome/Edge is installed it can run against it) and Safari. It still gives us a wider target than Puppeteer.
Past that, Playwrights offers some neat additions that make the tests clearer:
toBeVisible()
,toHaveAttribute(attributeName, value)
,toHaveCSS(propertyName, value)
,toHaveId()
,toHaveText()
and of coursetoHaveClass()
(though that one is a little unfortunately implemented and does string comparison against the whole value rather than checking individual classes, though RegExp can alleviate that. Probably worth declaring our owntoContainClass
that abstracts that up).Playwright supports snapshot matching. This would allow to check for regressions in the HTML of the component. I didn't explore this further, leaving our snapshot tests still to rely on Cheerio. One change is that snapshots are stored as individual files rather than a single file as of now. While Playwright points to Jest's
expect
API for its documentation, I couldn't figure a way to get them stored as a single file.[not explored] Playwright also offers a way to compare screenshots.
On CI
Test reports report can be uploaded as artifact of the Github action. Once downloaded and unziped, they can be opened to access the report with
npx playwright show-report
like a local report. They'll contain the traces for failing tests which can be then investigated in the browser.Playwright also offers a Progressive Web App for loading the traces, that may smoothen the workflow if we upload only the traces as Github action artifacts.
Perf wise, the PR runs a good few workflow so we can compare running times inside Github actions (see the Changes for description). Clicking "Checks" than "Tests" will give a more palatable view than the list of checks at the bottom of this PR due to the long names.
First notable thing is that Playwright will download the 3 browsers and it takes a little bit. Something to keep in mind when looking at the different checks, as 40-ish seconds of the Playwright runs are down to installing browsers. Hopefully a bit of caching can speed that up.
Even when running a single browser and using 2 workers (matching Github actions), Playwright does run slower than our tests with Puppeteer (comparing "Puppeteer Comparison" times with "Playwright - Matrix , Browser Test (chromium)"), whether it runs a both files or a single one. Goes from ~25s to ~35s with the two files, but hard to say how that will scale up with the number of files (is it the "launch" that's creating the increase and would be a one-off-ish thing, or each test running slower?).
Running multiple browsers unsurprisingly takes longer, especially as Playwright runs only a single worker (50% of available cores, I think), so we'd likely want to create a matrix to parallelise each browser run.
The hardest perf impact is moving template tests from Cheerio to Playwright. Spinning up a browser moves from 6s to 20ish seconds. Again no idea how that would scale, so the gain from a more fluent API for writing the tests will have to be weighted against the performance.
[playwright-port-tests]: alphagov/govuk-frontend@
c3ab8d2
(#3064)Steps trail
Adds 3 extra jobs to compare timings:
- Playwright Bulk to run all Playwright tests in one call
- Playwright Matrix that splits them per browser
- Puppeteer comparison which runs the CharacterCount tests in isolation for comparison
Left out for now:
htmlWithClassName
, that isolates the markup related to a component specifically using the element's classnames.