Javascripts - Loop a Group until you get an expected response (2 methods)

Document created by B-F-F08DRX Employee on Jul 20, 2017Last modified by B-F-F08DRX Employee on Jul 20, 2017
Version 3Show Document
  • View in full screen mode

First Method.

 

The below JavaScript is for controlling an infinite Group loop until we get the expected response from the application based on a property. The below JS is the best and simplest approach since we don't need to access the response itself.

 

// This JavaScript needs to be set after the request that we want to check its propertyToCheck. It can be some elements after but inside the repeating Group. // It will repeat the GROUP until the expected response is gotten
// The group repeat must be set to infinite or a very large number
// Update line 20 to set the maximum repeat of the Group

// Get the property that will contain the status or text that will be used for looping
var propertyToCheck = $prop.value("clip", "propertyToCheck");

if (propertyToCheck != null && propertyToCheck != ""){
    // The propertyToCheck returned something
    var stringToCompare = "Post published"; // string to compare; hardcoded or can be a property, uncomment the next line and comment this one if that's needed
    // var stringToCompare = $prop.value("clip", "clipPropName"); // Getting the value of stringToCompare from a clip property

             // If the value does exist in the propertyToCheck, then end the group repeat
    if (propertyToCheck == stringToCompare){
        $context.result.postMessage($context.result.LEVEL_INFO, $context.currentGroup.playNumber + 1 + " loop(s) done. Expected value was found in propertyToCheck, ending the looping...");
        $context.currentGroup.endRepeat(); // Ending the Group repeat
    }
    // The below lines prevent having an infinite loop, 10 loops in this case, playNumber starts on zero
    else if ($context.currentGroup.playNumber == 9){
        $context.currentGroup.endRepeat(); // Ending the Group repeat
        $context.currentClip.end(); // Ending the clip iteration
        // Posting an error
        $context.abortScript($context.currentGroup.playNumber + 1 + " repeats done and expected text NOT FOUND in propertyToCheck, finishing the user iteration");
    }
    else
        {
        // Output an informational message
        $context.result.postMessage($context.result.LEVEL_INFO, "Still waiting for expected text in propertyToCheck: " + stringToFind);
    }
}
else{
      // Posting an error
      $context.abortScript("propertyToCheck was null or empty");
}

 

Second Method.

 

The below JavaScript is for controlling an infinite Group loop until we get the expected response from the application. This second method accesses all the body response.

 

// This JavaScript needs to be set just after the request that we want to check its response.
// It will repeat the GROUP until the expected response is gotten
// The group repeat must be set to infinite or a very large number
// Update line 44 to set the maximum repeat of the Group
// The request's scope must be set to Local

// Get the Message that precedes this Script.
var msg = $context.currentItem.previousItem;
// Variable for checking if getResponse is successful
var wasFound;
try{   
    // Get the response Body to the Message.
    var response = msg.getResponse(msg.RESPONSE_HTTP_BODY);
    wasFound = true;
}
catch (e){
    wasFound = false;
}
if (wasFound){
    // Was there a response?
    if (response == null)
    {
        // There was no response.
        // Output an informational message.
        $context.result.postMessage($context.result.LEVEL_INFO, "Message " + msg.name + " received NO response to validate.");
        $context.currentClip.end(); // Ending the clip iteration
        // Posting an error
        $context.abortScript("Message " + msg.name + " received no response to validate.");
    }
    else
    {
        // There was a response.
        var stringToFind = "Post published"; // string to find; hardcoded or can be a property, uncomment the next line and comment this one if that's needed
        // var stringToFind = $prop.value("clip", "clipPropName"); // Getting the value of stringToFind from a clip property
       
        // Determine if the value exists in the response.
        var searchIndex = response.indexOf(stringToFind);
        // If the value does exist in the response, then end the group repeat
        if (searchIndex > -1){
            $context.result.postMessage($context.result.LEVEL_INFO, $context.currentGroup.playNumber + 1 + " loop(s) done. Expected value was found in the response, ending the looping...");
            $context.currentGroup.endRepeat(); // Ending the Group repeat
        }
        // The below lines prevent having an infinite loop, 10 loops in this case, playNumber starts on ZERO
        else if ($context.currentGroup.playNumber == 9){
            // Clearing the response from memory
            msg.clearResponse();
            $context.currentGroup.endRepeat(); // Ending the Group repeat
            $context.currentClip.end(); // Ending the clip iteration
            // Posting an error
            $context.abortScript($context.currentGroup.playNumber + 1 + " repeats done and expected response NOT FOUND, finishing the user iteration");
        }
        else
        {
            // Output an informational message
            $context.result.postMessage($context.result.LEVEL_INFO, "Still waiting for expected text: " + stringToFind);
        }
    }
    // Clearing the response from memory
    msg.clearResponse();
}
else{
      // Clearing the response from memory
      msg.clearResponse();
      // Posting an error
      $context.abortScript("Getting the response body FAILED: " + msg.failureText);
}

Attachments

    Outcomes