Executing JavaScript in Your Tests

Execute your own arbitrary JavaScript code in your browser tests

Search our documentation:

Toggle Documentation Menu

Executing JavaScript in Your Tests

Ghost Inspector gives you the ability to execute custom JavaScript within the browser during your test runs. You can use this to feature to simply execute arbitrary code, return a true/false value as an assertion, or extract data into a variable.

Jump to...

Execute JavaScript

Using the Execute JavaScript operation, we can execute JavaScript code that interacts with the browser test’s current webpage. We can even output things to the browser’s console, which gets recorded with the test. This example takes the content of the #header element and outputs it with console.log().

Execute JavaScript


JavaScript returns true

Also available is a JavaScript returns true assertion. This assertion type allows you to execute custom JavaScript code and return true or false (marking the assertion as passed or failed, respectively). In this example, we get the computed style for the #header element and assert that the margin-bottom property is set to 30px.

JavaScript returns true


Extract from JavaScript

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

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

Extract from JavaScript