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, e.g. registration with an email address

  • You want to run the same tests with different form inputs, e.g. 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.

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 on the "New variable" button.

Add a name and value for your variable.

Then click on the "Create variable" button to save it.

Important! You can't use spaces in a variable name. We recommend that you use camelCase.

Create custom JavaScript variables

Previously, a custom variable containing a text value was added. Now you can go further and use it with a JS variable that can generate a unique value. As a support, you are going to use the built-in variables in BugBug as well.

For starters, once again click on the "Create variable" button to create a new one, yet this time on the modal select the "Custom JavaScript" in the "Type" field.

Also, paste the JS code into the "JavaScript code" field. As an example, a simple function was used here:

return variables.testVariable + ' ' + variables.testRunId;

Next, click on the "Create variable" button, and we're all set.

Now you're ready to use the names of our created variables when editing a test. To do this, simply use the names of the existing variables, i.e. place them in the target field(s) on a page that's being tested.

{{testVariable}} or {{taskName}}

In general, the applied variables in your test might look like this:

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

You can use variables in other types of actions, such as typing text in a form, assertions, and even CSS selectors.

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 nameTypeDescriptionUsage 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, you can use variables during recording.

  • You can insert pre-prepared or built-in variables when recording tests.

  • You can also dynamically create variables during recording based on the source being tested.

  • You can also record the steps and then edit them manually.

For more details, go to the "Variables During Recording" document.

User registration and login using the BugBug inbox feature

The BugBug Inbox feature solves several problems of testing user registration and login.

Test case: As a user, I go to the home page and click the "Login" button. I enter my email and password. I receive a verification email. I click on the link. I use my previous email and password to log in.

You can use inbox to solve several problems, such as:

  • How to get a unique random email address

  • How to receive the email with the verification link

  • How to use previously generated unique email and password in the login form

Create your test using BugBug

First, start the test recording with a page that requires registration. Add steps that lead to the registration process. You don't need to set it up any particular way at this point. Record it as normal users perform their actions.

Use the Inbox feature to generate a unique email address

When you're ready to enter the email address, look at the BugBug extension overlay menu. There is an option called "Inbox". Click it, but do not stop the recording.

When you open it, you will see two options. Focus on the "Auto-generated random email unique for this test" option.

This option creates a random email address using the variable {{testRunId}} and the domain @bugbug-inbox.com. The {{testRunId}} variable is unique per test run, so the generated email address will be constant during a single test run. If you rerun the test, a new email address will be generated using this variable. On the BugBug web application, in steps, you will see it as {{testRunId}}@bugbug-inbox.com.

You can copy it directly from the extension using the copy icon.

And use it directly to register. If the registration process does not require a verification email, you can now go back to the extension menu and continue. If you need to confirm a verification email, go to the verification flow.

You can find the email address based on a variable in the test step. Now your test is free of duplicate registration email problems every time you run it.

Using the inbox feature to activate the registration email

Most of the registration processes require opening the verification email and confirming the address. With the BugBug inbox, you can open an email, automatically confirm it as a test step, and complete the process without using third-party providers. When you reach the moment of providing an email in the test steps, just copy the email address by an icon. Submit the form and open our inbox in a different tab by clicking the "open inbox" button. Do not stop the recording. Opening a new tab will be recorded and required.

In a new tab, you will see an inbox created specifically for an email address you used in the previous steps. Wait for a confirmation email. The inbox refreshes automatically, so there is no need to do it manually.

The BugBug inbox only saves emails for 10 minutes. After that time, all emails will be removed.

Open the registration email by clicking on "subject".

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())="Confirm your e-mail address"]

Now just click the button with the authorization link and continue recording.

When your email is confirmed you can return to the test page tab and continue.

Using specific email and virtual inbox features

If you need to use an email address with a specific alias during a test you can also use the BugBug Inbox feature. The menu provides an option for a custom alias.

Just select the "Specific test email" option.

If this option is selected, you can create your email alias by typing.

You create only an Alias for @bugbug-inbox.com.

Do not put other domains.

This option also allows you to open Inbox with a specific email you set.

If you decide to create an alias, make sure it's unique. We do not limit inboxes per project or test. Make sure that other users will not be able to interrupt your tests with the same email address.

Using variables with inbox feature

By default, inbox uses only the testRunId variable, but you can boost it by freely changing it in your recorded steps.

You can mix or change variables simply by changing the alias of the email and adding eg. a timestamp variable.

You can open a virtual inbox by combining https://bugbug-inbox.com/ and any variable.

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 when working with multiple development environments.

Important To see the profile section in the test, you need to have at least 2 profiles.

Run a test with a profile to override variables

First, make sure that you have read about what profiles are, and then:

  1. Create a profile in the "Variables" tab

  2. Go to test editing

  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 will run on different profiles with different variables.

  1. Go to "Suites" and select a suite to edit

  2. Choose with which profile this suite will run

Important! This suite will always run with this profile. The profile selection is saved in the suite settings.

Work with different development environments

Let's say you have 3 environments: production, staging, and local. They are all exactly the same, but they have different URL addresses. You want to run the test sometimes on production and sometimes on staging.

EnvironmentURL

Production

acme.com

Staging

stage.acme.com

Local

localhost:3000

Prepare the variables and profiles

  1. Create a variable named hostname with default value https://acme.com

  2. Create 2 profiles named Staging and Local

  3. Override the hostname variable in the profile Staging for https://stage.acme.com

  4. Override the hostname variable in the profile Local for https://localhost:3000

Update the URLs with your variable

  1. In the tests, find your steps with Go to url action

  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. Go to the "Variables" tab and click "New variable"

  2. Change the variable type to "Custom JavaScript"

  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, your custom variables, and local variables recorded during the test run.

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.

Important! Do not use the local variable and its combination in steps before BugBug captures the wanted value. Otherwise, BugBug will return an undefined string instead of the desired local value.

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. 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') {
        ...
    }
}

Last updated