test: add type-safe navigate
utility
#1032
Merged
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.
I'm adding a new Playwright fixture called
navigate()
. The purpose of this fixture is to provide type-safe navigation experience in tests by inferring the available page URLs fromreact-router
's codegen. This means less mechanical mistakes when testing your app.Test Plan
Verified by updating the existing E2E test suite and seeing it passing.
Checklist
Screenshots
Brainstorming
Why not override the default
page.goto()
?TL;DR
navigate()
is a higher-level abstraction overpage.goto()
, not its replacement.page.goto()
has a different call signature (i.e. customoptions
argument).How to ensure
navigate()
usage overpage.goto()
?You don't. As I mentioned above, the two are not equivalent and aren't meant to be used interchangeably. Our job is to show the best practices of type-safe navigation in tests, then it's the developer's job to adhere to them.
How to navigate to a dynamically generated URL?
Throughout the test suite, there are cases when the navigation target is created dynamically, e.g. extracted from the "Forgot password" email text. In those cases, the best course of action is to type cast the extracted URL to be of the most permissive type:
How to provide
page.goto()
options, likewaitUntil
?Ideally, don't.
navigate()
is an opinionated wrapper that provisions the same navigation behavior. Keep in mind that some options, likewaitUntil
, are being deprecated by Playwright themselves in favor of more deterministic state await mechanisms (i.e. don't wait until network is idle, add an assertion over what UI state to expect).We can expand the
navigate()
utility in the future if there's a need to pass other navigation options. For now, I believe it works sufficiently.