Sunday, April 15

RPT : Extracting value based on the occurrence that is passed dynamically

Rational Performance tester does correlation by extracting dynamic values in response by creating a reference. But it lacks ability for an user to specify if the occurrence dynamically that needs to extracted .
For ex: Say in an application, after user logins , he views displayed list of cards. He picks first card which is default and picks any other card alternatively sometimes. You got a task cut out to create a script which picks 60 percent default card and other 40 percent of time any of the alternate cards. You cannot select 'Random Occurrence ' option because it defeats purpose of simulating default card 60 % of time or vice verse.

So to overcome the limitation in RPT, one has to find a way to pass the occurrence you want to pick and pick the card by that occurrence value(not just random occurrence). Here in this blog I would focus on extracting that  value based on dynamic occurrence which is passed to a custom code interface . For this below are steps that need to be done

  1. Create a reference to extract entire response that contains the html text 
  2. Create a custom code and pass the this reference as an argument along with the occurrence value
  3. Write java code for this custom code interface that extracts value matching the pattern and occurrence and return the card number extracted
Step 1 : Create a reference to extract entire response that contains the html text containing all the cards
For this click on the response and from the right side of pane, click on content multiline text box(Do not select any text).
clip_image001
Right click and RPT displays menu as shown below with an menu item named create reference. Create field reference is different in a way from Create reference , that it extracts complete response into a reference field. While create reference will extract only part of text value that would have been highlighted by cursor. And then click create field reference and give it a appropriate name.
clip_image002
Step 2: Create a custom code and pass the this reference as an argument
Create a custom code element and Pass this reference as an argument to custom code interface as shown below.I have described in another blog on How to add reference item to custom code. In same, add the another argument that specifies the occurrence that needs to be extracted
image
Step 3 : Write java code for this custom code interface that extracts value matching the pattern and occurrence and return the card number extracted .
There are two ways to return this information to main test script from customcode. One by assigning value to already defined variable using put method call or by passing as return value of the custom code. Below code has both the methods shown


package customcode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
/**
* @author unknown
*/
public class RegExonResponse implements
com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {
/**
* Instances of this will be created using the no-arg constructor.
*/
public RegExonResponse() {
}
/**
* For javadoc of ICustomCode2 and ITestExecutionServices interfaces, select 'Help Contents' in the
* Help menu and select 'Extending Rational Performance Tester functionality' -> 'Extending test execution with custom code'
*/
public String exec(ITestExecutionServices tes, String[] args) {
String RegExpStr = "\"cardId\":\"(.*?)\"";
int i = 0;
int reqOccurence = Integer.parseInt(args[1]); // args[1] because this second argument passed to custom code
String CardBasedOnOccurence = null;
Pattern pattern = Pattern.compile(RegExpStr,Pattern.DOTALL);
IDataArea userDataArea = tes.findDataArea(IDataArea.VIRTUALUSER);
Matcher matcher = pattern.matcher(args[0]);
//tes.getTestLogManager().reportMessage( "Total " + matcher.groupCount());
while(matcher.find())
{
i = i + 1;
tes.getTestLogManager().reportMessage("First occurrence" + matcher.group());
if (i==reqOccurence )
{
CardBasedOnOccurence = matcher.group();
                                                /*Use this step if you have defined a variable named SelectedCardID in the
test else, you can skip this and pass value as return value from this custom 
code as shown in last few lines of code*/
userDataArea.put("SelectedCardID",CardBasedOnOccurence);
}
}

if (CardBasedOnOccurence==null)
{
return null;
}
else
{
return CardBasedOnOccurence;
}
}
}

No comments:

Post a Comment

---------------------------------------------

Related Posts Plugin for WordPress, Blogger...