Generate random number in Soap UI using Java

This is community board for the soapUI Community. Here the members can exchange experiences and help each other improve their soapUI testing.

Generate random number in Soap UI using Java

Postby floctc » 28 Apr 2011 16:09

Hello,
I want to genere a random number inside an xml page opened in SoapUI, in order to test a web service.

I have tried this :
Code: Select all
<msisdn>079999${=(int)(Math.random()*9999)} </msisdn>


It works, but i need only 10 number digit (a phone number ^-^ )

I thought I could use some java methods to get this :
Code: Select all
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(4);
nf.setGroupingUsed(false);
System.out.println(nf.format((int)(Math.random()*9999)));


Or :
[code]
System.out.println(String.format("%04d", new Random().nextInt(9999)));
[code]

Unfortunatly it does'nt work, because Groovy doesn't know NumberFormat or Radom...

Have you any solution for my problem?

Thanks for your help :D
floctc
User
 
Posts: 2
Joined: 28 Apr 2011 15:54

Re: Generate random number in Soap UI using Java

Postby dschweie » 03 May 2011 06:20

Hi,

In a project I created with soapUI it was looking for a possibility to bring functionality to library. In the Open Source version of soapUI you create jars and you have to store those in folder <installation directory>/bin/ext/.

Maybe you can use this either. You just code you function in Java and create a jar-file. This can be used by Groovy script very easily.

To ensure a special number of digits I would recommend to use datatype String. In XML-document you will not have trouble with it.

This solution might not be the most efficient but it will work.

Best Regards
Dirk
dschweie
User
 
Posts: 3
Joined: 14 Apr 2011 07:42
Location: Germany

Re: Generate random number in Soap UI using Java

Postby Finan » 03 May 2011 23:27

Hi,

If you want to create a random number with 10 digits, use the following groovy script:
Code: Select all
int a = 9
nr = ""
for(i = 0; i < 10; i++)
{
   random = new Random()
   randomInteger= random.nextInt(a)
   nr = nr + randomInteger
}
log.info nr

If you want to limit the value of a digit on a specific spot (for instance, the first digit is always a zero) you can use nr.lenght() to assert the digit-position:
Code: Select all
int a = 9
nr = ""
for(i = 0; i < 10; i++)
{
   if(nr.length() < 1)
   {
      nr = nr + "0"
   }
   else
   {
      random = new Random()
      randomInteger= random.nextInt(a)
      nr = nr + randomInteger
   }
}
log.info nr


Of course you still need to set the value of nr to the correct parameter in your XML...
Finan
Wizard
Wizard
 
Posts: 126
Joined: 06 Dec 2010 16:18

Re: Generate random number in Soap UI using Java

Postby floctc » 04 May 2011 10:18

Thanks for your help, it works better than an other solution I have found later :
Code: Select all
<msisdn>079999${=((int)Math.random()*(9999-1000)+1000)}</msisdn>


The post is solved.
floctc
User
 
Posts: 2
Joined: 28 Apr 2011 15:54

Re: Generate random number in Soap UI using Java

Postby coexmatrix » 16 Mar 2012 19:20

This solution works great. I set the random number to stop at 5 digits:
int a = 9
nr = ""
for(i = 0; i < 5; i++)
{
random = new Random()
randomInteger= random.nextInt(a)
nr = nr + randomInteger
}
log.info nr

However, how would I make it so that the number generated couldn't go higher than say 50000?
coexmatrix
User
 
Posts: 9
Joined: 14 Jan 2011 02:05

Re: Generate random number in Soap UI using Java

Postby smartTest » 03 Apr 2012 11:12

Hiya

Don't know if it would help but I am using a much shorter random number string that will write to a property.

//Generate TransactionID
testRunner.testCase.setPropertyValue("TransID", String.valueOf((int)Math.random()*1000000000) );

This gives me a random 9 digit number between 000000001 & 999999999.

Di
;D
smartTest
User
 
Posts: 20
Joined: 19 Aug 2011 11:05


Return to soapUI Community Board