Forum Discussion

Jon's avatar
Jon
Contributor
16 years ago

Groovy Datasource

Hi!

Trying to use a groovy datasource to generate some test values (to eliminate a spreadsheet containing just a column of numbers).

I need to iterate over that column, changing the values each time an outer loop increments.

What I have is this: a groovy datasource with a single property defined.  NExt, I have this script which takes two TestCase properties as the base from which to compute the list of values:

def numNetPortsPerTSStr = context.expand( '${#TestCase#numNetPortsPerTS}' )
def startNetPortStr = context.expand( '${#TestCase#startNetPort}' )

def numNetPortsPerTS = Integer.parseInt(numNetPortsPerTSStr)
def startNetPort = Integer.parseInt(startNetPortStr)

def i = 0

while (i < numNetPortsPerTS)
{
result.netPort = startNetPort + i
i++
}


The startNetPort is set to 1000 and numNetPortsPerTS is set to 10.

However, running this only gives me the same value (1009).  As well, trying to access the property, like so:

def netPortStr = context.expand( '${Net Port Datasource#netPort}' )
println "netPortStr:"+netPortStr


from another test step (groovy script) results in:

Mon Jun 02 13:53:51 EDT 2008:ERROR:java.lang.NumberFormatException: For input string: ""

The notes for the groovy datasource didn't help me out much so I'm hoping you can shed some light.

Cheers!

6 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    I'm not sure if you really need a Groovy DataSource for this, how about the following:

    1) an initial Script that writes the first value to the context
    2-X) any numbers of steps that use the value
    last) a script that increases the value in the context and loops back to 2 if the limit hasn't been reached

    would that work?

    regards,

    /Ole
    eviware.com
  • Jon's avatar
    Jon
    Contributor
    Hey Ole,

    That's a great idea, one which I'd thought of using at one point, but I couldn't figure out how to go back to a given step.  The conditional goto uses xpath and the datasource loop requires a datasource to increment.

    Cheers!
    Jon
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Jon,

    you can use

    testRunner.gotoStepByName( ... )

    which will transfer execution to the named step when the script finishes (not at once!)

    regards,

    /Ole
    eviware.com
  • Jon's avatar
    Jon
    Contributor
    Cool!  I will give that a try, thanks!
  • Jon's avatar
    Jon
    Contributor
    Hey Ole,

    Just wanted to say thank you... these suggestions worked out quite well in the end.  Nice to not have to use an external file for everything. 

    Cheers!
  • omatzura's avatar
    omatzura
    Super Contributor
    Great! Thanks for following up!

    regards,

    /Ole
    eviware.com