Forum Discussion

WillJones's avatar
WillJones
New Contributor
8 years ago
Solved

access numeric context property in groovy script

How do I access a numeric property I have set in the context. 

I have the properties set on the test case like this...

testCrimeNo    12456W/02

testAddressId   -2

 

I set a property in my test setup script like this...

 

context.setProperty("TEST_CRIME_NO", testRunner.testCase.getPropertyValue("testCrimeNo"))
context.setProperty("TEST_ADD_ID", testRunner.testCase.getPropertyValue("testAddressId"))

I then access my properties in a groovy script test step like this...

 

log.info(context.expand('${#TEST_CRIME_NO}'))
log.info(context.expand(${#TEST_ADD_ID})

I then run the test case and I want the first variable TEST_CRIME_NO as a string which works ok (on its own) but the TEST_ADD_ID I want as an integer and I get a compilation error, unexpected token : #

 

I can get around this by doing this but is there a better way..

 

log.info(context.expand('${#TEST_ADD_ID}').toInteger())

Am I doing something wrong. Thanks in advance for any help.

  • Hi,

     

    What you have done is not wrong, I can only really give tips on shorter syntax. Basically

     

    1) Object properties e.g. Project, TestSuite, TestCase etc are only String properties

    2) Whereas, the context map, can take any object type

     

    So, perhaps just as a shorter version you could try

     

    context["TEST_ADD_ID"]=Integer.parseInt(context.expand('${#TestCase#testAddressId}'))

     

    Then 

     

    log.info context["TEST_ADD_ID"]

     

    Note also:

    context["TEST_ADD_ID"] is the same as context.TEST_ADD_ID

    and

    context.expand('${#TestCase#testAddressId}') is the same as testRunner.testCase.getPropertyValue("testAddressId")

     

    Does this help?

     

    Regards,

    Rupert

     

1 Reply

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    What you have done is not wrong, I can only really give tips on shorter syntax. Basically

     

    1) Object properties e.g. Project, TestSuite, TestCase etc are only String properties

    2) Whereas, the context map, can take any object type

     

    So, perhaps just as a shorter version you could try

     

    context["TEST_ADD_ID"]=Integer.parseInt(context.expand('${#TestCase#testAddressId}'))

     

    Then 

     

    log.info context["TEST_ADD_ID"]

     

    Note also:

    context["TEST_ADD_ID"] is the same as context.TEST_ADD_ID

    and

    context.expand('${#TestCase#testAddressId}') is the same as testRunner.testCase.getPropertyValue("testAddressId")

     

    Does this help?

     

    Regards,

    Rupert