Variables
Last updated
Last updated
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
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.
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.
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:
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.
In general, the applied variables in your test might look like this:
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 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
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.
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}}
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.
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
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.
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
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.
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
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
Built-in variables also give you access to generators of random numbers and names that you can use right away in your tests.
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.
The BugBug Testing Inbox 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
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.
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.
Most of the registration processes require opening the verification email and confirming the address. With the BugBug Testing 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.
If you need to use an email address with a specific alias during a test you can also use the BugBug Testing Inbox. 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.
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 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.
First, make sure that you have read about what profiles are, and then:
Create a profile in the "Variables" tab
Go to test editing
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.
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.
Go to "Suites" and select a suite to edit
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.
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.
Production
acme.com
Staging
stage.acme.com
Local
localhost:3000
Prepare the variables and profiles
Create a variable named hostname
with default value https://acme.com
Create 2 profiles named Staging
and Local
Override the hostname variable in the profile Staging for https://stage.acme.com
Override the hostname variable in the profile Local
for https://localhost:3000
In the tests, find your steps with Go to url
action
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.
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 allow you to compute dynamic values every time the variable is used in a test.
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
Go to the "Variables" tab and click "New variable"
Change the variable type to "Custom JavaScript"
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.
Return a simple string value:
Return a different value when the day of the week is Sunday:
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:
You can use this to operate on the variables, combine them, calculate, etc.
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.
If you want to access a variable that was already calculated in a previous step, use localStorage
.
At the end of the function in the first variable, store the result in localStorage
.
2. At the beginning of the next variable get the value from localStorage
.