Links

Variables

Intro to variables

Why use variables?

Variables enable you to run and maintain tests efficiently in any of the following situations:
  • you work with multiple development environments
  • you have multiple tests that use the same value in multiple form fields or assertions
  • you want to test forms that require users to enter random and unique data, ex. registration with an email address
  • you want to run the same tests with different form input, ex. testing product searching against various search queries or testing different zip-codes

What are variables?

Variables are dynamic pieces of text that you can use in your tests. Use variables in tests by entering their name in curly brackets to place a variable in any text field in the test steps ex. {{myCustomVariable}}
Variables are good for storing:
  • Fragments of URL addresses like domains, subdomains, hostnames, etc.
  • Email addresses
  • Input values like product names, brands, phone numbers, ZIP codes, etc.
Example: variable "hostname" used in an URL
Example: definition of variable "hostname" in the variables tab
Variables do not have to be simple text assignments. Some built-in variables are different each time you run the test for example {{timestamp}} or generate random values like {{testRunId}}.
You can also write your own javascript variables - functions that return a value that is calculated every time you use the variable.

Using text variables

Create custom text variables

This is the most basic type of variable. Go to the "Variables" tab and click "New variable". Add a name and value for your variable.
Important! You can't use spaces in a variable name. We recommend that you use camelCase.

Use variables in tests

Use a variable name in curly brackets to place a variable in any text field in the test steps. You can combine variables with normal text, for example {{hostname}}/v1/index.html
Example: a variable used in "Go to url" action
You can use variables in other types of actions, such as typing text in a form, assertions, and even in CSS selectors.
Use variables in selectors, assertions, etc.

Use built-in variables for dynamic or random values

Use BugBug's pre-defined set of variables to handle your complex testing scenarios. Here are some examples of what you can do with built-in variables
Built-in variable name
Type
Description
Usage example

testRunId

Text
The current id of the running test (UUID format).
You want to have a new random unique value for one specific test run, for example you want to test user sign-up and then log in.

testId

Text
The current id of the test (UUID format).
You want to test add/edit/remove operations in one test, for example, add a product and then remove the same product with a name Test Product {{testId}}

suiteRunId

Text
The current id of the suite (if the test is running from a suite, UUID format).
You want to have a new, random unique value that is the same across all the test runs in one suite.

runMode

Text
The current run mode for the test ("local" or "server").
You want to have different input or assertion value when the test is run on server.
randomNameFixed
Text
Random name that remains unchanged during the test run (8 letters).
You want to have a new, random uniqe name that is the same across all the test and used multiple time

scheduleId

Text
The current id of the schedule (if the test has been started via a schedule, UUID format).
You want to have a new, random unique value that is the same across all schedule run.

timestamp

Text
The current Unix Time in milliseconds (int format, e.g.: 1645710937798).
You want to know when your test runs or use it similar to randomNumber

randomNumberFixed

Text
Random number that remains unchanged during the test run (10 digits).
If you want to edit an element, find it later in test steps and use it or remove it.

profileName

Text
The current profile name for the test. Default: "Default".
If you want to enter a production/test URL depending on the profile, test runs

suiteName

Text
The current suite name for the test (if the test is running from a suite).
If you want to create an email that inform you about suit name that was run

testName

Text
The current test name.
You can mix it with timestamp to create a new subscription notification test.
Built-in variables also give you access to generators of random numbers and names that you can use right away in your tests.

Recording and working with variables

Currently, there is no way to use variables during the recording. You can't type variables when recording tests.
First, record the steps and then edit them manually. Edit the step and replace the text with the variable name in curly brackets.
Upcoming improvement! We are working hard to enable this feature in the future and allow you to store and enter variables directly during the recording.

Test user registration and login using variables

Built-in variables solve multiple problems of testing user sign-up and logging in.
Test case: as a user, I go to the homepage and click "sign up" button. I enter my email and password. I receive a verification email. I click the link. I use previous email and password to log in.
We're going to use variables to solve a number of problems
  • How to get a unique random email address
  • How to receive the email with the authorization link
  • How to use previously generated unique email and password in the login form

