On rare occasions, it might be necessary to modify a server-supplied cookie when performing data correlation on an HTTP test. Cookies are small individual files and although they are listed as a request header under ‘HTTP Request Details’ for a request, we can't change them here. We can only change them by using custom code.
Cookies are not set by a request header but displayed here as a comment to show the current state of server-supplied cookies at the time of this specific request. Here's an example test that has two servers supplied cookies and the values are 1P_JAR and NID.
So, how are these values set? They're always set by a Set-Cookie response header from a previous request. In our example, we only have one previous request and it has two Set-Cookie response headers, one for 1P_JAR and the other for NID.
During the playback time, the values of these cookies are set on the basis of what is in these Set-Cookie headers, unless we explicitly change them via custom code.
The SetCookieFixedValue example
We can provide a sample custom code to set a fixed cookie value. To access it, search for "custom" in Help – HCL OneTest Performance and click Setting and clearing cookies for a virtual user.
At this point, you must select the custom code starting from the string "package customcode;" all the way till the last closing bracket. Copy and paste the selected custom code in a Notepad.
Note the three highlighted boxes in the following screenshot.
When we add a custom code object to the test, it will be in the following format: < package name >.< class name >. In our case, we must name it as customcode.SetCookieFixedValue.< /class >
We need to change this line:
String newCookie = "MyCookie=CookieValue;path=/;domain=.ibm.com";
Now, let's change this string and then, we can copy the entire contents of the custom code from the Notepad to the custom code object in the test which we will create.
Setting the new cookie value
The string is simple and has only three parts: the name and value of the cookie to be set, the path, and the domain.
Let's change the cookie, 1P_JAR, from what is returned by the Set-Cookie response header to "123456".
The changed string will be as follows:
String newCookie = "1P_JAR=123456;path=/;domain=.ibm.com";
Now, we need to change the path and domain as well. To make the changes, find the Set-Cookie response header where the path and domain are set. In our simple test, it will be from the first request:
The value is "path=/;domain=google.com". Our path and domain must have this value. So, in the Notepad, replace the original string as follows:
String newCookie = "1P_JAR=123456;path=/;domain=.google.com";
Creating the custom code object in the test
We must create the custom code object after the Set-Cookie response header sets the value and before it is used in another request.
Highlight the request and choose ‘Insert > Custom Code’.
An error is displayed immediately, but that’s fine. It is just a known error.
Change the Class name to customcode.SetCookieFixedValue and then, click the ‘Generate Code’ button.
Now, the errors are fixed.
Technically, we have the custom code but it doesn't set a cookie value. In the custom code source editor, first ‘select all’ and then, ‘delete’ everything.
Then on the Notepad, ‘select all’ and ‘copy’.
Go back to the java source editor and paste the content from the Notepad.
Running the test
Save and then run the test. Afterward, display the test log. Highlight the line displayed after the custom code. Then click on the ‘Request’ tab under ‘Protocol data’.
We see the value of the 1P_JAR cookie is now "123456".
Parameterizing the custom code
Our custom code worked and we were able to modify the cookie, 1P_JAR. What if we wanted to modify another cookie, NID, in our example? We hardcoded the cookie name and value in the custom code.
We can add another line to the custom code and modify NID simultaneously when we modify 1P_JAR. But then again, what if we wanted to modify a third cookie, a fourth, or modify 1P_JAR to a different value?
Hence, hard coding values are fine but they limit the versatility of the custom code.
Let's modify the custom code to accept two arguments: the first will be the cookie name (e.g. 1P_JAR) and the second will be the cookie value (e.g. 987654321).
Take a look at the custom code again and note the class String accepts string arguments:
Public String exec (ITestExecutionServices tes, String args)
If we pass two strings to our custom code, the first string will be args and the second string will be args. Let's go ahead and modify the custom code to use args and args instead of "1P_JAR" and "123456". It is advisable to do this in a Notepad first as it's easy enough. We're building the string newCookie by concatenating multiple strings by using the + sign. This is all the Java that we need to know.
As done before, delete the current contents of the custom code in the Java source editor with the contents in a Notepad or just modify the Java source in place.
The next step is to pass arguments to this custom code. We can do this in the test editor.
If you are using hardcoded values for an argument, choose ‘Text’. If you are using a data source, use ‘Add’. For this example, we're using hard-coded values so, let’s choose ‘Text’ and type in the cookie name that we want to modify, which is 1P_JAR
For the second argument, type in the value of the cookie. Let's use "987654321".
Save the test and re-run it and look at the cookie value at runtime for 1P_JAR.
In this blog, we explored how to use custom code to modify a server-supplied cookie. Although the custom code is written in Java, we need not be a Java programmer to use the custom code. All we need to know is how to modify an existing example typically in a single line. So, you can go ahead and try the steps given in this blog. In less than 20 minutes, you'll be able to set cookie values with the custom code as well.