I am working with an array of data received from an API, which contains 5000 items. I need to perform some checks on each item in the user interface (UI). I want these checks to be executed simultaneously by 3 users.
My goal is to split the array into 3 parts, so that each user handles approximately 1666 items, and have these checks run in parallel.
How can I achieve this using Playwright? Are there any best practices or recommended approaches for implementing this kind of parallel execution with multiple users in Playwright?
Any help or guidance would be greatly appreciated!
I have started by creating a Playwright script that performs the necessary checks on a single item in the array. This works fine for individual checks. However, I am struggling with scaling this up to handle the entire array with multiple users.
I tried using Promise.all to run multiple instances of the check function in parallel, but this approach doesn't neatly split the work among the 3 users. Additionally, it doesn't simulate the user interactions as if they were done by different users concurrently.
I was expecting to find a way to effectively distribute the 5000 items among 3 users and have each user perform their set of checks independently and in parallel. Ideally, there would be a way to manage and monitor these parallel tasks to ensure that all checks are completed efficiently.
Any insights on how to structure this in Playwright or any examples of similar implementations would be extremely helpful!
Playwright handles this automaticly for you, just define 3 workers and set fullyParallell = true:
playwright.config.ts:
import { defineConfig, devices } from "@playwright/test";
export default defineConfig({
testDir: "./tests",
fullyParallel: true, // 👈
workers: 3, // 👈
reporter: "dot",
projects: [
{
name: `Chromium`,
use: { ...devices["Desktop Chrome"] },
},
],
});
test.spec.ts:
import { test } from "@playwright/test";
const items = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
for (const item of items) {
test(`Testing item ${item}`, async () => {
// Waiting 1 second for test to run:
await new Promise((r) => setTimeout(r, 1000));
console.log(item, new Date().toLocaleTimeString());
});
}
Returns:
Running 9 tests using 3 workers
B 10:37:11 AM
C 10:37:11 AM
A 10:37:11 AM
E 10:37:12 AM
D 10:37:12 AM
F 10:37:12 AM
G 10:37:13 AM
I 10:37:13 AM
H 10:37:13 AM
As you can see, tests are performed in parallell, 3 by 3.