Use parameters in scripts

Parameters let a script use values from earlier in the test instead of hardcoded data. During a run, Pie generates values such as phone numbers, emails and names. Reference them in a script’s command, and the same value reaches your API.

Before you start

  • Create the script and confirm its command works with static values. Add parameters only after the basic call succeeds. See Use execution scripts.
  • Know which earlier value the script needs, such as the phone number generated at signup.
  • If the script signs in or acts as a test user, check your stored logins in Credentials Manager.

Write the placeholder

Put the reference inside double curly braces in the script’s command:

{{use the phone number generated}}

When Pie runs the script, it replaces the placeholder with the generated value. If Pie generated a phone number at step two, a script at step five uses that exact number.

{
  "phone_number": "{{use the unique phone number generated at the beginning of the test}}",
  "password": "{{use the password created during signup}}"
}

Name parameters consistently

Use descriptive names in lowercase, with underscores between words. The script editor recognizes {{variable_name}} with letters, digits and underscores.

UseAvoid
phone_numberPhoneNumber
access_tokenaccessToken
user_passwordPASSWORD

Check the editor

Named placeholders, such as {{phone_number}}, show under Parameters Detected. Free-text placeholders, such as {{use the phone number generated}}, aren’t listed and get no field when you test. If you write a single-brace placeholder such as {phone_number}, the Issues panel offers the fix, {{phone_number}}. Select Fix for one issue or Auto-fix all to correct them together.

Test with real values

Select the script, then Test. Enter a value for each detected parameter, select Run Test and check the output before you use the script in a test. For a script with free-text placeholders, test a copy of the command with hardcoded values instead.

Examples

Use generated test data

Your test generates unique values during signup, and a later call needs the same values. In the script’s command:

"phone_number": "{{use the unique phone number generated at the beginning of the test}}"

Pie fills in the generated value when the script runs. You can reference any value that was generated or entered earlier in the test.

Use a password value

Reference a password created earlier in the test the same way:

"password": "{{use the password created during signup}}"

Chain API responses

Scripts can pass values to each other, which helps with multi-step authentication and setup flows:

  1. The first script makes an API call and receives a response, such as a challenge request ID.
  2. The second script uses that value and returns something new, such as an access token.
  3. The third script uses the token as a Bearer token in its Authorization header.

Describe the sequence in your test steps, and Pie passes the values along.

Use a token as Bearer authentication

When your API needs a value from an earlier step, insert it in the header:

curl -X POST "https://api.yourapp.com/endpoint" \
  -H "Authorization: Bearer {{use access_token from login_script}}" \
  -d ''

Pie inserts the access token from the earlier login script.

What you’ll see

The script runs with the substituted values, and its output is available to the steps that follow.

If something goes wrong:

  • A parameter isn’t replaced: check that an earlier step generates the value, that step finishes before the script runs, and your reference text matches the value you mean.
  • The script fails once you add parameters: run it with a hardcoded value first to confirm the endpoint works.
  • Values don’t pass between scripts: confirm the earlier script ran and returned the field you reference.

Next steps

Last updated on