JavaScript Calls

You can execute a JavaScript call in a custom flow test. For example the following command retrieves the text content of the HTML element with the ID first-name.

Execute JavaScript: `document.querySelector(#first-name).textContent`

The result of the command is stored in the {js-result} variable, which can be used in other steps. For example:

type {js-result} as "first name"

You can also use JavaScript for a non-visual ascertainment. For example:

evaluate js: document.querySelector({selector}).textContent

Examples

  • Log any value to the browser console

    evaluate js: console.log({var1})

  • Convert a string to lowercase

    evaluate js: {str1}.toLowerCase()

  • Capitalize the first letter of a string

    evaluate js: {str1}[0].toUpperCase() + {str1}.slice(1)

  • Iterate through an array

    evaluate js: {array1}.forEach(item => doSomethingWithItem(item))

  • Access object properties or map key-value pairs

    evaluate js: {record1}.prop1 + {record1}.prop2 + {record1}['kebab-prop']

    You can also perform this follows:

    {record1.prop1} + {record1.prop2} + {record1['kebab-prop']}

  • Iterate through an object or map

    evaluate js: Object.entries({record1}).forEach(([key, value]) => doSomethingWithKeyAndValue(key, value))

The following sample script demonstrates the following steps:

  1. Navigates to the page https://example.org,

  2. Retrieves and validates the text content of a specific element

  3. Fetches a UUID (Universally Unique Identifier) from an external service.

  4. Sets the text content of an element to this UUID.

  5. Validates that the text content was correctly updated.

Copy
Go to https://example.org
set variable selector = "h1"
evaluate js: document.getElementsByTagName({selector})[0].textContent
assert that {js-result} is "Example Domain"
get "https://httpbin.org/uuid" 
set variable rand = {response.body.uuid}
evaluate js: document.querySelector({selector}).textContent = {rand}
evaluate js: document.querySelector({selector}).textContent
assert that {js-result} is {rand}