1. Prepare the email inbox

Register a new account in an email provider that supports plus aliasing or sub-addressing ex. Outlook or iCloud mail.
Important! Don't use Gmail as BugBug extension will fail in the Gmail app because of a bug caused by Chromium
Let's assume you registered such an email [email protected]

Important tips on Outlook configuration

Your emails may be at first classified as "junk mail", but later Outlook may just put them in your inbox and organize them into a conversation. To prevent this unpredictable behavior you need to change a few switches in the Outlook settings
Add BugBug email to safe addresses to prevent junk email classification
Disable the conversation view in Outlook settings
Disable the Focused inbox

2. Record the sign-up and login without variables

Create a new test and start recording. When you're about to enter the registered email address, remember to use the plus aliasing and add a random text after the "+" symbol for example [email protected]
We're going to replace this suffix later with a variable!
During the recording, open a new tab and access your email inbox. Use your standard email address without the +12345 part in the email log-in form.
Find the email with the authorization link.
Don't forget to check the junk email folder! To prevent that we recommend that you add BugBug email to "safe emails" in Outlook settings.
Important! Make sure you click the subject line of the email! BugBug needs to always open the most recent email, so your selector here needs to click the first email with a specific subject line, for example //SPAN[normalize-space(text())="Finish your sign-up"]
Now just click the Button with the authorization link and continue recording.
In the login form to your app, remember to again use the email address with the plus alias [email protected]
Now finish the recording. We now need to replace the recorded texts with variables manually.

3. Replace the email sub-address with a built-in variable

If you run the test without using variables, it will fail because you will get a validation "This email already exists".
We need to have a unique email address every time we run this test.
To do this, you can use a built-in variable testRunId
In the test find all the steps that use [email protected] and replace it with acmeautomation+{{testRunId}}@outlook.com

4. Run the test to see the results

Every time you run the test, you will see that a new unique email address is typed in the registration and login fields.
You will see a computed value in the step details.
Computed value will appear below the field after a test run

Profiles

What are profiles?

Profiles are your own presets for different variable values. You can run tests or suites with profiles to override your default variables to a specific value. This is especially useful if you work with multiple development environments.
Important To see the profile section in the test, you need to have at least 2 profiles.
You can quickly swap the profile in the menu near the "Run" button. This menu will not appear if you only have one default profile.
To add, remove and edit profiles go to the "Variables" tab
Override variables in the profile settings. Leave an empty field to keep the default value.

Run a test with a profile to override variables

First, make sure you read what are profiles
  1. 1.
    Create a profile in the "Variables" tab
  2. 2.
    Go to test editing
  3. 3.
    Swap profiles before running the test in the menu near the "Run" button
You can also run tests with a profile from the command line in your pipeline
Important! Profiles selection is not saved in the test. If you run the test from the tests list, we will ask you to select a profile.
Tip! Your tests should pass on all profiles. Don't create tests that only work in one profile.

Run a suite with a profile

You can have multiple suites and each suite can run your tests with a specific profile. For example, you create suites for "Production smoke tests" and "Staging all tests". Each suite will have different sets of tests that are going to run on different profiles with different variables.
  1. 1.
    Go to "Suites" and select a suite to edit
  2. 2.
    Choose with which profile this suite will run
Important! This suite will always run with this profile. Profiles selection is saved in the suite settings.

Work with different development environments

Let's assume you work with 3 environments: production, stage, and local. Both are exactly the same, but they have different URL addresses. You want to sometimes run the test on production and sometimes on staging.
Environment
URL
Production
acme.com
Staging
stage.acme.com
Local
localhost:3000
Prepare the variables and profiles
  1. 1.
    Create a variable named hostname with default value https://acme.com
  2. 2.
    Create 2 profiles named Staging and Local
  3. 3.
    Override the hostname variable in the profile Staging for https://stage.acme.com
  4. 4.
    Override the hostname variable in the profile Local for https://localhost:3000

