Hi,
Someone can please help me to write the test to get the value inside variable description?
This is my response:
{
"key1": value1,
"**description**": "text with id **ID** is already deprecated."
}
The value of the ID changes every time and to be updated I saved it in a variable “id_to_operate” , this is the test I tried written, but obviously it doesn’t work:
const jsonData = pm.response.json();
pm.test("Check value description", () => {
pm.expect(jsonData.description).to.eql("text with id " + {id_to_operate} + " is already deprecated.");
});
You could split the string using the spaces as a seperator, that would return an array of values and you can select the index for the ID:
let jsonData = {
"key1": 'value1',
"description": "text with id **ID** is already deprecated."
}
console.log(jsonData.description.split(' ')[3]);
pm.environment.set('id_to_operate', jsonData.description.split(' ')[3]);
That’s going to work if the sentence in the description never changes but it would fail it anyone were to change it.
Is the ID returned in a specific format? You could use regex if it were in a known format, to extract it from the value.
If so, then these are special characters and the targeting of that element using dot notation will fail. (Please see my example code below for a work around\alternative method for targeting the element).
I’m not sure how you have stored your id_to_operate, but that is not how you work with variables in scripts.
const response = pm.response.json()
let id = pm.collectionVariables.get("id_to_operate")
let expectedResult = "text with id " + id + " is already deprecated."
console.log(expectedResult)
let actualResult = response["**description**"]
console.log(actualResult);
pm.test(`description check for ID ${id}`, () => {
pm.expect(actualResult).to.eql(expectedResult);
})
When things fail, its useful to include the failure message, as that tells you a lot about what is failing. Although in this case, it potentially may have multiple issues.
I wasn’t sure. You would hope that developers would stay away from special characters in their JSON responses (but you never know).
I guess a key aspect is to use the console log.
Define the expected and actual results as separate variables and console log them. It makes it much easier to troubleshoot, particular if you are quite new to Postman or JavaScript in general.
The ** symbols are my mistake, as @danny-dainton wrote, they were in fact for show/ highlight.
However I would like to understand/know how I can compare the text inside the key-variable " description" with what the expected result should really be.
with the POST request I am trying to create something but with a body missing a key-value.
This is the correct response I get, but I would like to create a test that compares the value every time I try to create something without this key-value I get this description.
This is my Test: (of course the second test “fails”)
pm.test("Validate response code 400", function () {
pm.response.to.have.status(400);
});
const jsonData = pm.response.json();
pm.test("Check response description is as defined", () => {
pm.expect(jsonData).to.be.an("object");
pm.expect(jsonData.length).to.be.greaterThan(0);
jsonData.forEach(function (entry) {
pm.expect(entry).to.have.property("description");
pm.expect(entry.description).to.be.a("string");
pm.expect(entry.description).to.equals("Cannot create 'xxx' without 'xxx'");
});
});
This all depends on the type of assertion that you want in that test:
equal would be need to match that exactly or it’s going to fail.
contains would be slightly more flexible but could introduce other issues, if the description contains that string but also something that shouldn’t be in there - That would still pass the test.