We can create LoadRunner scripts for JSON request in multiple ways. I will take an example of REST API POST method and show how to create a LoadRunner script for JSON request.
After following above steps, you should be able to run your script successfully.
This method can be used when you have a very big JSON request. Drawback of this method is, you cannot parameterize any values inside JSON request. This would not help us when we need to run the test with multiple data. To overcome this you can make use of below method to create a script.
You can parameterize any values as you would do in any other script.
Method 1: Using web_custom_request function and a .json file.
Create a Web - HTTP/HTML script and insert web_custom_request function.
- Enter the Step Name, Method(POST), URL of your REST API, Record Mode(HTTP), Encoding Type(application/json).
- Create a ‘sample.json’ file under Extra Files.
- Place the JSON message in the sample.json file you created in previous step.
- In web_custom_request part, remove the line “Body” and add a new line BodyFileLocation. And the line should look like this after the change "BodyFileLocation=sample.json".
- Some Sites requires user authentication to access the content. In that case you may have to use user credentials along with your request. Use web_set_user to pass the user credentials.
- If you need to pass any headers into the script, you can do so by using web_add_headerfunction.
After following above steps, you should be able to run your script successfully.
This method can be used when you have a very big JSON request. Drawback of this method is, you cannot parameterize any values inside JSON request. This would not help us when we need to run the test with multiple data. To overcome this you can make use of below method to create a script.
Method 2: Using web_rest function and placing JSON request within the body of the script.
Create a new Web- HTTP/HTML script and insert the REST API function.
- Enter the REST API url, Encoding Type (application/json). Enter the JSON request in the body and insert the step.
- Pass User credentials and Headers as needed.
- Script would look like below after all the modifications.
You can parameterize any values as you would do in any other script.
NOTE:
- REST API insert step is supported only after LoadRunner Version 12.53.
- You may need to enable WinINet replay if you get any SSL related errors.
No comments:
Post a Comment