1

When submitting an order to an API we get a Json response containing:

"AMLResults": [
    {
        "amlResult": [
            {
                "addresses": [
                    {
                        "flatNo": "Flat 1",
                        "houseNumber": "32",
                        "line1": "Ayr Street",
                        "line2": "",
                        "town": "GLASGOW",
                        "postcode": "G21 4DG"
                    }

And then I'm trying to test that the address is returned and formatted correctly

let amlResult = {};
pm.test("response should contain a formated client address",function(){
jsonData = pm.response.json();
pm.expect(amlResult).to.have.property('addresses');
addresses = amlResult.addresses;
    pm.expect(addresses).to.have.property('flatNo', 'Flat 1');
    pm.expect(addresses).to.have.property('houseNumber', '32');
    pm.expect(addresses).to.have.property('line1', 'Ayr Street');
    pm.expect(addresses).to.have.property('line2', '');
    pm.expect(addresses).to.have.property('town', 'GLASGOW');
    pm.expect(addresses).to.have.property('postcode', 'G21 4DG');
});

However I keep getting the following failure response:

response should contain a formated client address | AssertionError: expected {} to have property 'addresses'

Any help would be appreciated.

1

Firstly, at

pm.expect(amlResult).to.have.property('addresses')

amlResult is an empty object, defined outside the test.

You may want something like this:

const firstAmlResult = jsonData.AMLResults[0];
pm.expect(firstAmlResult).to.have.property('amlResult');

Now you can inspect this AML Result object:

const result = firstAmlResult.amlResult[0]
pm.expect(result).to.have.property('addresses')

const address = result.addresses[0]
pm.expect(address ).to.have.property('flatNo')
pm.expect(result).to.have.property('flatNo')
pm.expect(result.flatNo).to.eql('Flat 1')
....

However, for all to.have.property checks, a better approach would be a Schema Validation. Tip #4 of this blog post explains it.

0

Validating by JSON Path Match:

var jsonData = pm.response.json();
pm.test("Check for house number", function () {
    pm.expect(jsonData.AMLResults[0].amlResult[0].addresses[0].houseNumber).to.eql(32);
});

You can also use an array find function.

var jsonData = pm.response.json();
var result = jsonData.find(x =>x.AMLResults.amlResult.addresses.flatNo ==="Flat 1");
pm.environment.set("houseNumber", result.houseNumber);

You'll look for the object where flatNo is "Flat 1"
Store the other values like houseNumber in a variable of set it as an environment variable.

So you can validate their value:

var jsonData = pm.response.json();
pm.test("Check if houseNumber is correct", function () {
    var houseNumber = pm.environment.get("houseNumber "); 
    pm.expect(houseNumber ).to.eql(32);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.