Rayrun
← Back to Discord Forum

How to share data from a before all?

captainjetskiposted in #help-playwright
Open in Discord
captainjetski
captainjetski

I'm working with Node JS and playwright test.

I have my tests set up such that I want all tests within a describe block to use data created within a before all hook.

currently I have

test.describe("test with config", async () => {
  let data; 
  test.beforeAll(async () => {
    data = 'configuration data returned from an api call' 
  })

  test('test on the config', async () => {
    //... test runs using "data" 
  })

  test('other test on the config', async () => {
    //... test runs using "data" 
  })
});

but based on what we are seeing another describe block is using the information from "data" , which leads me to believe that this configuraiton is wrong.

I'd love to know the best way to share data taken from a before hook

This thread is trying to answer question "What is the best way to share data taken from a 'before all' hook in Node JS and playwright test? Can 'let data' be declared before describe?"

4 replies

can you not declare let data before describe?

captainjetski
captainjetski

is that different?

captainjetski
captainjetski

I mean I can but I have two sets, so what the person who wrote the code did was they had

test.describe("test with config A", async () => {
  let data; 
  // ... 
})

test.describe("test with config B", async () => {
  let data; 
  // ... 
})
captainjetski
captainjetski

however we are seeing concurency issues somewhere. But I'm not certian if its in the tests or the system under test

Answer

Sure, you can share data from a 'before all' hook in Node.js and Playwright tests using environment variables. Here's how you can do it:

Setting Environment Variables

First, you need to set the data as environment variables 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.DATA = 'some data';
}

export default globalSetup;

Accessing Environment Variables

Next, you can access the shared data in your tests through process.env. Here's how:

// test.spec.ts
import { test } from '@playwright/test';

test('test', async ({ page }) => {
  const sharedData = process.env.DATA;
  // Now you can use the sharedData in your test logic
});

Using environment variables is a great way to share data between different parts of your tests. It's like a centralized storage where you can retrieve shared information without passing it between functions or files.

Plus, it ensures that the shared data remains consistent throughout all tests in a single run. This is super useful when you're running tests concurrently or across multiple workers.

So, go ahead and leverage environment variables to share data in your Node.js and Playwright tests. It's an effective approach that can make your testing process smoother and more efficient.

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.