Playwright can reuse authenticated browser state with storageState, but role-based access tests usually need one signed-in user per role. In a real application, different roles often see different pages, buttons, and navigation options. An admin may be able to create or manage records, while an editor may only update content and a viewer may only read it. To test those boundaries properly, each role should authenticate with its own test account and save its own browser state.
That way, the test suite can verify more than just whether a user can log in. It can confirm that the right people can reach the right screens, that restricted actions are hidden or blocked, and that one role cannot accidentally access another role’s permissions. For example, a profile menu might show extra options for an admin, while a normal user only sees the shared items. Keeping one saved state per role also makes the tests easier to maintain, because each test runs with the identity it was meant to validate.
That is manageable in a small suite. It becomes test infrastructure when multiple roles, shared data, and parallel runs all depend on the same authentication setup.
Related guides
If you still need the authentication foundation, start with How to automate SSO login in Playwright with Okta, Auth0, or Microsoft Entra ID. For state expiry, workers, shards, and CI, see “How to keep authentication state fresh across long Playwright CI runs”.
Already know the Playwright pattern?
Jump to the Leapwork Play section to see how one parameterized login subflow can be reused for admin, normal-user, and future roles without creating a separate login implementation for each role. How does the same role-based workflow look in Leapwork Play?
Before you start
-
A Playwright Test project with reusable authentication already working.
-
At least two approved test identities, such as Admin and Viewer.
-
Credentials supplied through environment variables or an approved secret manager.
-
A written permission matrix that states what each role should be allowed and denied
Why QA engineers use Play for this workflow
-
Stop debugging the wrong user state. The role is explicit in the test, so you can immediately see whether the scenario is running as Admin, Viewer, or another user instead of tracing
storageState, projects, and fixtures. -
Change the role, not the authentication setup. Switch
ADMINtoVIEWERand rerun the same flow to create a negative permission test without rebuilding the surrounding login configuration. -
Fix login once. When the sign-in journey changes, update the reusable Login test case and let the role-based tests continue to reference it.
-
See the permission intent directly in the test. A QA engineer can read
Viewer → Login → Admin capability should be deniedwithout first understanding how the authentication framework is wired. -
Your framework develops tribal knowledge. After a couple of months, authentication may involve setup projects, fixtures, helpers, environment variables, project dependencies and CI configuration. Playwright provides these mechanisms because serious suites need them. The downside is that changing a business test can require understanding framework code written by the person who designed all of those layers.
If you continue with the traditional Playwright approach, none of this is impossible, but the QA engineer increasingly works across auth setup files, storageState artifacts, project configuration, fixtures, credentials, and the business test itself just to understand why a role-based test is behaving differently. The permission check may be simple; diagnosing the authentication context around it is often where the time goes.
This guide first shows the clean Playwright implementation, then uses the same Admin/Viewer scenario in Play so you can compare the authoring and debugging experience yourself.
Watch: reuse one parameterized Login workflow across roles
This walkthrough uses Play to test Play and shows the same reusable Login test case being used with different role inputs, such as Admin and Normal User. Each role-specific test continues with its own permission checks after authentication. It also includes a negative RBAC check where changing the role causes an Admin-only validation to fail as expected, confirming that the access boundary is enforced.
Playwright vs Leapwork Play for role-based access testing
|
Area |
Playwright |
Leapwork Play |
|
Role representation |
Separate test identities plus storageState/project configuration. |
Role ID or role data passed to a reusable parameterized Login subflow. |
|
Login reuse |
Reuse saved authenticated browser state. |
Reuse the login workflow; each test case signs in when the subflow is referenced. |
|
Add a new role |
Add identity/state configuration and role-specific permission tests. |
Add the role data/secret mapping and role-specific validation; the Login logic stays shared. |
|
Auth artifacts |
Protect one authenticated state file per role. |
No shared storageState file is carried across these role tests. |
|
Parallel execution |
Use separate accounts or isolated data when workers could conflict. |
Test cases execute independently; use different test users where account or data conflicts are possible. |
|
Authoring visibility |
Role setup may be spread across auth setup, config, fixtures, and specs. |
Reusable Login and role-specific business tests are visible in the shared workspace. |
|
Execution |
Team runs and maintains the Playwright browser/runtime environment. |
Built-in cloud browser and managed cloud execution. |
Best Practices
-
Define the permission matrix before writing tests, and keep it aligned with the product or security requirements.
-
Test both positive and negative permissions. A role should prove what it can do and what it cannot do.
-
Use a dedicated approved test identity per role; use more identities when parallel tests would otherwise conflict.
-
Keep role authentication state and credentials separate. Never store passwords or tokens in source control or plain-text data files.
-
When a role assignment changes, re-authenticate before validating the new permissions so the test uses current claims/session state.
-
Use multiple BrowserContexts only for workflows that genuinely require two users to participate in the same scenario.
Troubleshooting
|
Issue |
Likely cause |
What to check |
|---|---|---|
|
Viewer gets Admin permissions |
Wrong role state or role-to-project mapping |
Confirm the test is using the Viewer state/project and the expected test account. |
|
Viewer cannot see the button, but a protected route/action still works |
UI is hiding the control but authorization is not enforced at the boundary |
Test the protected URL or operation directly and raise it as an authorization defect if it succeeds. |
|
Permissions are wrong after an administrator changes the user role |
Existing session still reflects old claims/role data |
Re-authenticate the user before validating the changed role. |
|
Parallel role tests interfere with each other |
Tests share the same account or backend data |
Use separate test users or isolate the records each test modifies. |
|
Play test signs in with the wrong role |
Role ID, data row, or secret mapping is wrong |
Trace the role parameter passed into the shared Login and verify the mapped identity before the business steps run. |
Frequently asked questions
Can one storageState file be used for every role?
No. The saved state represents a specific signed-in identity. Keep separate authenticated state for the roles you need to validate.
Should I write a separate login implementation for every role?
Usually no. Keep one login helper and vary the identity/state. The role-specific tests should focus on permissions rather than repeating sign-in mechanics.
How do I test two signed-in users in one Playwright scenario?
Create two independent BrowserContexts and initialize each one with the appropriate role state. This gives both users isolated cookies and storage inside the same test.
What if an administrator changes a user’s role?
Re-authenticate before validating the new role. Existing session claims or application state may still represent the previous permissions.
Does Leapwork Play share one authenticated session across all role tests?
No. In the workflow shown here, each test case runs independently and performs the reusable Login flow when it is referenced. The login workflow is shared; the authenticated session is not.
Do I need a new Login test case for every new role in Play?
Not when the login is parameterized. Add the role data and secure credential mapping, then pass the new role into the same reusable Login test case.