Extracting a string or token from its input argument

The ParseResponse class extracts a string from its input argument. The ExtractToken class extracts a particular token (string) from its input argument. Both classes can be useful for handling certain types of dynamic data correlation.

The Javadoc for the test execution services interfaces and classes can be accessed from the product by clicking Help > Help Contents > HCL OneTest Performance API Reference.

The ParseResponse class extracts a string from its input argument, using a regular expression for pattern matching.
package customcode; 

import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices; 

import java.util.regex.*; 

/** 
 * The ParseResponse class demonstrates using Custom Code to extract a 
 * string from its input argument using a regular expression for pattern 
 * matching. 
 * 
 * In this sample, the args[0] input string is assumed to be the full 
response  from a previous request.  This response contains the day's 
headlines in a format such as: 
 * 
 *   <a class=f href=r/d2>In the News</a><small class=m>&nbsp;<span id=nw> 
 *   </span></small></h2> 
 *   <div class=ct> 
 *   &#149;&nbsp;<a href=s/213231>Cooler weather moving into eastern 
U.S.</a>  *   <br>&#149;&nbsp;<a href=s/262502>Digital camera shipments 
up</a><br>  * 
 * Given the above response, the extracted string would be: 
 *        Cooler weather moving into eastern U.S. 
 */ 

/** 
 * @author IBM Custom Code Samples 
 */ 

public class ParseResponse implements 
        com.ibm.rational.test.lt.kernel.custom.ICustomCode2 { 

    /** 
     * Instances of this will be created using the no-arg constructor. 
     */ 
    public ParseResponse() {} 

    public String exec(ITestExecutionServices tes, String[] args) { 
        String HeadlineStr = "No Headline Available"; 
        String RegExpStr = ".*In the News[^;]*;[^;]*;[^;]*;<a 
href=([^>]*)>([^<]*)<";         Pattern pattern = 
Pattern.compile(RegExpStr, Pattern.DOTALL);         Matcher matcher = 
pattern.matcher(args[0]);                 
        if (matcher.lookingAt()) 
            HeadlineStr = matcher.group(2); 
        else 
            tes.getTestLogManager().reportMessage("Input does not match 
pattern."); 
        return HeadlineStr; 
    } 
The ExtractToken class extracts a particular string from its input argument.
package customcode;

import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;

/**
 * The ExtractToken class demonstrates using Custom Code to extract a particular
 * token (string) from its input argument.  This can be useful for handling
 * certain types of dynamic data correlation.
 *
 * In this sample, the args[0] input string is assumed to be comma-delimited
 * and the token of interest is the next-to-last token.  For example, if
 * args[0] is:
 *    javascript:parent.selectItem('1010','[Negative]1010','1010','','IBM',
 *         '30181','Rational','1','null','1','1','6fd8e261','RPT')
 * the class will return the string 6fd8e261.
 */

/**
 * @author IBM Custom Code Samples
 */

public class ExtractToken implements
        com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

    public ExtractToken() {
    }

    public String exec(ITestExecutionServices tes, String[] args) {
        String ArgStr;
        String NextToLastStr;
        String[] Tokens = args[0].split(",");
                
        if (Tokens.length > 2) {
            ArgStr = Tokens[Tokens.length - 2];        // Extract next-to-last token

            // Remove enclosing '' 
            NextToLastStr = ArgStr.substring(1, ArgStr.length() - 1);
        } else {
            tes.getTestLogManager().reportMessage("Could not extract value");
            NextToLastStr = null;
        }
        return NextToLastStr;
    }
}