Rayrun

How do I share data between different tests in Playwright?

Answer

Sure, you can share data between different tests in Playwright. You can use the global setup and environment variables for this.

First, set the desired value as an environment variable in your global setup file. Here's an example:

// global-setup.ts
import type { FullConfig } from '@playwright/test';

async function globalSetup(config: FullConfig) {
  process.env.FOO = 'some data';
}

export default globalSetup;

Then, fetch the value from the environment variable in your first test block:

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

test('First Test', async ({ page }) => {
  const foo = process.env.FOO;
  // Now you can use 'foo' in your test
});

You can access the same environment variable in your second test block:

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

test('Second Test', async ({ page }) => {
  const foo = process.env.FOO;
  // 'foo' is available here too
});

This way, you can share data between different tests. Remember, these environment variables are only available within each individual test() function.

This approach assumes that both tests are executed sequentially and not concurrently. If you need to pass values between tests running concurrently or across multiple files, you might need additional synchronization mechanisms.

References

Thank you!
Was this helpful?
Still have questions?

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

Related Discord Threads

Related Questions

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 [email protected].