0

I generate an API and Collection for my app by applying the steps on the following article: The hidden gem: Postman API and Documentation feature.

You may try by using a test endpoint e.g. https://petstore.swagger.io (user:test, pass:abc123).

Here is an example json body that I am trying to generate:

{
    "name": "{{$randomLoremSentence}}",
    "description": "{{$randomAdjective}}",
    "productUuid": "{{productUuid}}",
    "address": "{{$randomLoremSentence}}"
}

However, I am looking for a practical way for generating json body for Postman requests. Is there a proper way for this? Or do ı have to build each request manually? I think there must a a smarter way. Any idea?

Jack
  • 1
  • 21
  • 118
  • 236
  • Programmatically in prerequest script? Pass info thru environment? Unclear what the problem is. – matt Jul 12 '21 at 19:49
  • Collection runner might help you to do some of the "smart" things https://learning.postman.com/docs/running-collections/intro-to-collection-runs/ – aksappy Jul 12 '21 at 19:57
  • @matt See my update pls? – Jack Jul 12 '21 at 20:07
  • @aksappy Thanks for reply, but I need to generate values instead of running runners ( I had already run one of them, but I need to generate). Any idea? – Jack Jul 12 '21 at 20:08
  • 1
    "Here is an example json body that I am trying to generate" It looks like you have in fact generated it. Just paste that into the request body and away you go. But as I said, if you prefer you can generate it in the prerequest script. – matt Jul 12 '21 at 20:39
  • 1. Your title and content of question has mismatched. 2. You can seach "pre-request script", "environment", "test" in postman, it will definitely help you out. – lucas-nguyen-17 Jul 13 '21 at 00:39
  • The JSON response body is not created within POSTMAN, it is generated by the response from a web API HTTP request. The API method you execute determines the response. – Andrew Halil Jul 13 '21 at 07:46
  • @Rosa Glad that my article helped , could you make clear what is the issue you facing with body ? – PDHide Jul 14 '21 at 09:31
  • @PDHide I followed that article and then wanted to create environment variables automatically after sending corresponding GET reguest. As an example, I send GET request for Product then I want to create all the response parameters as environment variable e.g. name, quantity and price. Then, when I post a request, I want to assign the name as `{{name}}` (that was set after first returned product request) instead of "dummy name". **>>>** – Jack Jul 14 '21 at 11:27
  • For this purpose, I want to generate necessary strings on **Pre-request script** and **Tests** fields of the request. So, I am wondering if people build such kind of scripts and set the necessary parameters manually or if there is a way to generate these values automatically? – Jack Jul 14 '21 at 11:27
  • @Rosa the article is about how to create the base skeleton structure for your request. Once thats done , you need to change the content accourding to your use – PDHide Jul 14 '21 at 11:53
  • Thanks a lot, but as far as I see, it does not generate Pre-request script and Tests fields. Is that true? – Jack Jul 14 '21 at 12:24

1 Answers1

1

The JSON response body is not created within POSTMAN, it is generated by the response from a web API HTTP request.

The API method that is executed determines the response.

Once you have determined the response and it's structure, you can then create the request and test script within a POSTMAN Collection.

It is easier to manually test each HTTP request with sample inputs then copy that into an existing Collection, then write the test scripts for each test case, template any input parameters into URL query strings or the JSON request body with global or collection scoped variables.

After you have determined how to parameterize and template each request (and both the Test Script and Pre-request Script), you will then be able to implement the test script to create assertions on the JSON response content using BDD expressions.

I recommend looking at the POSTMAN documentation at

https://learning.postman.com/docs/writing-scripts/test-scripts/ https://learning.postman.com/docs/writing-scripts/script-references/test-examples/

as it shows some really good examples on how to create a basic test, then automate it using JavaScript, Chai BDD language and the POSTMAN Collection Runner.

This is based on my experience with POSTMAN. I am not aware of any simple way to automate request and test script creation from API Swagger definitions as every API method response could have any number of potential responses based on different inputs, so this (I believe) has to be constructed manually by the tester.

Andrew Halil
  • 1,195
  • 15
  • 15
  • 21
  • Thanks a lot, voted up. I also have a look at these pages you suggested. But, I am not sure if there is only one option for creating test and pre-request scripts. – Jack Jul 14 '21 at 11:48
  • As an example, I send GET request for Product then I want to create all the response parameters as environment variable e.g. name, quantity and price. Then, when I post a request, I want to assign the name as {{name}} (that was set after first returned product request) instead of "dummy name". >>> – Jack Jul 14 '21 at 11:48
  • For this purpose, I want to generate necessary strings on Pre-request script and Tests fields of the request. So, I am wondering if people build such kind of scripts and set the necessary parameters manually or if there is a way to generate these values automatically? – Jack Jul 14 '21 at 11:49
  • There is no known way (that I am aware of) in automating the generating of the test scripts within POSTMAN, as I mentioned, the parameters from the API requests are determined from the API service. The only other possibility would be if you could find a utility that could read the API controller methods, generate the Swagger document from it, then generate the test scripts from that through a UI interface where the inputs and expected outputs for each API method could be specified. – Andrew Halil Jul 15 '21 at 11:55
  • Having reviewed the documentation on the POSTMAN site, the procedure I mentioned is the only one I could find, and is the one I use. Once you create one set of tests in a collection and they are parametrized, you can then re-use the same tests with different input and/or output parameters. – Andrew Halil Jul 15 '21 at 12:06