Forum Discussion

mu123's avatar
mu123
Occasional Contributor
2 years ago
Solved

How to access the variable defined in the setup script from a groovy script

I have a variable creationTime (a field to represent data and time) defined in the setup script of my project. I want to access this variable from a groovy script.

 

This is what I wrote in the groovy script. 

a = testRunner.testCase.testSuite.getPropertyValue('${creationTime}')
log.info(a)

 

This returns a null value. Please let me know where am I making the mistake.

  • Hi,

     

    Something set in context is accessible in further steps, providing you running the whole 'thing', which is the project in your case.

     

    If you are running the test suites/cases within the project one at a time, then the original context is lost and the context only contains that of the running test suite or test case.

     

    This is why the Groovy is probably not working because if you just run that Groovy step on it's own, the context is created for that step.

     

    For example...

     

    This test has a setup script to create a context var called "setupScriptProperty".  I then have a Groovy script (called Set Value in Context) that writes another var to the context.  This one is called myVar.

     

    Here is the script...

    context.setProperty("myVar", "my value");
    
    log.info(context.getProperty("myVar"));
    

     

    I then have a second script that attempts to log the var created in setup and the second one from the first groovy script.  This script is called "Get Value from Context - Groovy Script" and here are the contents of that script...

    def quickCheck = context.getProperty("myVar");
    log.info(quickCheck);
    
    log.info(context.getProperty("setupScriptProperty"));

     

    If I run this test case as a whole, then everything is logged and no null values, because everything was run under the same context....

    Now, if I run my "Get Value from Context - Groovy Script" step on its own, the context is reset....

     

     

     

5 Replies

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

     

    Something set in context is accessible in further steps, providing you running the whole 'thing', which is the project in your case.

     

    If you are running the test suites/cases within the project one at a time, then the original context is lost and the context only contains that of the running test suite or test case.

     

    This is why the Groovy is probably not working because if you just run that Groovy step on it's own, the context is created for that step.

     

    For example...

     

    This test has a setup script to create a context var called "setupScriptProperty".  I then have a Groovy script (called Set Value in Context) that writes another var to the context.  This one is called myVar.

     

    Here is the script...

    context.setProperty("myVar", "my value");
    
    log.info(context.getProperty("myVar"));
    

     

    I then have a second script that attempts to log the var created in setup and the second one from the first groovy script.  This script is called "Get Value from Context - Groovy Script" and here are the contents of that script...

    def quickCheck = context.getProperty("myVar");
    log.info(quickCheck);
    
    log.info(context.getProperty("setupScriptProperty"));

     

    If I run this test case as a whole, then everything is logged and no null values, because everything was run under the same context....

    Now, if I run my "Get Value from Context - Groovy Script" step on its own, the context is reset....

     

     

     

    • mu123's avatar
      mu123
      Occasional Contributor

      Thank you for the detailed explanation

  • KarelHusa's avatar
    KarelHusa
    Champion Level 3

    mu123,

    if you need to use the creationTime property in different test suites and test cases, define it as a project-level property:

     

    project.setPropertyValue("creationTime", todayDate + todayTime) 

    Then you can use it anywhere by:

     

    project.getPropertyValue("creationTime")

    or in the requests, mock etc. by:

     

    ${#Project#creationTime}

     

    Best regards,

    Karel

     

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

    You're almost there, try....

    a = testRunner.testCase.testSuite.getPropertyValue('creationTime')

    This assumes you have created a custom property at the test suite level.  E.g.

     

    It's also worth noting that these customer property values are of the type String.  So, if you put a number in, ReadyAPI will still think it a String.  You will need to cast to in Int to use.

  • mu123's avatar
    mu123
    Occasional Contributor

    Thanks for the response Chris. Using the script mentioned above returns a null value from the groovy.

    ------------------------------

    This is how creationTime is defined in the setupscript.

    def today = new Date()
    def todayDate = String.format('%tY%<tm%<td', today)
    def todayTime = String.format('%tH%<tM%<tS',today)
    context.setProperty("creationTime", todayDate + todayTime)