How to share variables in Playwright code steps

Prev Next

This page explains how variables work in Playwright code steps and their scope.

Sharing variables between Playwright code steps

Variables defined in a Playwright code step (e.g., const myVariable = ...) are designed to be available in subsequent Playwright code steps.

These Playwright code step variables are valid only within the execution of the same scenario and are used when you want to temporarily pass values within that scenario.

Differences from JavaScript Steps

This variable-sharing behavior is different from JavaScript steps.

JavaScript steps execute code within the browser's context (the visited page), so they do not have the functionality to share variables between JS steps. Variables defined in a JS step are discarded once that step's execution is complete.

Distinction from Workspace Variables

Playwright code step variables are valid only within that scenario.

If you need to use variables that can be referenced across multiple scenarios within a workspace, please use workspace variables.

Find out more about Workspace Variables here.