-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
E2E Test Utils: Add getCurrentUser(), and use it for user switching #33050
Conversation
Size Change: -1.08 kB (0%) Total Size: 1.05 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic makes sense here, the function to retrieve the current user is a bit unexpected but I guess it works :)
Yeah, agree that it's a bit weird. I was looking for some way that WP exposed the currently logged-in user already (on the client side), and this was the best I could find. I was considering some alternatives (see PR desc) but ultimately thought that the cookie-based approach was more performant, and less dependent on a separate test plugin (which would've had other drawbacks). |
e2e failures are unrelated and being investigated. |
Nice one, thank you for working on it 👍🏻 |
Description
Spun off from #32868.
A lot of our E2E test utils use
switchUserToAdmin()
andswitchUserToUser()
. Typically,switchUserToAdmin()
is used at the beginning to be able to change a site-wide setting in wp-admin; andswitchUserToUser()
is used at the end to switch back to 'normal' test privileges.The credentials used for
switchUserToAdmin()
are statically stored here:gutenberg/packages/e2e-test-utils/src/shared/config.js
Lines 1 to 4 in 37d6024
The credentials for
switchUserToUser()
are informed by theWP_USERNAME
andWP_PASSWORD
env vars, which in turn can be set through the--wordpress-username
and--wordpress-password
args for the e2e test command, as documented here.If those args/env vars aren't set, we fall back to the admin credentials:
gutenberg/packages/e2e-test-utils/src/shared/config.js
Lines 6 to 10 in 37d6024
Due to the implementation of
switchUserToAdmin()
andswitchUserToUser()
, this means that in the default case (with no custom username/password combination set), both functions are pretty much no-ops, as they both bail early in that case:gutenberg/packages/e2e-test-utils/src/switch-user-to-admin.js
Lines 11 to 16 in 37d6024
gutenberg/packages/e2e-test-utils/src/switch-user-to-test.js
Lines 11 to 16 in 37d6024
This means that those functions cannot scale to a scenario where we want to use a different user programmatically, i.e. as part of an e2e test, rather than statically setting a different user for all tests. One example for such a scenario would be #32868 -- or really any e2e test where we'd like to use a user with lesser-than-admin privileges.
For this reason, I'd like to change the checks in
switchUserToAdmin()
(and, for symmetry reasons, inswitchUserToUser()
), to compare the currently logged-in user to the admin or custom user, respectively.(This means that for a scenario like #32868, we still won't be able to use
switchUserToUser()
, as that just falls back to the custom user set via e2e tests args or env vars; but it's easy enough to useloginUser()
directly).Note that
switchUserToAdmin()
andswitchUserToUser()
are present in a lot of our e2e test utils, so performance is crucial: we want to continue to bail early if the admin is already logged in, and we want to do it quickly.Alternatives considered
Evaluate
.display-name
(in the masterbar, top right)Downside: Returns the display name, not the username
Add a test plugin that injects the username as a global JS var
Downside: Requires a plugin to be active, which can present a bit of a catch-22: e2e test setup needs to log in as admin (via
switchUserToAdmin()
) -- butswitchUserToAdmin()
determines whether to log in based on the global var provided by the plugin.How has this been tested?
Run the e2e tests locally (
npm run test-e2e
), or observe them in CI on this PR.