Extract a string or token from its input argument
The ParseResponse class extracts a string from its input argument using a regular expression for pattern matching.
package test; import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices; import java.util.regex.*; /** * * 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> * <span id=nw> * </span> * </small></h3> * <div class=ct> * • * <a href=s/213231>Cooler weather moving into eastern U.S.</a> * <br>• <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. */ public class ParseResponse implements com.ibm.rational.test.lt.kernel.custom.ICustomCode2 { /** * Create 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 token from an input argument. Useful for dynamic data correlation.
package test; import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices; /** * * 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. */ 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) { // Extract next-to-last token ArgStr = Tokens[Tokens.length - 2]; // Remove enclosing '' NextToLastStr = ArgStr.substring(1, ArgStr.length() - 1); } else { tes.getTestLogManager().reportMessage("Could not extract value"); NextToLastStr = null; } return NextToLastStr; } }