Conditional Step Execution

Ghost Inspector gives you the ability to add conditions to your step execution during a test run. You can use this feature to create branches of step execution within your test, for instance if you need your test to respond slightly differently in a different environment or you wish to import another test under a certain condition.

Adding a Step Condition

To add a condition to a step, simply click the Add Condition link in the step header using the Test Editor. The step background will change to indicate that it is contained within a condition and the condition JavaScript editor will appear. You can specify your condition logic using JavaScript code. The code will be executed at runtime when the step is reached. The code must use return to return a true or false value. If the conditional logic returns true then the step will execute. If the conditional logic returns false then the step will be skipped.

Adding Conditional Steps in the Test Editor

Conditional Step Grouping

When two subsequent steps have conditional execution enabled and the condition statements are the same, then the steps will group under the same condition heading. This makes it simple to update the condition for multiple steps with the same condition at once. The simplest way to add a step within a current condition is to click Copy and then Paste Below or Paste Above on the existing conditional step.

If you have an existing step before or after a conditional step that you would like to group, click Add Condition and then copy and paste the same JavaScript condition statement into the new condition editor. As a result, the steps will group together under the same condition heading.

Grouped Conditional Steps in the Test Editor

To remove a condition from a step, simply click <i class="fa fa-code-fork"/ > Remove Condition.

Conditional Evaluation Results

After test execution, any conditional step results will indicate the execution of the step condition and the value of the results in the test result:

Conditional Steps Results

Other Considerations

  • All conditions are currently evaluated as JavaScript.
  • Condition logic must return a “truthy” JavaScript value in order for the step to execute, otherwise it will be skipped.
  • JavaScript execution happens synchronously in the browser. The code has access to globals like window, document, etc.
  • Syntax errors or exceptions thrown during the JavaScript execution will result in a false condition result.
  • Ghost Inspector variables can be accessed in your JavaScript code using these instructions.
  • Nested conditions are supported. Conditions used on “Import Steps from Test” steps will be combined with any conditions on the imported steps themselves. This means that steps can accumulate multiple conditions when conditions are being applied to multiple levels of imports. The conditions will be effectively AND'ed together. Each condition will be checked in order. The step will be executed only if all conditions return truthy values.