E2E testing with Japa and Playwright #3902
HipsterBrown
started this conversation in
Cookbooks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
End-to-end (E2E) testing provides the most realistic and broadest coverage of full stack AdonisJS applications. Playwright is handy open-source project for cross-browser automation that can also be used to test web applications.
This guide will show how to integrate Playwright into the built-in Japa testing framework to make E2E testing as seamless as possible.
Some customization may be required for each application use case, i.e. auth.
TL;DR View a RealWorld example app with end-to-end testing coverage
Japa Integration
The core piece of this integration is a custom Japa plugin (unpublished at the time of writing):
You may notice the use of
playwright-testing-library
for thegetScreen
helper to provide more accessible selection of elements in the browser. Read more about this testing philosophy here: https://testing-library.com/docs/guiding-principlesThe plugin can be imported and used in the
tests/bootstrap.ts
file:Also, make sure the HTTP server is started for that suite in the
configureSuite
hook in that bootstrap file (this assumes the suite is callede2e
:My
.adonisrc.json
is configured for the e2e suite of tests, with a longer timeout than the functional tests due to potentially increased latency when interacting with a browser environment:Writing tests
With all that in place, let's take a look at an example test case:
Things like factories and database interactions can still be used in these tests, and the text context now includes the
page
object andgetScreen
helper. Once we have ascreen
instance fromgetScreen
, we get access to all the Testing Library queries for asserting on the markup of the page.If we want to test an authenticated user experience, we can use the
login
helper from the test context as demonstrated below:These tests can be run like any other suite with
node ace test
. If you want to view the live browser (rather than running headless), set theHEADLESS
environment variable to0
:HEADLESS=0 node ace test
Running Tests on CI
For continuous feedback in pull requests or before deployment, the tests can be run in a continuous integration environment, like GitHub Actions:
Read the Playwright docs for more CI configuration examples; be sure to trade out references to
npx playwright test
withnode ace test
instead.Conclusion
This recipe provides the most basic browser testing features from Playwright. The plugin could eventually become a separate package with official auth support (rather than manually logging in through the UI) and be configured to provide automatic screenshots after failures or for visual regression testing. Iterating on that, there could be video capture for full user flows and automatically generating test interactions.
Until that time, I hope you've found this information helpful!
Beta Was this translation helpful? Give feedback.
All reactions