Undefined variable

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