From 05527bf1107b415ea66883c366e3fb84496e2ef1 Mon Sep 17 00:00:00 2001 From: Mahesh Sundaram Date: Tue, 19 Apr 2022 05:44:30 -1000 Subject: [PATCH] docs: add example for global setup process.env (#13397) Co-authored-by: Max Schmitt --- docs/src/test-advanced-js.md | 58 +++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/docs/src/test-advanced-js.md b/docs/src/test-advanced-js.md index f769940901..34811acf76 100644 --- a/docs/src/test-advanced-js.md +++ b/docs/src/test-advanced-js.md @@ -211,7 +211,7 @@ test('test', async ({ page }) => { To set something up once before running all tests, use `globalSetup` option in the [configuration file](#configuration-object). Global setup file must export a single function that takes a config object. This function will be run once before all the tests. -Similarly, use `globalTeardown` to run something once after all the tests. Alternatively, let `globalSetup` return a function that will be used as a global teardown. You can pass data such as port number, authentication tokens, etc. from your global setup to your tests using environment. +Similarly, use `globalTeardown` to run something once after all the tests. Alternatively, let `globalSetup` return a function that will be used as a global teardown. You can pass data such as port number, authentication tokens, etc. from your global setup to your tests using environment variables. Here is a global setup example that authenticates once and reuses authentication state in tests. It uses `baseURL` and `storageState` options from the configuration file. @@ -301,6 +301,62 @@ test('test', async ({ page }) => { }); ``` +You can make arbitrary data available in your tests from your global setup file by setting them as environment variables via `process.env`. + +```js js-flavor=js +// global-setup.js +module.exports = async config => { + process.env.FOO = 'some data'; + // Or a more complicated data structure as JSON: + process.env.BAR = JSON.stringify({ some: 'data' }); +}; +``` + +```js js-flavor=ts +// global-setup.ts +import { FullConfig } from '@playwright/test'; + +async function globalSetup(config: FullConfig) { + process.env.FOO = 'some data'; + // Or a more complicated data structure as JSON: + process.env.BAR = JSON.stringify({ some: 'data' }); +} + +export default globalSetup; +``` + +Tests have access to the `process.env` properties set in the global setup. + +```js js-flavor=ts +import { test } from '@playwright/test'; + +test('test', async ({ page }) => { + // environment variables which are set in globalSetup are only available inside test(). + const { FOO, BAR } = process.env; + + // FOO and BAR properties are populated. + expect(FOO).toEqual('some data'); + + const complexData = JSON.parse(BAR); + expect(BAR).toEqual({ some: 'data' }); +}); +``` + +```js js-flavor=js +const { test } = require('@playwright/test'); + +test('test', async ({ page }) => { + // environment variables which are set in globalSetup are only available inside test(). + const { FOO, BAR } = process.env; + + // FOO and BAR properties are populated. + expect(FOO).toEqual('some data'); + + const complexData = JSON.parse(BAR); + expect(BAR).toEqual({ some: 'data' }); +}); +``` + ## Projects Playwright Test supports running multiple test projects at the same time. This is useful for running the same or different tests in multiple configurations.