Showing posts with label Postman. Show all posts
Showing posts with label Postman. Show all posts

Friday, September 29, 2017

How to do simultaneous request calls using Postman collections

Newman is a command line collection runner for Postman. It allows you to run and test a Postman Collection directly from the command line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.

With Newman and Async, you can do simultaneous request calls which will be very helpful for performance tests.

Here are what you'll need to do to get it working:


  1. Install Newman by following the instructions on http://blog.getpostman.com/2015/04/09/installing-newman-on-windows/
    In my case, I didn’t have to install Python to get it working.
  2. Create/use a folder for the tests
  3. Export a Postman collection into a file
  4. Export a Postman environment that you want to invoke the calls to
  5. Open a command prompt in administrator mode on the folder from step 2
  6. Install async module by typing npm install --save async
  7. Create a .js file
  8. var path = require('path'),
      async = require('async'), //https://www.npmjs.com/package/async
      newman = require('newman'),

      parametersForTestRun = {
        collection: path.join(__dirname, 'postman_collection.json'), // your collection
        environment: path.join(__dirname, 'postman_environment.json'), //your env
      };

    parallelCollectionRun = function(done) {
      newman.run(parametersForTestRun, done);
    };

    // Runs the Postman sample collection thrice, in parallel.
    async.parallel([
        parallelCollectionRun,
        parallelCollectionRun,
        parallelCollectionRun
      ],
      function(err, results) {
        err && console.error(err);

        results.forEach(function(result) {
          var failures = result.run.failures;
          console.info(failures.length ? JSON.stringify(failures.failures, null, 2) :
            `${result.collection.name} ran successfully.`);
        });
      });

  9. Run the .js file from step 7 from command prompt node fileName.js

Saturday, September 16, 2017

More on Postman functions

Continuing from my posts on Postman, here are some of the Postman functions that I use from time to time.


  1. To assign a value from the response to an environment variable:
    var json = JSON.parse(responseBody);
    postman.setEnvironmentVariable("bearerToken", json.access_token);


    In that example, it is assigning the access_token from the response to a variable called bearerToken
  2. Test conditions to decide the test result. This is to make it easier to see which request is failing and which one is not failing.
    tests["Status OK"] = responseCode.code == 201

    In this case, Status OK will be considered to be failed if the response status code is NOT 201.
  3. Randomise an integer between certain values
    _.random(1,5)

    In this case, it will return any integer number between 1 and 5.
  4. Randomise an integer value
    {{$randomInt}}

    In this case, it will return a random integer number.
  5. When you create a collection of requests in Postman, you can then run them using the collection runner. By default it will go by the request order in the collection. You can use this function to direct the sequence if you need to deviate from the default order:
    postman.setNextRequest("call name");
    If you put call that in the "Tests" script, then after running the current call, it will then run another call named "call name".

    postman.setNextRequest(null);

    The above will stop the execution.

    With this function you can basically call the same request more than once (for example to create multiple lines).
    Please note that this function only works in the Collection Runner.
In the next post, I will explain how to use Postman collection and environment data to do multiple concurrent calls, which will be very useful for performance tests.

Wednesday, August 30, 2017

Testing Dynamics 365 for Operation Recurring Integrations with Postman

Continuing on the last post, let's do some tests on the D365O recurring integrations with Postman.

Initial setup

We still need the "GetToken" call as before. Please check on the previous post for information on how to set this up.

Next on the D365O data management workspace, we need to create an import and an export projects. To make it simple, let's use the "Customer groups" entity.

Create an import project as usual, and you need to create a sample of import file. Once you click the "Upload" button, then click the "Create recurring data job" button at the top. You will then need to:

  • Specify a name
  • Specify the application ID, and tick the checkbox beside it
    (This is the client ID that we use in the GetToken call)
  • Click the "Set processing recurrence" and set the job recurrence
  • Click the "Set monitoring recurrence" and set the monitoring recurrence
  • Keep note of the job ID
  • Click OK
Next, create an export project as usual. After you click the "Add entity" button, then click the "Create recurring data job" button at the top. You will then to do the same as above.

Send a file to the recurring import job

Open Postman, and execute the "GetToken" call to get the access token. Then create a new POST call with this url https://<axurl>/api/connector/enqueue/<activity id>?entity=<entity name>

Activity ID is the job ID that was displayed when you created the recurring jobs.


Then click on the Body tab, choose Binary, and click the "Choose Files" button to choose the import file. After that just click the "Send" button.

If it's successful, it will return with a job ID and HTTP 200.

You then can inquire the status of the job by doing a GET call to this url: https://<axurl>/api/connector/jobstatus/<activity_id>?jobId=<job_id> 
The result will then show you the job status, including the job started/completed date time and execution logs

Please note that you always need to add the authorisation and the access token in the message header with every call.

Receive a file from the recurring export job

Open Postman, and execute the "GetToken" call to get the access token. Then create a new GET call with this url https://<axurl>/api/connector/dequeue/<activity id>
If it's successful, it will return with a download location and HTTP 200.

Then you'll need to create a new GET call and use the download location as the URL, and then instead of clicking the "Send" button, you'll need to click the "Send and Download" button. Postman will then display a dialog where you can use to select the location where you want the file to be saved.

After downloading the file, you should send an acknowledgement:
https://<axurl>/api/connector/ack/<activity_id>
{
  "CorrelationId": "<CorrelationId>",
  "PopReceipt": "<PopReceipt>",
  "DownloadLocation": "<DownloadLocation>"
}

The body (correlationId, popReceipt, downloadLocation) should be the same as the body from the original dequeue request.

Please note that you always need to add the authorisation and the access token in the message header with every call.