Forum Discussion

mu123's avatar
mu123
Occasional Contributor
3 years ago

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 gro...
  • ChrisAdams's avatar
    3 years ago

    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....