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...

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.

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

Built-in 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 so that numeric timestamps and random alphanumeric strings can be generated within a step.

Available Built-in Variables

This will generate a numeric timestamp
This will generate a random 16 character alphanumeric string

So for instance, instead of assigning as the email address in your form, you can use test+{{TIMESTAMP}} 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.

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;