Conditional steps

Run a step only when a JavaScript check in the page returns true.

Add a condition

Select Add condition on a step. A block labelled Execute step when JavaScript returns true: appears below the step fields. Select Remove condition to delete it.

A new condition starts as false, so the step is skipped until you replace it with your own check.

Conditions are available on every action except the request actions GET, POST, PUT, PATCH, UPDATE and DELETE.

How the condition runs

Before the step runs, the condition is executed in the page:

  • A bare expression is wrapped as return (expression); so you do not need to write return
  • Code that already starts with return runs as written
  • Variables are resolved first, and their values are escaped for JavaScript
  • A truthy result runs the step
  • A falsy result skips the step. A skipped step counts as passing and is reported as Step skipped: JavaScript condition returned false
  • An error thrown by the code fails the step with Condition evaluation error

Writing longer conditions

Code with several statements must start with return, otherwise it is wrapped as an expression and fails. Put the statements inside a function and return its result.

return (() => {
  const banner = document.querySelector('#cookie-banner');
  return banner !== null && banner.offsetParent !== null;
})();

Add a Click step on the banner's accept button and give it this condition. On runs where the banner is not shown, the step is skipped and the test continues.

document.querySelector('#accept-cookies') !== null

Using variables in conditions

Variable values are escaped as the inside of a JavaScript string, so wrap the placeholder in quotes.

document.title.includes('{{PRODUCT_NAME}}')