How to Create a Script for a REST API in Loadrunner
In this post i am going to explain about REST services testing in load runner by this How to Create a Script for a REST API in Loadrunner post you will get good knowledge about rest service testing.
What is REST ?
Rest is a short form of Representational State Transfer ,it has grown very popular model for software architecture mostly for Web applications.From past few year software companies are using REST instead of Web Services in their web applications,today we are learning how to create Load Runner script for REST API with examples and script.
WATCH VIDEO TUTORIAL WITH REAL TIME EXAMPLES
Components of REST API:
1.URI : Uniform Resource Identifier
The uniform Resource Identifier is a string which is having host address for API call,path which is used to identify the Call functionality and last one is pairs of some key values as below example.
http://hostserverip:portno/shopping_cart/product-details/paymentcardno
Host : hostserverip:portno
Path : shopping_cart/
Key Values: product-details/paymentcardno
2.Method :
In API method perform some actions like POST , GET , PUT and DELETE
POST: Post the values into database through REST API service.
GET : Get the values from particular REST API service ,which will display input Parameter with values in body.
PUT: Update the existing values with the use of PUT method.
DELETE : Delete existing values using DELETE Method with REST API service.
3.Header :
REST API service header will have different types such as JSON,XML,HTML etc.Mostly we are using JSON header format such as Content-Type : application/json.
For creating REST API service script in VUGen we will use below details such as
1.Create new script with Web – HTTP/HTML protocol
2.web_custom_request();
3.URL(REST API url)
4.Method
5.Resource
6.Mode
8.EncType
9.BodyFilePath
Create a Script for a REST API in Loadrunner
Here we are using web_custom_request() function to create the REST API script as below,
You can use data.json or simply follow below scripts.Simple you can write as below.
Action() {
//Pass the URL of REST API in web_custom_request
web_custom_request("CartDetails"
//Pass the REST URL here
"URL=http://hostserverip:portno/shopping_cart/product-details/paymentcardno",
"Method=POST",
//POST method to post the values ex: Login"
"Resource=0",
//zero means posting the values
"EncType=application/json",
//content-type value passing here as application/json
"Mode=HTTP",
//Mode is HTTP because we have selected protocol as Web - HTTP/HTML
"BodyFilePath=data.json",
//Please find the below details for data.json
LAST);
return 0;
}
====================================================================
Card_Balance()
{
lr_start_transaction("03_Get_Card_Balance");
lr_start_transaction("03_Get_Card_Balance");
web_custom_request("Bank_Card_Balance"
"URL=http://10.0.89.900:5080/Api/api1/ardbal/cardbalance",
"Method=POST",
"Resource=0",
"EncType=application/json",
"Mode=HTTP",
"Body={\"cardNo\":\"{Card_No}\",\"type\":\"{Type}\",\"wallet\":\"{Mobile_No}\"}", LAST);
lr_end_transaction("03_Get_Card_Balance",LR_AUTO);
return 0;
}
====================================================================
REST API with Parameterization
Card_Balance()
{
lr_start_transaction("03_Get_Card_Balance");
web_custom_request("Bank_Card_Balance",
"URL=http://10.0.89.900:5080/Api/api1/ardbal/cardbalance",
"Method=POST",
"Resource=0",
"EncType=application/json",
"Mode=HTTP",
"Body={\"cardNo\":\"{Card_No}\",\"type\":\"{Type}\",\"wallet\":\"{Mobile_No}\"}",LAST);
lr_end_transaction("03_Get_Card_Balance",LR_AUTO);
return 0;
}
As per above script I am passing input parameters in Body directly so that Vugen works as expected.
Card_No : You can pass 9876543212345678
Type: CC
Mobile_No: 9876543210
Card_Balance()
{
lr_start_transaction("03_Get_Card_Balance");
web_custom_request("Bank_Card_Balance",
"URL=http://10.0.89.900:5080/Api/api1/ardbal/cardbalance",
"Method=POST",
"Resource=0",
"EncType=application/json",
"Mode=HTTP",
"Body={\"cardNo\":\"9876543212345678\",\"type\":\"CC\",\"wallet\":\"9876543210\"}", LAST);
lr_end_transaction("03_Get_Card_Balance",LR_AUTO);
return 0;
}
REST API without Parameterization
Now above script looks same as below but directly passing values with out parameterization values.
Now run above code it will run successfully here i have used Parameterization of input parameters,I think it is very simple instead of using separate JSON file/without JSON file as explained above.Try this once and i hope you will enjoy writing scripts for REST API easily.
Conclusion:
Thank you for reading my post ,please share with many people so that any anyone get good knowledge about REST Api testing in load runner. In my coming posts you will learn how to perform parameterization ,web services testing using load runner. Please provide your suggestions ,comments here in case anything is missing feel free to comment.
Loadrunner Tutorials
lr_xml_find examples
WebServices Scripting
Rest API scripting