How can we help?

JavaScript steps in your tests

You are here:

Trudon gives you the ability to execute custom JavaScript functions within the browser during your test runs. Using functions in your tests is the best way to make sure they are reusable and easy to maintain.

You can use JavaScript code to simply execute arbitrary code, have custom actions or expectations, extract data into variables, etc.

Create a custom JavaScript function with Trudon

Go to the JavaScript functions section, click the create button, provide a name, a description, and select the project for it. A JavaScript function can be used only for tests within the same project.

Once the function is created, you can configure the actual code. To configure the code for a new function or to change the code for an existing one, just click on the desired function and add the custom code you need to be executed within your tests.

Saving a JavaScript function allows anyone in your organisation to use that function. With a descriptive function name and description, you will enable the rest of your team to understand what the step will do and easily reuse the step without them needing to understand the code.

Trudon JavaScript function syntax

All custom functions in Trudon should look like this:

function trudonJSStep(element, callbacks, builtinVars, customVars)
{

  // enter your code here
  // return restults in customVars, call callbacks, ...

}

As you can see, a custom JavaScript function should expect 4 variables:

  • element: If you assign a selector in your test step for this function, the element variable would be the result of window.document.querySelector(selector). Otherwise, it would be undefined.
  • callbacks: To support asynchrnous custom code, we provide three callback functions in this variable: callbacks.fail(text) – used to add a fail assertion; callbacks.pass(text) – used to add a passed assertion; callbacks.done() – used to finish the execution for this function. Basically, you can add multiple pass/fail assertions. However, you can (and should) call the done callback exactly once, after the completion of any asynchronous code that needs to be executed.
  • builtinVars: An object with all Trudon builtIn variables. You can only read values from this object.
  • customVars: An object with all the custom variables for your test. You can access previously defined variables or define new ones.

Execute a JavaScript function in your tests

Once you have create a custom functions, using it in your tests is very easy and can be assigned to your tests in two ways:

  • Using the Chrome extension: during the test recording, go to expectations mode, add an expectation to an element, click on the edit button and select the function you want to execute.
  • By editing an existing test: Go to the test you want to edit, find and click on the step that you want to execute code, click on the add step after button, select the custom JavaScript function action type and select the desired function. Optionally, you can also assign a selector, if you need an element for your function.

Examples of JavaScript functions

To better understand how to create custom JavaScript functions, please check the following examples:

We strongly encourage you to read the tips and tricks article as well.

Was this article helpful?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
Need help?