execute javascript
interaction step allows you to execute arbitrary JavaScript or TypeScript code as part of your test flow.
This is useful for performing custom logic, calculations, or interacting with APIs that are not directly accessible through other test steps.

'execute javascript' interaction option in a test step, 05/2025
stderr
, the execute javascript
step fails.
Sandbox or browser execution
The code can run in 2 different contexts:- Sandbox: The code runs in a secure, isolated environment and cannot access the Playwright runtime or browser context directly. This restriction is in place for security and stability reasons.
- Browser: The code runs in the browser context and can access browser context directly. This is useful for performing custom on your application like setting cookies, etc.
Sandbox isolation and limitations
- No direct access to Playwright or browser context: Your script cannot interact with page elements, cookies, or the DOM directly.
- Resource limits:
- Runtime: Scripts are limited in execution time to prevent infinite loops and slow tests.
- Memory: Scripts have a capped memory allocation.
- Network: Outbound network requests may be restricted or rate-limited.
Browser isolation and limitations
- Code will be executed in the browser context and can access browser context directly. e.g. window, document, etc.
- You can access existing dynamic variables (set by previous steps) directly through the
context
object:context.variableName
- You can set dynamic variables by returning an object from your function:
return { variableName: value }

'execute javascript' interaction option executed in browser context, 07/2025
Using dynamic variables in browser context
When running JavaScript in the browser context, you have two special features:- Access existing dynamic variables through the
context
object:
- Set dynamic variables by returning an object from your function:
$$pageTitle
, $$userCount
, and $$currentUrl
.
Setting dynamic variables in sandbox context
You can create dynamic variables by calling a helper functionsetDynamicVariable
.
These variables can be used in subsequent steps of your test.
For example, if your script creates a new document in your application and receives a document ID in the response, you can expose this ID as a dynamic variable:
$$documentId
.

Example: Exporting a dynamic variable from a JavaScript step
Using dynamic variables in javascript steps
In sandbox context
In the sandbox environment, you can use dynamic variables in your test steps by using thegetDynamicVariable
function.
For example, if you have a dynamic variable $$documentId
, you can use it in a step like this:
Using secret template variables
You can use secret template variables in your test steps by using thedecrypt
function.
For example, if you have a secret template variable API_KEY
, you can use it in a step like this:
- Create a secret template variable in the Environment variables.

Example: Using a secret template variable in a javascript step
- Use the
decrypt
function to decrypt the secret template variable in your test step.
Helper functions
The following helper functions are available:setDynamicVariable(name, value)
: Set a dynamic variable.getDynamicVariable(name)
: Get a dynamic variable.decrypt(name)
: Decrypt a template variable.
Logging
You can log messages to the console using theconsole.log
function.

Example: Logging a message
Error handling
You can throw errors using thethrow
statement or output with console.error
will be considered as an error and the step will fail.
Best practices
- Keep scripts short and efficient to avoid hitting resource limits.
- Validate outputs before logging dynamic variables to prevent errors in later steps.
- Avoid sensitive operations since scripts run in an isolated environment with limited permissions.
- Use dynamic variables to pass data between steps and make your tests more flexible.
execute javascript
step, you can extend your tests with custom logic while maintaining security and reproducibility.