Can I put a random int as a "default value" of a variable?

Hi all !
I 've a question. Can I put a random integer as a default value of a variable directly in a Web Service object?
Please, view the next screenshoot:
image

I put {{randomInt}} to represent the random integer, but obviously I dont know how it works and if it is possible

3 Likes

Sure give it a try.

I’ve already tried it. On this specific way: {{randomInt}} it doesn’t work. My question is, if there are any specific function o r something that works and return a random integer, directly in the Web Service object. Thanks anyway

1 Like

Random rand = new Random()
GlobalVariable.rnd = rand.nextInt(1000)

The above code will return a random value. please try to implement it into your test script.

  1. add a Global variable in your profile with name rnd and select type as int
  2. in your test case before your test steps add the shared 2 line code
  3. in the screenshot change the type to Global variable and in the Value select the variable rnd.
3 Likes

Hi,
In that case I think you need create CustomeKeyword to generate random integer, than you can passing parameter into your sessionID

2 Likes

You also will have to take into consideration that you are creating an Integer (or “int”) data type and trying to insert it into a String data type. You will get an error. You will need to cast the Integer (or “int”) to a String, like:

Random rand = new Random()
sessionId = rand.nextInt(1000).toString()
2 Likes

Yes. I was able to solve it the way you indicated. But I have a problem. I need to change hundreds test cases. For that reason I need a way to generate the random number in the Web Service Objetc, without needing to modify test cases. Thanks anyway!

1 Like

Of course. I appreciate your help :handshake:

1 Like

you designed your test suites wrong.
good luck with that, next time consider parametrization from the early stage.
and pass the random value from test case call, or whatever else value

Yes, I can see it. It was a mistake not take in consideration the parameterization of this value. Thanks anyway

1 Like

Could you make the type Test Data Value rather than String? The TDV could point to Excel file - a particular cell could read ‘=randbetween(0,1000)’? Ie let the Excel function generate a number.
If this doesn’t work, perhaps explain how the random number in the test object will be used?

1 Like

please use test listeners to define the same.
you can add the random number generation under @BeforeTestcase or @BeforeTestsuite listners

this will help you to do the execution before each and every test case gets triggered for execution.

since we have this feature you have to change the variables under every case you designed if you used local variables.

3 Likes

Thanks @bharathi.a, I finally managed to solve it!
I assigned the value of the Web Service as a parameter (type GlobalVariable). Next, I created a Test Listener where the value of globalVariable is simply modified to a random value. In this way, the global variable is modified before each test case. I really appreciate your help!
Regards

1 Like