Forum Discussion

richw's avatar
richw
New Contributor
16 years ago

setting and retrieving variables within each thread

Hi, I'm trying to create a groovy script that will do the following inside a testcase thread.

Retrieve a variable set by this code:
Examine the variable and if null set to the current system time
  if not null retrieve the current time and calculate the passage of time between the two times
If the passage of time is less than a predefined period then wait for that difference.

The main issue I think I'm having is to get the variables to persist within a test case loop.

Here's my code ... any help would be appreciated as this is my first attempt at Groovy script and I don't have a Java background.

// check if time is set

long startTime = context.getProperty( "startTime" )

if( startTime == null )
  {
  long startTime = System.nanoTime()
  context.setProperty( "startTime", startTime )
  }

long thisClock = System.nanoTime()
long timePassed = thisClock - startTime

// SET THIS TO DESIRED DELAY IN NANOSECONDS i.e. 999,999,999 +1 = 1 second
desiredDelay=333333333
//
// if delay hasn't happened then wait until delay up
//

if( timePassed < desiredDelay )
  {
  long sleepTime=(desiredDelay-timePassed)/1000000
  Thread.sleep( sleepTime )
  }
context.setProperty( "startTime", thisClock )

2 Replies

  • sinnes_1's avatar
    sinnes_1
    Occasional Contributor
    I believe you can do this with a "Properties" step.

    Set one up called "Properties"

    At the start of your script:

    def properties = testRunner.testCase.getTestStepByName("Properties");
    long startTime = properties .getPropertyValue( "startTime" );

    .... code


    properties.setPropertyValue("startTime", starTime.toString());

    If you leave the actual property blank, it will come up as null the first time around.

    Hope that helps!