Update the URLs with your variable

  1. 1.
    In the tests, find your steps with Go to url action
  2. 2.
    Replace the acme.com with {{hostname}}.
Tip: you can also combine the curly brackets with the rest of the URL for example {{hostname}}/registration or app.{{hostname}}
Now you can run the tests with different profiles and quickly swap between your dev environments.

Override variables and profiles from the command line (CLI)

Use BugBug CLI to integrate with your build pipelines and run tests with variables that you can adjust to multiple environments.
If you run tests or suites from the command line, you can override each variable or profile with parameters, for example, --variable val1=value and --profile=profileName. Read more in the CLI documentation.

JavaScript variables

Javascript variables allow you to compute dynamic values every time the variable is used in a test.

Use JavaScript variables for

  • getting data from API, for example, SMS codes, authorization magic links
  • mathematical calculations such as adding, subtracting, multiplying, etc.
  • generating unique strings that need to meet specific validation criteria such as social security numbers, postal codes
  • values that are different depending on conditions, for example, if your app shows a different text during night and daytime

To create a JavaScript variable

  1. 1.
    Go to the "Variables" tab and click "New variable"
  2. 2.
    Change the variable type to "Custom JavaScript"
  3. 3.
    Enter a JavaScript function that will be executed every time this variable is used in the test
Important! Remember that your function needs to have a return statement - it needs to return a specific value.

Example JavaScript variable functions

Return a simple string value
async function(variables) {
let firstName = "John";
let lastName = "Smith";
return firstName + lastName;
}
Return a different value when the day of the week is Sunday
async function(variables) {
const d = new Date();
let day = d.getDay();
if (day === '7') {
return "Delivery in 48 hours"
} else {
return "Delivery in 24 hours"
}
}

Use the "variables" argument in your JavaScript function

Your custom JS variable can also read your other variables. The variables argument stores all the built-in variables and your custom variables. If you run a custom javascript step with a function console.log(variables) you will get such an output in the console:
{
hostname: "https://marmelab.com/react-admin-demo/"
profileName: "Default"
runMode: "local"
scheduleId: null //only for scheduled runs
scheduleName: null //only for scheduled runs
startDemoURL: "https://marmelab.com/react-admin-demo/"
suiteId: "54bd4384-ad9c-4e83-946f-111392ed0a82"
suiteName: "All tests"
suiteRunId: "18e1e3c3-ec2a-45c9-b817-8f48a56a0807"
testId: "37e64599-31a9-4707-9fed-a64cdec24294"
testName: "Test"
testRunId: "6498c9bd-81a5-412f-a275-2c7f1cfc4d45"
timestamp: 1665667075470
username: "demo"
}
You can use this to operate on the variables, combine them, calculate, etc.
async function(variables) {
return variables.firstName + variables.lastName
}
async function(variables) {
return variables.userEmail + variables.testRunId
}
async function(variables) {
return variables.userHeightInCentimeters + 20;
}
Important! Variables are calculated immediately before a step is executed. If you want to calculate variable only once per test, use localStorage to cache the result and then access it with custom javascript steps.
Important! Other Custom JavaScript variables are not available in the variables object. This is impossible because you can't specify the order in which the JS variables are calculated or control the dependencies. See the localStorage workaround below.

Use localStorage to pass on data between variables

If you want to access a variable that was already calculated in a previous step, use localStorage.
  1. 1.
    At the end of the function in the first variable, store the result in localStorage.
async function(variables) {
let name = variables.firstName + variables.lastName;
localStorage.setItem('bugbugUserName', name);
return name;
}
2. At the beginning of the next variable get the value from localStorage.
async function(variables) {
let name = localStorage.getItem('bugbugUserName', name);
if (name === 'Carl') {
...
}
}