web_add_cookie function in LoadRunner

Before understanding the function web_add_cookie, let us understand what is a cookie. An HTTP cookie also is known as a cookie, web cookie, browser cookie, internet cookie may be defined as a tiny piece of data that is transferred between the user computer and a website that the user is browsing. Cookies remember stateful information and record our activity and store passwords, form data that the user had previously entered so that we do not need to fill the same form data again and again.

Cookies may be stored on the local system and can be used all the time if the same information is required in the request of the server. Let us understand the concept of permanent cookies and Temporary cookies.

We may define permanent cookies as session information that is stored on the local file system by the browser for later use. Different browsers used use different locations on the local system to store cookies information.

For example, Internet Explorer keeps this information in a Windows home subdirectory. Now let us understand the concept of cookies in terms of performance testing using LoadRunner.

What happens in Loadrunner is that it records a web_add_cookie() when a browser sends a cookie from its permanent cookie table to a server, which we can see many times after recording of the script of many scenarios.

What happens is that after getting cooking formation in the request, server sends a “Set-Cookie” header to a Vuser, as a result of which LoadRunner will add this cookie to the Vusers virtual cookie table.

These cookies can be used for any number of requests to the server which helps in improving the performance of execution as well. We have seen that if browser stores cookies and when we request for any web page.

It loads quickly because of cookies that are stored on the local system which improves the performance of the application as well. In LoadRunner, we use this function to add cookies.

We can remove cookies as well. So if we desire to remove a specific cookie from the table, we can use another function, we can use web_remove_cookie(…) function.

Now that we have understood that this function web_add_cookie adds, deletes, or replaces a cookie, let us see the syntax of the function which is given as below.

int web_add_cookie( const char *Cookie);

We can see that there is an argument of type string that contains many values. Let us see the format of this argument as given below.

The Cookie parameter has the following format:

name=VALUE; (required)

domain=DOMAIN_NAME; (required)

expires=DATE;

path=PATH; (default path is “/”)

secure

Let us understand it with an example given below.

In the following example, the web_add_cookie function adds a cookie with the name “client_id” to the list of cookies available to the script.

web_add_cookie (“client_id=China127B; path=/; expires=Wednesday, 09–Nov–2001 23:12:40 GMT; domain=www.cnn.com”);

Now that we have understood that it web_add_cookie function adds a new cookie, but apart from that, we have to understand one more thing that if there is match in name and path in an existing cookie, the existing cookie is overwritten with the new one. If the “expires” date has passed, the cookie is deleted.

Also, one more thing to be noted here is that this function has nothing to do with the cookies that are stored by the browser on the local system. It uses the cookie from the script using this function and whenever the script is executed, the function sends the cookies to the server.

It may be considered as temporary cookies used for adding cookies to the browser before starting the browser.

One more thing to be noted here is that when we get a Set–Cookie header from a server, the domain attribute is optional. If it is not specified, then the default value of domain is the hostname of the server that has generated the cookie response.

Now, Since this information is not available to the load generator host when web_add_cookie is called, the domain attribute is required.

Overall, we can say that it is a very important function to add, delete, modify the information. It is a very useful function and has to be used wisely for productive results while doing performance testing using LoadRunner.