Creating and Using Variables

Variables can be created and used in later steps during your tests

Search our documentation:

Toggle Documentation Menu

Creating and Using Variables

Jump to...

Video Tutorial


Set Variable

One common scenario where this comes up is when we need to generate a unique email address for a signup form, then wish to assert that email address later on.

Storing a unique email into a variable

You can see in the test above that we’re creating a unique email address (using one of our built-in value generators) then storing it in a variable named “email” with the “Set Variable” command. The value in the “email” variable can then be accessed using notation. You can see that we’re assigning it to the email field, submitting the form, then asserting that the email address is present in the display element on the following page.


Built-in Variables

We offer a range of built-in variables that can be used for making unique values and creating filler details. We've categoried these options into "Simple" and "Specific" variables.

You may find that you need to generate a unique value each time a test is run. For instance, you may need to create a new email address each time you test your website’s signup form. We provide two built-in variables for a numeric (millisecond based) timestamp and a random alphanumeric string, respectively.

Simple Built-in Variables

{{timestamp}}
This will generate a numeric timestamp (ex: 1565047520521)
{{alphanumeric}}
This will generate a random 16 character alphanumeric string (ex: x5Rq9vZ0meC6wlP3)

Instead of assigning test@domain.com as the email address in your form, you can use test+{{timestamp}}@domain.com to generate a unique email address with each test. If you are looking to use the generated email address in various steps, we suggest using the “Set Variable” command to place it into a variable, which can then be reused in multiple steps.

To assign more specific values, the variables below can be used. You must be using Chrome or Firefox v66+ to have access to these variables. They are not available in our legacy browser options. Also be aware that these values are chosen from a pool of data and will repeat over time. You are not guaranteed unique values. For guaranteed uniqueness, consider using one of the "Simple" built-in options above.

Specific Built-in Variables

{{name.firstName}}
Random first name (ex: Fred)
{{name.lastName}}
Random last name (ex: Doyle)
{{name.prefix}}
Random name prefix (ex: Mrs.)
{{name.suffix}}
Random name suffix (ex: Jr.)
{{name.title}}
Random job title (ex: Product Division Supervisor)
{{company.companyName}}
Random company name (ex: Ondricka LLC)
{{address.streetAddress}}
Random street address (ex: 395 Russel Place)
{{address.city}}
Random city name (ex: Abbottville)
{{address.state}}
Random US state name (ex: Hawaii)
{{address.stateAbbr}}
Random US state abbreviation (ex: NM)
{{address.zipCode}}
Random US zip code (ex: 92624)
{{address.countryCode}}
Random 2-letter country abbreviation (ex: DK)
{{phone.phoneNumber}}
Random phone number in any one of various format (ex: 1-393-740-6680 x958)
{{phone.phoneNumberFormat}}
Random phone number in the ###-###-#### format (ex: 805-848-1499)
{{image.avatar}}
Random avatar image URL which can be used in a file upload step (ex: https://s3.amazonaws.com/uifaces/faces/twitter/ponchomendivil/128.jpg)
{{internet.email}}
Random e-mail address (ex: Erick_Lakin@hotmail.com)
Note that this will not always be unique. Use the test+{{timestamp}}@domain.com approach above for unique e-mail addresses.
{{internet.password}}
Random password (ex: rF_nnm3wAmdQvs5)
{{internet.ip}}
Random IP Address (ex: 240.152.174.36)
{{internet.color}}
Random color in hex format (ex: #074d24)
{{date.month}}
Random month name (ex: December)
{{date.weekday}}
Random weekday name (ex: Sunday)
{{date.past}}
Random past date (ex: 2016-05-23T01:44:11.857Z)
{{date.future}}
Random future date (ex: 2028-11-30T13:49:50.478Z)
{{commerce.productName}}
Random product name (ex: Unbranded Metal Car)
{{commerce.price}}
Random numerical price (ex: 418.00)
{{lorem.text}}
Random filler text (ex: Cum alias eos aliquam minus et molestias est. Aut cupiditate harum necessitatibus.)
{{random.number}}
Random number (ex: 5190)
{{random.uuid}}
Random UUID (ex: 70a41166-0b74-436b-9125-868e1f3640a1)

We've only listed the more common options above. For a complete list (including examples), visit the Faker.js documentation. Please note that the system module is not available.


Extract from Element

This command allows you to extract the contents of value of an element and store it in a variable for use in later steps.

Extracting a value from the DOM into a variable


Extract from JavaScript

This command allows you to return a value from custom client-side JavaScript and store it in a variable for use in later steps.

Extracting a variable from JavaScript

You can also use this functionality, along with JavaScript’s Date functions to create and use custom date strings in your test. This example will return the current date in YYYY-MM-DD format.

Storing a date into a variable


Suite-level Variables

Variables can be defined at the suite level under Settings > Variables. These variables are available within all tests in the suite when they are run.

Set suite-level variables


Organization-level Variables

Variables can be defined at the organization level. If you hover over your name on the top right of the application, then click on "Account Settings", you'll land on a page that lists your organization on the left side menu with various options underneath. Among them is a "Variables" item. These variables are available within all tests in your organization when they are run.

Set organization-level variables


Variables via Application Execution

You can also easily set variables on the fly when triggering tests or suites in our application using the "Run with Custom Settings..." option accessible in the "More" dropdown menu at the top of the page.

Run test with custom settings


Variables via API Execution

Variables can be passed into your test or suite runs as simple GET paramaters when you execute via the API. See our API documentation for details.


Escaping Variables

Ghost Inspector's variable syntax uses double curly brackets like this {{varName}}. This syntax will automatically be replaced by the system. In rare cases, you may wish to use a string with this syntax but avoid the replacement. For instance, you may want to assign the value {{varName}} into a text input as is, without it being replaced. This can be a bit tricky. Our current suggested approach is to format the string with single curly brackets, replace the single brackets with a double brackets using JavaScript, then assign the string with JavaScript like this:

var str = "String with {variable} in it";
str = str.replace(/{/g, "{{").replace(/}/g, "}}");
document.querySelector(".input").value = str;