← Back to Discord Forum

How to share variables?

I am new to playwright and I would like to get an understanding of shared "resources. My use case is for a SaaS app, so for my most tests I need a tenant (called app) or some kind of other entity.

Therefore I defined project dependencies to reuse these entities. For example I have 'given-login', and 'given-app' which depends on the login.

In given login I create a new app, but I need this app name in the dependent tests. I don't want to hardcode it, but create exactly one app each run. So I created a fixture for that, but this does not seem to work:

type AppFixture = {
    appName: string;

const appName = `my-app-${getRandomId()}`;

export const test = base.extend<AppFixture>({
    // appName,
    // appName: [`my-app-${getRandomId()}`, { option: true }],
    appName: [async (_, use) => {
        const appName = `my-app-${getRandomId()}`;

        await use(appName);
    }, { auto: true }]

As you can see I tried several things. The first 2 versions create new app names each time it is used (so for the app creation and the actual tests) and the latest attempt does not show my tests anymore.


I just tried worker fixture and it does not work either:

export const test = base.extend<{}, AppFixture>({
    appName: [async ({}, use) => {
        const appName = `my-app-${getRandomId()}`;

        console.log(`Use app name ${appName}`);
        await use(appName);
    }, { scope: 'worker', auto: true }]

It is also executed for each test. I think it makes sense bcause the projects are independent. But would I share some stuff? Perhaps similar to application state over some files?

This thread is trying to answer question "How can I share variables in Playwright?"

4 replies

How to share variables?


Try creating a setup project in playwright.config.ts and make it a dependency of your main test project. The setup project will get run first, separate from your main test run - meaning it will only run once per test run. If your login also needs to be separate, you can also put that in a separate setup and make it a dependency of your app setup. See https://playwright.dev/docs/test-global-setup-teardown#setup-example.

Within your setup(s), you can then create your app and then store it's information in environment variables, which can then be referenced from any test in the test run when you need it.

Example login setup:

import { test as setup } from '@playwright/test'
import { Login, SeedUser } from 'shared/gql/users'

const superAdmin = <SeedUser>{
  email: process.env.SUPER_ADMIN,
  password: process.env.SUPER_PASSWORD,

setup('superLogin', async ({ request }) => {
  // Send authentication request. Replace with your own.
  const data = await Login(request, superAdmin.email, superAdmin.password)

  process.env.ACCESS_TOKEN = data.accessToken
  process.env.REFRESH_TOKEN = data.refreshToken

Example app setup:

import { test as setup, APIRequestContext } from '@playwright/test'
import { NewOrg } from 'shared/gql/organizations'

let apiContext: APIRequestContext

// Create a new APIRequestContext to be used for API calls
setup.beforeAll(async ({ playwright }) => {
  apiContext = await playwright.request.newContext({
    baseURL: process.env.API_SERVER, // stored in .env file
    extraHTTPHeaders: {
      'Content-type': 'application/json',
      Authorization: `Bearer ${process.env.ACCESS_TOKEN}`,

 * envSetup
 * Sets up a new test organization
setup('envSetup', async () => {
  // Check to see if org domain is available
  let orgData = await NewOrg(apiContext)

  process.env.ORGANIZATION_ID = orgData.organization.id

This is basically how I solve authentication, but the app name is dynamic, so that I have a new app (tenant) for each run and I could not get this working. I solved it by writing the app name into a file, similarly how the auth works as well.

Related Discord Threads

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.