Playwright

Handling Authentication for Multiple User Logins in Playwright

Handling Authentication for Multiple User Logins in Playwright  

Testing applications that support multiple user roles or account types often requires handling multiple user logins in a single test suite. Playwright is robust authentication tools simplify this by enabling persistent authentication, session management, and seamless switching between different user accounts. 

In this blog, we’ll explore how to handle multiple user logins effectively in Playwright, ensuring your tests remain scalable, maintainable, and efficient.

Why Test Multiple User Logins?

Applications often have varying access levels or features based on user roles, such as: 

  • Admin vs. Regular User: Admins access additional controls or settings.
  • Paid vs. Free Users: Paid accounts unlock premium features.
  • Guest vs. Authenticated Users: Registered users can perform tasks unavailable to guests.

Testing these scenarios ensures role-specific functionality works as intended and maintains security boundaries.

Approach 1: Saving Storage States for Playwright Multiple Users

Playwright allows you to save storage states for each user after login. These saved states can then be reused in tests. By capturing the authenticated state of a user session (cookies, localStorage, sessionStorage), you can reuse this state across multiple tests without repeating the login process.

Steps to Handle Multiple Users

1. Save Storage States for Each Playwright User

Create a test that logs in with the required credentials for a specific user role and saves the storage state to a file.

Save the storage states to distinct files named according to the user role (e.g., admin.json, regular.json). Store them in a dedicated folder, such as playwright/.auth/, to keep your project organized.

This approach can also be implemented under global setup file which would run before the test suite and the login step will not be counted a separate test case.  

2. Reuse Storage States in Tests

Load the appropriate storage state for each test. Set up your test to load the storage state file corresponding to the desired user role using the test.use() method. This tells Playwright to initialize the browser context with the specified state. 

Benefits

  • Tests run faster as logins are avoided for every test. 
  • Separate storage states ensure no interference between user sessions. 
  • Supports parallel test execution. 

Approach 2: Using Different Contexts for Each Playwright User

When testing applications with multiple user roles, such as Admins, Regular Users, or Guests, using separate contexts in Playwright ensures isolated sessions for each user. Each context represents an independent browser session, complete with its own cookies, localStorage, and sessionStorage. This approach eliminates the risk of interference between different user sessions and allows simultaneous interactions with the application in the same test. 

Steps to Use Contexts for Multiple Users

1. Create Contexts for Each Playwright User

Set up multiple contexts and log in as different users in each and then store the session to the dedicated folder.

2. Run Tests with Separate Contexts

If you’ve saved storage states for each role, you can load these states into new contexts to skip the login process.

Approach 3 : Implementation of authentication in POM fixtures.

We can add the storing of session per browser context in the below manner

Below is an example that creates fixtures for two Page Object Models - admin POM and user POM. It assumes adminStorageState.json and userStorageState.json files were created in the global setup.

Using the page fixtures in your tests

Using this approach makes your tests more clean and you can manage as many user roles sessions in fixture files.

Best Practices for Multiple User Logins in Playwright

  1. Use Role-Specific Test Descriptions: Clearly define tests based on user roles (e.g., Admin, Guest, Subscriber). 
  2. Avoid Shared States: Keep user sessions isolated using contexts or separate storage states. 
  3. Secure Test Credentials: Use environment variables to manage user credentials securely. 
  4. Minimize Redundancy: Automate login steps once and reuse storage states. 
  5. Parallelize Tests: Utilize Playwright’s parallel execution to run tests for different users simultaneously. 

Conclusion

Handling authentication for multiple user logins in Playwright ensures that your application’s role-based functionality is thoroughly tested and secure. Whether you use persistent storage states or isolated browser contexts, by adopting these practices, you can enhance your test efficiency, reliability, and coverage, ensuring a seamless experience for all user roles.

prithviraj-ingale

Senior SDET