Rigor’s Real Browser Checks step builder helps us build user flows or multi-step checks to test. While many basic interactions such as “Click” or “Fill in field” are available out-of-the-box, we may sometimes want to include custom steps that could require running JavaScript or saving custom variables from JavaScript that can be re-used in later steps. JavaScript is versatile and allows us to manipulate elements on a web page, send data, or make use of conditional logic.
In Rigor’s Real Browser Checks there are two ways to run JavaScript as part of a user flow:
- Run JavaScript: this check step type simply runs a snippet of JavaScript code, or
- Save return value from JavaScript: this check step type runs JavaScript code AND saves a variable returned from that code
Saving JavaScript Return Values to Custom Variables
Sometimes, it may be necessary to save values from JavaScript functions so that these values can be used for other steps in a check. Examples of such values include dates, timestamps, randomized data, or unique identifiers.
To do so, select “Save return value from javascript.”
In the input field with the placeholder text “value” enter the JavaScript you’d like to run that includes a line of code to return a value.
Then, this custom variable can be accessed in other steps using this format: **
Common Use Cases for JavaScript in Real Browser Checks
Here are some examples of how Javascript can be used in your checks:
-
Clicking on an element that is not directly accessible by the browser.
document.getElementById("hiddenBtn").click();
-
Executing instructions based on conditional statements.
if(document.getElementById("overlay") != undefined){ document.getElementById("overlayCloseBtn").click(); }
-
Sending data via a POST request.
$.post("demo.asp", //jQuery { first: "John", last: "Doe" } );
-
Running custom functions on a webpage
pageInit();
Tip:
Many Javascript functions like $.ajax and setTimeout are run asynchronously, which means that they may not return data before the browser moves on to the next step. If you make use of such functions when setting custom variables, your custom variables may not return the data you expect.