How do you insert the same random variable into multiple soapui testcase requests?

user83598 picture user83598 · Sep 10, 2012 · Viewed 7.3k times · Source

I may be going about this in the completely wrong way, but how do I pass a dynamic variable to a bunch of requests within the same testsuite in SoapUI?

My first test step is a Groovy script. I need to generate a random account name, and then use it in all my other requests. There are about 20 other requests. I initially thought I could just loop the testsuite, but it is not working.

This is my groovy script at the beginning:

Random random = new Random()

def randUserAccount = "testAccount" 

int max = 100000
randnum = random.nextInt(max+10000)

randUserAccount += randnum

log.info "     Creating account: $randUserAccount"

Then in each request step, I have things like this:

<ns:CreateAccountRequest>
    <accountID>${randUserAccount}</accountID>
...

or

<ns:PurchaseRequest>
    <accountID>${randUserAccount}</accountID>
...

The account is null when I actually send it, and of course that gives errors on the server side. How do I really get the variable to persist across all the requests in the testsuite?

Thanks in advance for any hints!

Answer

chrismead picture chrismead · Sep 10, 2012

You can use the context, I believe. You can definitely use it between requests in a test, but I also think it will work between tests in a suite.

context.setProperty("randUserAccount", randUserAccount)

Then use the syntax you specified in the actual requests.

Let me know if this doesn't work. You can also use 'properties' to do this, but it is a little more work.