Scripting conditional pages

Conditional pages can be scripted using custom JavaScript that enables branching and looping with a script.

Before you begin

Scripting conditional pages requires the following:

  • Knowledge of how to manually add actions into a GSLv2 script through the Recorder.

  • Knowledge of how to script within the control scope.

  • Understanding of the GSLv2 JSON structure.

  • Some JavaScript knowledge, because it may be required for more advanced custom scenarios.

Play back the script.

If the conditional page appeared and was recorded, go to Step 4. Otherwise, continue with Step 3.

If the conditional page did not appear during playback, add it to the script.

Find the location in the script where the conditional page should appear. Add a step boundary for the page step, then manually add the user actions you need to perform on the conditional page.

Add a Custom action Custom action at the point in the script where the conditional page would finish loading (after a wait).

Change the Target Window for the Custom action to point to the DOM window containing the conditional text (if the transaction takes place within a single window, you would use gomez_top[0]), and add the following JavaScript. Replace <TEXT FROM CONDITIONAL PAGE> with a text string unique to the conditional page.

 if (document.body.innerHTML.indexOf('<TEXT FROM CONDITIONAL PAGE>') == -1)
   {
      gomez_control.setVar('doNotExecute', true);
   }
   else {
    gomez_control.setVar('doNotExecute', false);
     }

Add a second Custom action after the action created in Step 4.

Change the Target Window for the Custom action to control, and add the following JavaScript.

Replace <step_index> and <action_index> with the index values for the first action to ignore if the conditional page does not appear. These are 0-based indexes: the first step would be script.steps[0], the second step would be script.steps[1], and so on.

 if (doNotExecute){
  script.steps[<step_index>].actions[<action_index>].type='do_not_execute';
}

Copy the line of code you just modified, so there is one line for each action performed against the conditional page (including wait actions). Change each line's step and action indexes to match each action to which you want to apply the conditional.

Play back the script and verify that it works.

  • If the conditional page appears on playback, it should appropriately interact with that page and continue the script through completion.

  • If the conditional page does not appear on playback, the script should play all the way through completion. You will still see a screen shot for the conditional page (it will be identical to the previously captured page); you will, however, see no objects or response times recorded for that page.

Example: understanding the code

Below is a cleaned-up version of the custom code with comments included to explain what the custom code actually does. You can customize this code for more complex use cases (for example, if you have a challenge page with multiple questions/answers).

First Custom action

/* Create a new Locator object containing the target window's documentElement -
this locator can be modified if you are working within a frame or pop-up window. */

var loc = new Locator ({
 'locators': [['dom', document.documentElement']]
});
var el = loc.execute();

/* Check if the documentElement contains the conditional identifier text. */

if (el.innerHTML.indexOF('TEXT FROM CONDITIONAL PAGE') == -1){

/* If the text is found, create a variable within the control scope with a value of "true".
If the text is not found, set the value to "false". This variable will be used by the second
custom action. */

   gomez_control.setVar('doNotExecute', true);
} else {
	  gomez_control.setVar('doNotExecute', false);
}

Second Custom action

if (doNotExecute){

/* If the value of doNotExecute is "true", change the "type" element for that action to a
bogus value that the agent will ignore - repeat this line for each action requiring the
conditional. */

   script.steps[step_index].actions[action_index].type='do_not_execute';
}