Forum Discussion

electric_Insura's avatar
electric_Insura
Contributor
14 years ago

[Resolved]Newb needs a primer in property handling w/Groovy

Hi all,

Need some help getting started with Groovy and properties. I read the user guide and some other posts but that didn't get me very far.

My test case contains:

1) A Property step with a property named "randomValue"
2) A Groovy Script step with the following code:


testRunner.testCase.getPropertyCount()


The Groovy script returns a count of 0.

Why isn't it seeing the property from the Property step? Do I need some sort of transfer?

Ultimately I'm trying to do something like this:


///pseudocode follows
[define a property named randomValue]
def randomNumber = 100000000 + (int)(Math.random()*900000000)
randomValue.value = randomNumber


I tried this using code from some other posts, but I get an error that randomValue isn't defined.
  • Finan's avatar
    Finan
    Frequent Contributor
    First example:
    switch
    testRunner.testCase.getPropertyCount()

    to
    testRunner.testCase.testSteps["properties"].getPropertyCount()

    In your example, you were counting the testCase properties, i.e. the properties on the testCase level, instead of the properties on testStep level.

    Second example:
    If you want to set the value of the property "randomValue" (from testStep "properties") to a random value, you should do the following:
    testRunner.testCase.testSteps["Properties"].setPropertyValue("randomValue", (100000000 + (int)(Math.random()*900000000)).toString());
  • Finan,

    Thanks for the response.

    While I was setting up the test case, I discovered that I could output the result of the Groovy script to the Web request without any need to set properties. I went this route instead, simply generating a random 9-digit number in the script.

    I'm sure I'll be able to use the properties information you provided at some other time, though...!