0

I'm currently writing a post request witha a JSON body. i'm wanting my JSON to produce unique data on each iteration of test plan, but due to dependancies, this is leading to my requests sending invalid data.

for example, one of the parameters is DocumentID. I have written a ${chooseRandom()} function to select the documentID:

${__chooseRandom("5ea1a4116f1987335809ae0d","5ea1a4d36f1987335809ae0e","5ea29645f5082539f8d430cd","5ea29f6df5082539f8d430db","5ea29fa4f5082539f8d430dc",companyDocumentID)}

so the selected value is stored in the variable "companyDocumentID".

the next parameter is ReferenceFieldValue. this is an id that is associated to the companyDocumentID, and based on the selection of companyDocumentID in the prior chooseRandom function, the referenceFieldValue needs to match the correct ID.

for example, if the first choose random option is selected, then ReferenceFieldValue needs to be "1", if the second choose random option is selected, then ReferenceFieldValue needs to be "2" and so on for all 5 options.

i have tried to use an if function to capture the correct ReferenceFieldValue, but as the false value, i have nested a further if statement:

${__if(${companyDocumentID}, "5ea1a4116f1987335809ae0d", "1", ${__if(${companyDocumentID}, "5ea1a4d36f1987335809ae0e", "2", ${__if(${companyDocumentID}, "5ea29645f5082539f8d430cd", "3", ${__if(${companyDocumentID}, "5ea29f6df5082539f8d430db", "4", ${__if(${companyDocumentID}, "5ea29fa4f5082539f8d430dc", "5", "6")})})})})}

but when i test my request, ReferenceFieldValue is returning as "6", meaning companyDocumentID is not equal to any of the possible values, which isn't the case, so i'm confused as to why this is happening.

I have three questions from this:

  1. why is ReferenceFieldValue returning "6", when i have covered all of the possible values of companyDocumentID?
  2. is there an easier way to achieve my desired outcome?
  3. can you nest if functions to act as an else/elseif?

here is the fulle JSOn body to help give context:

{
    "projectId": "${projectID}",
    "classId": "${classID}",
    "data": {
        "90e7a6df-3c47-3f09-61cd-d37720e5103a": {
            "documentId": ${__chooseRandom("5ea1a4116f1987335809ae0d","5ea1a4d36f1987335809ae0e","5ea29645f5082539f8d430cd","5ea29f6df5082539f8d430db","5ea29fa4f5082539f8d430dc",companyDocumentID)},
            "referenceFieldValue": ${__if(${companyDocumentID}, "5ea1a4116f1987335809ae0d", "1", ${__if(${companyDocumentID}, "5ea1a4d36f1987335809ae0e", "2", ${__if(${companyDocumentID}, "5ea29645f5082539f8d430cd", "3", ${__if(${companyDocumentID}, "5ea29f6df5082539f8d430db", "4", ${__if(${companyDocumentID}, "5ea29fa4f5082539f8d430dc", "5", "6")})})})})}
        },
        "fc06d039-b3e8-988a-5402-88666acd10e5": "${__UUID()}",
        "fffef986-9c12-8ca4-2d22-3a7e8abd08f7": "${__UUID()}",
        "44b9a8e0-d933-047c-8659-cb4f1672cddc": "${__UUID()}@example.com"
    }
}

1 Answer 1

1
  1. You're getting 6 because your syntax is not correct, you have extra spaces in your comparison operators

    ${__if(${companyDocumentID}, "5ea1a4116f1987335809ae0d",
                                ^---here
    
  2. Not sure regarding "easier" but in my opinion it would be way more readable if you switch to __groovy() function and implement your logic in Groovy language
  3. Yes

Just in case, correct __if() function syntax:

${__if(${companyDocumentID},"5ea1a4116f1987335809ae0d","1",${__if(${companyDocumentID},"5ea1a4d36f1987335809ae0e","2",${__if(${companyDocumentID},"5ea29645f5082539f8d430cd","3",${__if(${companyDocumentID},"5ea29f6df5082539f8d430db","4",${__if(${companyDocumentID},"5ea29fa4f5082539f8d430dc","5","6")})})})})}

And demo:

enter image description here

1
  • Thank you for correcting my if function, this has resolved my issue! i was following the example on jmeter-plugins.org/wiki/Functions which has spaces in the comparison. I will look to use groovy functions in the future.
    – coltest123
    Commented Apr 28, 2020 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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