Rayrun
← Back to Discord Forum

parallel execution with workers specific credentials

Ashish Vishnoiposted in #help-playwright
Open in Discord
Ashish Vishnoi
Ashish Vishnoi

I am looking for executing playwright scripts in parallel with each worker using a specific login details, and the same login credentials can be reused for any script run on that worker. Anyone know about such solution with typescript? <#1054804523652231198>

This thread is trying to answer question "How can playwright scripts be executed in parallel with each worker using specific login details, and the same login credentials be reused for any script run on that worker?"

1 reply
lucky_rabbit_44106

Yes it's fairly easy, Login in a loop in your global setup file and save auth state. --> Declare browser context and page at the top of test spec file and reuse auth state --> Create a custom function to dynamically get saved authentication state with the help of process.env.TEST_PARALLEL_INDEX variable

Answer

Running Playwright Scripts in Parallel with Unique Login Credentials

You can run Playwright scripts in parallel with each worker using unique login credentials and reuse them for subsequent tests. Here's how:

Set Up a Worker-Scoped Fixture

First, define a worker-scoped fixture in your test setup file. This fixture is responsible for authenticating and storing the login credentials for each worker.

import { test } from '@playwright/test';

test('worker-scoped fixture', async ({ workerStorageState }) => {
  // your code here
});

Generate a Unique Identifier

Use the parallelIndex provided by Playwright Test to generate a unique identifier for each worker. This identifier is used to create a separate storage file for each worker.

const parallelIndex = test.parallelIndex;

Check if Authentication State Exists

Before authenticating, check if an authentication state file already exists for the current worker. If it does, reuse the existing state by loading it into the browser or context.

Authenticate in a Clean Environment

To ensure authentication is performed in a clean environment, unset any existing storage state before authenticating. Create a new browser context or page with storageState set to undefined.

Acquire Unique Account Credentials

Obtain unique account credentials for each worker either by creating new accounts or using precreated accounts specifically designed for testing purposes.

Perform Authentication Steps

Replace the example authentication steps provided in the context with your own actions based on your application's login flow.

Store Authentication State

After successful authentication, store the current storage state into a file specific to that worker. This allows you to reuse this stored state across multiple tests executed by that particular worker.

Dispose of Resources

Once authentication is complete and storage state is saved, dispose of any resources created during this process.

Reuse Authentication State for Subsequent Tests

In each test that requires authentication, load the stored authentication state file from the worker-scoped fixture. This ensures that subsequent tests executed by the same worker can reuse the same login credentials.

By following these steps, you can run Playwright scripts in parallel with each worker using unique login credentials and reuse them for subsequent tests. This approach allows multiple team members to run tests simultaneously without interference and ensures a clean environment for each authentication process.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.