How do I pass data between workers in Playwright?


To pass data between workers in @playwright/test, you can use the evaluate() and evaluateHandle() methods. These methods let you execute a function in a worker's context and return its result. If the function returns a serializable value, like an object or an array, you can easily share it between workers.

Here's how you can retrieve data from one worker and pass it to another:

  1. Use page.evaluate() to get data from the first worker:
const data = await page.evaluate(() => {
  // Retrieve data from the worker
  return { key: 'value' };
  1. Pass the data as an argument to another worker's evaluate() method:
await page2.evaluate((data) => {
  // Use the data in the second worker
}, data);

Remember to ensure that each worker has access only to the data it needs. You can achieve this by using separate storage locations for each worker or by partitioning your data into smaller chunks.

If your application requires synchronization between multiple workers, consider implementing locking mechanisms like semaphores or mutexes. These mechanisms make sure that only one worker at a time has access to shared resources, preventing race conditions.

In summary, passing data between workers in Playwright involves using the evaluate() and evaluateHandle() methods and carefully considering your application's requirements and architecture. By applying appropriate techniques for sharing and synchronizing data, you can build robust multi-worker applications with Playwright.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

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.