I have a collection variable called artistId
i wanted to check whether in the response body the value of artistId matches the value of that variable
i think i did something wrong
i tried both of these:-
const response = pm.response.json();
pm.test(âExisting Artist Has Been Modifiedâ, function(){
var artistId = pm.collectionVariables.get(âartistIdâ)
pm.expect.response.artistId.to.eql(artistId);
})
pm.test(âExisting Artist Has Been Modifiedâ, function(){
pm.expect.response.artistId.to.eql(pm.collectionVariables.get(âartistIdâ))
})
and both gave me the same message:-
TypeError: Cannot read properties of undefined (reading âartistIdâ)
Calling variables the same name isnât something that I would recommend as it can get messy with scope.
Call the stored artistID something like expectedArtistId
The define the responseArtistId something likeâŠ
var responseArtistId = response.ArtistId;
The test then comes a lot easier to work with.
pm.test(âExisting Artist Has Been Modifiedâ, function(){
pm.expect(response.ArtistId).to.eql(expectedArtistId);
})
On face value though, your main problems is with this line.
pm.expect.response.artistId.to.eql(pm.collectionVariables.get(âartistIdâ))
Which should beâŠ
pm.expect(response.artistId).to.eql(pm.collectionVariables.get(âartistIdâ))
thank u so much
wking gr8 now
My Postman Test script required printing the length of the âclientIDâ variable. I tried the below Test script but it returns âUndefinedâ.
var responseJson = pm.response.json();
var cld = console.log((responseJson.content.outputVariables.oddOutput.clientId).length);
pm.test("ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is " +cld, () => {
pm.expect(statText).to.eql(expectedStatText);
)}
ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is undefined
@sreekanth.t.31
This should have been its own question.
Remove the console log from the definition of the âcldâ variable and then console log it separately afterwards.
var cld = responseJson.content.outputVariables.oddOutput.clientId.length;
console.log(cld);
If that doesnât work, you will need to paste an example response, so we can see the structure.
On another note, I canât see where the following variables are initialized in your example code.
pm.expect(statText).to.eql(expectedStatText);
Thank you @michaelderekjones
Below is my complete request and response
Test script:
var responseJson = pm.response.json();
var expectedStatText = "Unsuccessful";
var statText = responseJson.content.outputVariables.oddOutput.responseMessageList.transactionStatusText;
var cld = responseJson.content.outputVariables.oddOutput.clientId.length;
pm.test("ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is " +console.log(cld), () => {
pm.expect(statText).to.eql(expectedStatText);
})
Test Result
I am expecting Test Result as
âClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is 21â
Could you share the structure of the response, if the reference to that value is wrong, it would show as undefined.
{
"transactionId": "xxxxxx",
"content": {
"outputVariables": {
"oddOutput": {
"apiVersion": "xxxx",
"subClientId": "xxxxxxx",
"clientId": "xxxxxxxxx"
}
}
},
"status": "xxxxxxxxxx",
"startTimestamp": "xxxxxxxxxxxx",
"endTimestamp": "xxxxxxxxxx",
"timeTaken": 365
}
Ok, dealing with your initial issue.
You canât use console log in the test case name like that.
Use backticks and enclose the variable in ${} like the following.
pm.test(`ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is ${cld}`, () => {
pm.expect(statText).to.eql(expectedStatText);
})
This is using a concept called Template Literals and in particular string interpolation.
Template literals (Template strings) - JavaScript | MDN (mozilla.org)
I use it all the time to customize test case names, in particular if you are looping through a response.
However, Iâm still not understanding your test.
Your example response does not include the transactionStatusText so surely this will be âundefinedâ and fail the test.
responseJson.content.outputVariables.oddOutput.responseMessageList.transactionStatusText;
The test itself is called âlength must be 1-20 charactersâ, but you are not actually testing this but that the transactionStatusText = âunsuccessfulâ.
1 Like
Thank you, It worked.
Sorry while posting the full response I deleted a few lines.
I need vocalbilary test because I an a visual artist
My test is the count of each photo download form printing platform on each photo at gmail.com phone canma