1

I have a simple api GET request and the response body is just like below. I'm using Postman API automation and I want to validate the response and check whether below conditions are met using a Postman test.

  1. Response array size should always be 2
  2. In each of the array item, the “build.time” value should be a date time.
  3. In each of the array item, the “build.artifact” value should be equals to “main_component”.
  4. “build.name” value of the 1st array item should equals to “web app”
  5. “build.name” value of the 2nd array item should equals to “back-end”.
[
    {
        "build.time": "2020-01-24 02:07:03 UTC",
        "build.artifact": "main_component",
        "build.name": "web app",
        "build.version": "1.0.0"
    },
    {
        "build.time": "2019-07-10 15:26:18 UTC",
        "build.artifact": "main_component",
        "build.name": "back-end",
        "build.version": "1.0.1"
    }
]
Prabodha
  • 520
  • 1
  • 6
  • 19
  • 1
    What have you tried so far? What exactly is not working with your tries? This platform is meant to provide help with specific issues, not to provide you with a complete solution. – Christian Baumann Aug 28 '20 at 07:02

1 Answers1

2

It was difficult for me to get around this UTC format, so I just converted it to string. Did the job, but will not fit all situations.

// Import moment.js for date validation
var moment = require('moment');

// Load response into object
const jsonData = pm.response.json();

// Test HTTP status code
pm.test("Check if HTTP status code is 200", () => {
pm.response.to.have.status(200);
});

// Check if response contains an array
pm.test("Check if response contains an array", () => {
pm.expect(jsonData).to.be.an("array");
});

// Response array size should always be 2
pm.test("Check if response array size is 2", () => {
pm.expect(Object.keys(pm.response.json()).length).to.eql(2);
});

// In each of the array item, the “build.time” value should be a date time.
pm.test("Check in each of the array item, the “build.time” value is of type datetime", 
() => {
pm.expect(jsonData[0]).to.have.property("build.time");
pm.expect(jsonData[1]).to.have.property("build.time");
});

// In each of the array item, the “build.time” value should be a date time.

// Get full timestamp
var timestamp = jsonData[0]["build.time"];

// Get time
var time = timestamp.substring(0,19);

// Get timezone
var timezone = timestamp.toString().substring(20);

// Test the time format
pm.test("Check in each of the array item, the “build.time” value is of type datetime", 
() => {
pm.expect(moment(new Date(time)).format("YYYY-MM-DD HH:mm:ss") == time);
});

pm.test("Timezone is valid", () => {
pm.expect(timezone).eql("UTC");
});

// In each of the array item, the “build.artifact” value should be equal to 
“main_component”.
pm.test("Check in each of the array item, the “build.artifact” value equals 
“main_component”", () => {
pm.expect(jsonData[0]["build.artifact"]).to.eql("main_component");
pm.expect(jsonData[1]["build.artifact"]).to.eql("main_component");
});

// “build.name” value of the 1st array item must equal to “web app”
pm.test("Check if “build.name” value of the 1st array item equals 'web app'", () => {
pm.expect(jsonData[0]["build.name"]).to.eql("web app");
});

// “build.name” value of the 2nd array item equals to “back-end”.
pm.test("Check if “build.name” value of the 2nd array item equals 'web app'", () => {
pm.expect(jsonData[1]["build.name"]).to.eql("back-end");
});
frankfurt-laravel
  • 3,731
  • 2
  • 20
  • 29