Forum Discussion

AnandKiran's avatar
AnandKiran
Frequent Contributor
16 years ago

Want to set folder path as global property

Hi Ole,
              I want to set a folder path in the Global Property and want to use it in my groovy script .
I tried defining a property called PATH with value D:/Gallery/Anand/Input

I tried to access this property using ${Global#PATH} in the script but i am getting an error.

Let me explain the script ,

Workbook Output = Workbook.getWorkbook(new File("D:/${Global#PATH}/Output.xls"))


In the above script instead of mentioning the complete path i want to access the values saved as global variable .  But when i executed the script it throws an error



Error :

Mon Mar 09 11:45:56 GMT+05:30 2009:ERROR:An error occured [startup failed, Script34.groovy: 27: unexpected char: '#' @ line 27, column 60.org.codehaus.groovy.syntax.SyntaxException: unexpected char: '#' @ line 27, column 60.

When i tried :

Workbook Output = Workbook.getWorkbook(new File("D:{#PATH}/Output.xls"))

I got this error :

ERROR:An error occured [No such property: PATH for class: Script35], see error log for details

Can you help me in this issue

Thanks in advance
Anand Kiran

5 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi Anand,


    When accessing properties from Groovy scripts you should use the context object to macro-expand the property values in run-time. Using context object you can access properties from different contexts, incl. Global, that you're after.

    If your PATH global variable does not include the partition letter, here's what you should have:

    def excelFilename = context.expand('D:/${#Global#PATH}/Output.xls');
    Workbook Output = Workbook.getWorkbook(new File(excelFilename))


    Not the prefixed hash symbol (#) before the context name. Properties defined inside a test step do not need this prefix. Also, you should use single quotes and not double quotes since they would assign the string value as is which is without property macro-expansion you need.

    There's also documentation on property expansion in soapUI that you might want to peruse.

    I would also recommend using log object to learn more about your Groovy scripts and object values. For example:

    log.info("excelFilename = " + excelFilename);


    I hope this will solve your problem.


    Cheers!
    /Nenad Nikolic a.k.a. Shonzilla
    • SaurabhT's avatar
      SaurabhT
      New Contributor

      Hi,

       

      This solution is very useful.

       

      Thanks.

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    sure;

    log.info testRunner.testCase.testSuite.project.path

    (if you are in a Groovy TestStep)

    regards!

    /Ole
    eviware.com
  • AnandKiran's avatar
    AnandKiran
    Frequent Contributor
    Hi,
            That was great . It was a very useful information you have shared with me . Thanks a lot


    Cheers !!!!!

    Anand Kiran
  • gurpreets's avatar
    gurpreets
    Occasional Contributor
    Is there any way.. to get the project file location.. ?? (using groovy script ofcourse..)