Highlighted
Contributor.
Contributor.
236 views

How to validate the Response body data

Jump to solution
 

Hi All,

I am new to LoadRunner. I was successfully able to create a script using vugen for API testing, It passed successfully.  I got the JSON response too. I wanted to validate the content present in Json response Body so that the value should not have a null or blank or empty.  Before I being my load test, I wanted to make sure that  error code should be 0,  companyName cannot be empty or null, likewise exchange, lastTradePrice, todayChange, bid, bidSize, ask, askSize, volume, high etc cannot be empty or null.

Please advise how this can be achieved. I wanted to make sure the data is valid for the performance test is valid.

Below is the Json Response Body

{

"errorCode":"0",

"quoteList":

{"FB":

{"companyName":"FACEBOOK INC CLA",

"exchange":"NASDAQ",

"lastTradePrice":"240.6343",

"todayChange":"1.1157",

"bid":"240.6",

"bidSize":"1",

"ask":"240.64",

"volume":"3199882",

"high":"241.9",

"low":"238.92",

"openPrice":"240.26",

"symbol":"FB",

"type":"EQUITY",

"securityType":"STOCK"

}

}

}

Regards

Suresh

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

Hi Suresh,

 

If you are using Web HTTP/HTML script, you may use  web_reg_save_param_json to query the element in JSON response you want and save it into a parameter for later validation. Below is an example, in which I want to query author from the JSON returned.

 

 

Action()
{
	web_reg_save_param_json(
		"ParamName=author",
		"QueryString=$..slideshow.author",
		SEARCH_FILTERS,
		LAST);

	web_custom_request("web_custom_request",
		"URL=http://httpbin.org/json",
		"Method=GET",
		"TargetFrame=",
		"Resource=0",
		"Referer=",
		"Body=",
		LAST);
	
	lr_output_message("author=%s", lr_eval_string("{author}"));

	return 0;
}

 

 

API helpcenter:

https://admhelp.microfocus.com/lr/en/2020/help/function_reference/FuncRef.htm#FuncRef/web/lrFr_web_reg_save_param_json.htm?Highlight=web_reg_save_param_json

https://admhelp.microfocus.com/lr/en/2020/help/function_reference/FuncRef.htm#LR_JsonPath.htm?Highlight=json%20path 

Hope it's helpful.

 

Thanks,

Tedy

View solution in original post

2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

Hello,

I recommend that for working with JSON responses you use DevWeb scripts.

You can set returnBody to true and then work with the JSON response body returned from the WebResponse object.

Then you would be able to simply script something similar to:

if (webResponse.jsonBody.errorCode == 0) {

    // Do something

}

Highlighted
Micro Focus Expert
Micro Focus Expert

Hi Suresh,

 

If you are using Web HTTP/HTML script, you may use  web_reg_save_param_json to query the element in JSON response you want and save it into a parameter for later validation. Below is an example, in which I want to query author from the JSON returned.

 

 

Action()
{
	web_reg_save_param_json(
		"ParamName=author",
		"QueryString=$..slideshow.author",
		SEARCH_FILTERS,
		LAST);

	web_custom_request("web_custom_request",
		"URL=http://httpbin.org/json",
		"Method=GET",
		"TargetFrame=",
		"Resource=0",
		"Referer=",
		"Body=",
		LAST);
	
	lr_output_message("author=%s", lr_eval_string("{author}"));

	return 0;
}

 

 

API helpcenter:

https://admhelp.microfocus.com/lr/en/2020/help/function_reference/FuncRef.htm#FuncRef/web/lrFr_web_reg_save_param_json.htm?Highlight=web_reg_save_param_json

https://admhelp.microfocus.com/lr/en/2020/help/function_reference/FuncRef.htm#LR_JsonPath.htm?Highlight=json%20path 

Hope it's helpful.

 

Thanks,

Tedy

View solution in original post

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.