Search code examples
apitestingautomated-testsnumberspostman

Is there a way of testing in Postman if the body respons of a variable contains text or numbers?


I want to test an API endpoint GET, where through this I retrieve a list of abbreviations which have an ID number. This is a snippet of how the respons body looks like:

[
    {
        "id": 11,
        "text": "A.D."
    },
{
        "id": 29,
        "text": "a.m."
    }

From here I want to test if all "text" instances contain abbreviations and not numbers.

I tried this 2 ways, but clearly is not working:

pm.test("Id includes a number", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.include(Number);
});
var jsonData = pm.response.json(); 
pm.test("Text check", function () { 
    pm.expect(jsonData.text).is.to.equal(parseInt());
});

Solution

  • Your response data is array, it started [ and ended ]

    So you needs to access item by index then can access content by key.

    And this console.log() can help to figure out it.

    var jsonData = JSON.parse(responseBody);
    console.log(jsonData.length)
    console.log(jsonData[0])
    console.log(jsonData[1])
    console.log(jsonData[0]["id"])
    console.log(jsonData[0]["text"])
    console.log(jsonData[1]["id"])
    console.log(jsonData[1]["text"])
    
    pm.test("Id includes a number", function () {
        pm.expect(jsonData[0]["id"]).to.equal(11);
    });
    
    pm.test("Text check", function () { 
        pm.expect(jsonData[0]["text"]).is.to.equal("A.D.");
    });
    

    test results in Postman

    console result