1
votes

I have been trying various ways to send a random value as part of payload of Rest testcase. I also need to assert that random value in the returned response.

I am able to add a random value to the payload. But I am not able to assert that random value in the response.

I have tried the following ways.

  1. Add a groovy script and set a global variable.

    def randomValue = java.util.UUID.randomUUID() com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "randomValue", randomValue )

Then use it in the payload as ${randomValue} This way I am able to access it in both request and assertion. This goes well with one request. When Load test is executed with lot of concurrent requests, the requests are having same value instead of random values.

  1. Set property using Properties test step.

    ${=(int)(Math.random()*100)

    This is proving a random value to the request. But in assertion the random value is new value. It is not the same as that in the request.

Any help is greatly appreciated.

2

2 Answers

1
votes

You are on are on the right track.

Set the random value from the Test Setup script, something like:

def randomValue // = whatever you like: UUID.randomUUID() or Math.random() work fine
testCase.setPropertyValue( "randomValue", randomValue.toString() )

This will generate a random value once for each test run. Then anywhere from your test, you refer to this with property expansion as ${#TestCase#randomValue}.

1
votes

May be you need to parse and build your request

  1. Use JsonSlurper or xmlSlurper to parse request
  2. Insert random value in request

3.Build Json/xml again using JsonBuilder/xmlBuilder

  def builder = new JsonBuilder(your_changed_json)
  def json = builder.toPrettyString()
  json = groovy.json.StringEscapeUtils.unescapeJava(json)
  1. set the above json/xml to your request

    step.testRequest.setRequestContent(json)