Rayrun
← Back to Discord Forum

How to pass the value fetched in one test block to another test block

I want to pass the value fetched in one test block to another test block

But, getting the value as undefined instead of original in another test block

Can someone please look into it and suggest the solution with example

This thread is trying to answer question "How can I pass the value fetched in one test block to another test block?"

1 reply

not entirely sure what the scenario/conditions are, but perhaps via an environment variable?

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.

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.