Parameterization and Correlation in JOH (Java Over HTTP) protocol scripts

Parameterization and Correlation in JOH (Java Over HTTP) protocol scripts

The following details how to parameterize and capture dynamic data in a JOH script.

Parameterization

In the following example we have recorded a JOH script that adds an email address to a SiteScope server Email Preferences. The first thing to do is find the request to the server that contains the email address that we added. Note: probably the easiest way to do this is search the server request xml files in the script folder.

Once the relevant request has been found expand out the xml file from inside vugen. In the map argument you can see the fields with the email address and name that we added when recording the script:

To parameterize the _email and _name we need to replace the values in the object with our parameterized values. There are basically 2 approaches to doing this:

1) Use the objects normal public methods (i.e. get/set methods)

2) Use the reflection API wrapper provided by LoadRunner (lr2.fieldGetter and lr2.fieldSetter)

Approach 1) is normally the preferred way to do this but will normally need either good knowledge of the application classes or the relevant class files decompiling. Using this approach we can add the following code to set the _name and _email in the object. The added code is in blue font

////////////////////// requestBody37.xml //////////////////////

InvocationInfo InvocationInfo_create37 = (InvocationInfo) JavaHTTP.readObject(InvocationInfoBA37);

Map createDetails=(Map)InvocationInfo_create37.getArgs()[1];

createDetails.put("_name","Test Email");

createDetails.put("_email", "testemail");

……………….

Finally we replace “Test Email” and “testemail” with parameters:

////////////////////// requestBody37.xml //////////////////////

InvocationInfo InvocationInfo_create37 = (InvocationInfo) JavaHTTP.readObject(InvocationInfoBA37);

Map createDetails=(Map)InvocationInfo_create37.getArgs()[1];

createDetails.put("_name","<NameParam>");

createDetails.put("_email", "<EmailParam>");

………………

Approach 2). The other way to set data fields in objects is with the replection API wrapper provided by LoadRunner – specifically the lr2.fieldGetter and lr2.fieldSetter methods. These allow the manipulation of an object’s private data. The usage of these methods is documented in the Function Reference documentation. The following is the same example as above but using lr2.fieldGetter and lr2.fieldSetter:

////////////////////// requestBody37.xml //////////////////////

InvocationInfo InvocationInfo_create37 = (InvocationInfo) JavaHTTP.readObject(InvocationInfoBA37);

Map myMap = (Map)lr2.fieldGetter(InvocationInfo_create37, "args[1]");

myMap.put("_name", "<NameParam>");

myMap.put("_email", "<EmailParam>");

lr2.fieldSetter(InvocationInfo_create37, "args[1]", myMap);

………………..

Correlation

The same approach is used when it is necessary to perform correlation of a dynamic data. The difference is that the dynamic value first needs to be captured from the server response.

In the following example we want to capture the http port returned from Current User ID returned from the server. The ID is in the responseBody1.xml :

Again we can expand out the xml:

And use either Approach 1) or Approach 2) above to capture the data:

Using Approach 1)

////////////////////// requestBody1.xml //////////////////////

InvocationInfo InvocationInfo_getApplicationStateObject1 = (InvocationInfo) JavaHTTP.readObject(InvocationInfoBA1);

InvocationResult InvocationResult_ApplicationStateObject1 = (InvocationResult) JavaHTTP.sendSerialized(InvocationInfo_getApplicationStateObject1, 1,

"ObjectsDeserializerDefaultImpl",

"remoteProxy_2",

"URL=http://16.26.85.150:8080/SiteScope/remoteProxy",

new String[]{

"Method=POST",

"Resource=0",

"RecContentType=application/remoteclientresponse",

"Referer=",

"Mode=HTML",

"EncType=application/octet-stream",

LAST});// 1 is the number of the header file, record time response is at file responseBody1.xml

ApplicationStateObject applicationState = (ApplicationStateObject) InvocationResult_ApplicationStateObject1.getResult();

String currentUserId = applicationState.getCurrentUserId();

………………..

Using Approach 2)

////////////////////// requestBody1.xml //////////////////////

InvocationInfo InvocationInfo_getApplicationStateObject1 = (InvocationInfo) JavaHTTP.readObject(InvocationInfoBA1);

InvocationResult InvocationResult_ApplicationStateObject1 = (InvocationResult)

JavaHTTP.sendSerialized(InvocationInfo_getApplicationStateObject1, 1,

"ObjectsDeserializerDefaultImpl",

"remoteProxy_2",

"URL=http://16.26.85.150:8080/SiteScope/remoteProxy",

new String[]{

"Method=POST",

"Resource=0",

"RecContentType=application/remoteclientresponse",

"Referer=",

"Mode=HTML",

"EncType=application/octet-stream",

LAST});// 1 is the number of the header file, record time response is at file responseBody1.xml

String currentUserId = (String)lr2.fieldGetter(InvocationResult_ApplicationStateObject1, "result.currentUserId");

………………..

The saved data can now be written in a subsequent request body object in the same way as our parameterized values were – i.e. either using the objects normal public methods or using lr2.fieldGetter/lr2.fieldSetter.

Advertisements

3 thoughts on “Parameterization and Correlation in JOH (Java Over HTTP) protocol scripts

  1. sid

    it is really help full Sir . my only question is how to identify the dynamic values in rplay log that needs to be correlated since there is no tree view . and i have one java script which is recorded in web and then converted in to JOH. one of his post body he used lr_eval_string for perticular string. problem is that guy left the organisation . it is now i have to understand that , i am new to JOH . So rahim Sir could you please bhelp me with your valuble necessary inputs. my Id Is PTENGINERSID@gmail.com.

    Thanks
    Sid

    Reply
  2. siva

    Its really helpful sir .. but i still have an issue and needs your help.. As of now i am getting an error as
    System.err: 373 bytes still left which where not read!!!
    System.err: found ANOTHER subsequent object! : com.communication.bean
    System.err: com.communication.bean@471e30
    Error: System.err: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.communication.bean
    The below line is highlighted in red color:
    Error: System.err: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.communication.bean

    The script terminated before the sendSerialized line

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s