Home > Software design >  How to use script generated UUID in REST Request
How to use script generated UUID in REST Request

Time:04-14

I need to test a scenario where I need to generate a random UUID in order to use it lately as a part of a json body of a POST request. My steps based on what I understand from the documentation are the following:

  1. UUID Generation

enter image description here

  1. A property declaration like so

enter image description here

  1. Than a property transfer setted up in this way:

enter image description here

Now at this point if i run my step from the GenerateGUID to the property transfer i can see that the value has been succesfully transfered.

enter image description here

Now I need to use that property inside my Json like so:

enter image description here

I tried different solutions picked up around internet but with no luck, I'm new to SoapUI and this task has been assigned to me. Please can anyone spot what I'm missing or can point me the right direction?

CodePudding user response:

One thing that IMHO SoapUI documentation does not make clear, is that there are several different ways of accomplishing the same thing. Some more or less efficient. In your case I see two options.

=> Option 1

If you need to use the value in only one place, that is, only in the payload but you do not need it later in something like an assertion. In this case you can use inline dynamic property.

In your "CalculateObj Test" step, right in your payload, enter the following:

{ "guid": "${=java.util.UUID.randomUUID()}", ...other fields }

Every time you run this, a different UUID will be generated. Of course, you no longer need your three preceding steps.

=> Option 2

If you need to use the same UUID in multiple places, but you still need a different UUID each time you run the test. Here you probably want to store the value as a testcase property and use simple property expansion.

Change your "Change GUID" script step to the following:

def Guuid = java.util.UUID.randomUUID()
testRunner.testCase.setPropertyValue('guuid', Guuid)

Then in your "CalculateObj Test" step payload, enter the following:

{ "guid": "${#TestCase#guuid}", ...other fields }

Subsequently, anywhere else in your testcase that you need to use this value, for example in an assertion, you can always refer to it as ${#TestCase#guuid}. But each time you run the test, the value will be different.

I almost never use the Properties or the Property Transfer steps.

  • Related