Custom JavaScript actions
When a built-in step can't do what you need, you can run your own JavaScript in the tested page: call an API, read localStorage, compute a value, or check something complex.
Where you can use JavaScript
async function(selector, variables)
element, selector, variables
Must be true (pass) or false (fail)
async function(variables)
variables
Becomes the variable's value. return is required.
Set variable step with Value: Custom JavaScript (evaluate in YAML)
async function(variables)
variables
Saved to the local variable as text
How your code runs
Write only the body of the function. The editor shows the function signature above the code field, and BugBug wraps your code in that
async functionitself. Usereturnto return a value andawaitfor promises. If you return a Promise, BugBug waits for it to resolve.const response = await fetch('/api/me'); const user = await response.json(); return user.email;The step's timeout applies. The code can run as long as the step's timeout allows. If it hasn't finished by then, the step fails. Increase the step's timeout for slow requests.
It runs in the tested page. You have access to
document,window,localStorageand the page's cookies. Requests made withfetchare sent from the page, so the API must allow its origin (CORS).Variables are in the
variablesobject. Usevariables.myVarorvariables.testRunId.{{myVar}}isn't replaced inside JavaScript code. JavaScript variables from the project or a profile aren't evaluated invariables: they have the text valueNOT_COMPUTED. See JavaScript variables in code and Use the variables argument.In YAML and the API, a Set variable step with Value: Custom JavaScript has
local_variable_source: evaluate, and Value: Text haslocal_variable_source: value.Objects are saved as JSON. When a JavaScript variable or Set variable returns an object or array, the value becomes its JSON text. Returning nothing saves the text
undefined.Code is checked when you save it. Syntax errors, and code that the checker warns about, can't be saved. A JavaScript variable without
returncan't be saved either.Errors fail the test. An error thrown by your code stops the test with the
CODE_EXECUTION_ERRORerror code and shows the message. A custom JavaScript assertion that returns something other than a boolean fails withINVALID_EXECUTION_RESULT.
When JavaScript variables are evaluated
A JavaScript variable defined in the project or a profile has no fixed value. BugBug runs its code each time a step uses {{name}} in one of its fields, for example the text of a Type text step or a URL. The result is used for that step only, and the next step that uses the variable runs the code again.
So a variable that returns a random value gives a different value in each step. To use the same value for the whole test run, for example a random email that you type at sign-up and check later, save it once:
Add a Set variable step with Variable name
email, Value set to Custom JavaScript, and the code:Use
{{email}}in later steps. The value stays the same until the end of the test run.
If no page is open yet when the variable is used, for example in the URL of the first Go to URL step, the code runs in an isolated sandbox instead of the tested page. It has no access to the page's document, cookies or localStorage there.
JavaScript variables in code
A JavaScript variable is evaluated only when it's used as {{name}} in a step field. In the variables object of a Run custom JavaScript step, a custom JavaScript assertion, a Set variable step or another JavaScript variable, it has the text value NOT_COMPUTED. This is by design.
To use its value in code, save it first:
Add a Set variable step with a Variable name such as
myVarValue, Value set to Text, and the text{{myVar}}.In later code, read
variables.myVarValue.
Pass a value to later steps
The return value of a Run custom JavaScript step is ignored. To use a computed value in later steps:
Use a Set variable step with Value set to Custom JavaScript. Its result is saved to a local variable that you use as
{{name}}in later steps and asvariables.namein later code.Or use
localStorageto pass data between JavaScript steps and variables: save withlocalStorage.setItem()and read withlocalStorage.getItem().
Example: save the hash of the current URL and check later that the URL still contains it.
Add a Set variable step with Variable name
pageHash, Value set to Custom JavaScript, and the code:Later, add a URL address assertion with contains
{{pageHash}}.
Use a project variable in code
{{apiUrl}} written inside the code isn't replaced. Read the value from variables instead:
Example: a BugBug Inbox mailbox from a login email
An Open inbox step takes only the mailbox name, the part before @bugbug-inbox.com. If your test has the full address in a variable, for example loginUsername, cut it with a Set variable step with Variable name mailbox and Value set to Custom JavaScript:
Then set the Open inbox step's Username to {{mailbox}}.
Examples
Make an API request
Request data from your API and use it in a JavaScript variable, a Set variable step or an assertion. The code runs in the browser, in the context of the tested page.
Compare the data shown in your app with the data from the server
Check that your currency exchange rate is up to date
Get test users and their parameters from a central test data file
Get a login and password for a random test user and skip registration
Get the latest item and its ID from the server
Check that the newest product is shown in the search results
Check an element with an assertion
A custom JavaScript assertion always has an element: set its selector like for any other assertion. In the code, element is the found element and selector is its selector. Return true to pass or false to fail. The expected value field isn't used.
Check that an input has a specific placeholder:
Record a regular assertion on the input.
Change its type to Custom JavaScript.
Enter the code:
To check something that isn't tied to one element, select a stable element such as body and use document in the code.

Prepare the page
Set a value in localStorage before the next steps, for example to skip a cookie banner:

FAQ
Last updated
Was this helpful?
