Forum Discussion

ibright's avatar
ibright
New Contributor
7 months ago

Using a file location stored as a project property in a groovy set up script

I have created a test case that outputs the failed results to a file. I only want the file to be present in the folder if there are failed results to be viewed so I created a set up script that deletes the file at the start of each test run.

What I want to do is use the folder path set in the project property to avoid it being hardcoded in each test case in the project.

I used Get Data to get the project property and it automatically created this:

def aPIresultsfolder = context.expand( '${#Project#API results folder}' )

 

How do I get it to work with this part of the script that I had already set up:

new File(folderPath).eachFile (FileType.FILES) { file ->
//Delete file if file name contains Forward NDF errors
if (file.name.contains('Forward NDF errors')) file.delete()
}

This is the 

6 Replies

  • Hello ibright,

     


    ibright wrote:

    I used Get Data to get the project property and it automatically created this:

    def aPIresultsfolder = context.expand( '${#Project#API results folder}' )


    How do I get it to work with this part of the script that I had already set up:

    new File(folderPath).eachFile (FileType.FILES) { file ->
    //Delete file if file name contains Forward NDF errors
    if (file.name.contains('Forward NDF errors')) file.delete()
    }


     

    new File(aPIresultsfolder).eachFile (FileType.FILES) { file ->
    //Delete file if file name contains Forward NDF errors
    if (file.name.contains('Forward NDF errors')) file.delete()
    }

    • richie's avatar
      richie
      Community Hero

      Hey TNeuschwanger 

       

      I saw this post by ibright originally when he posted and didn't reply cos I wasn't sure about the answer.

       

      Part of why I didn't answer is cos I suck at coding, but also I was wondering - the 'aPIresultsfolder' variable is generated within a test case via a Get Data test step - so the variable is generated at test step so available at test case level.  If the script ibright is using is a 'Setup Script' on say the test suite or test case level - would the 'aPIresultsfolder' variable be picked up when the 'Setup Script' executes before the test suite, test case and relevant test step has fired?

       

      Thanks man TNeuschwanger - I'm asking for my education - it all helps, you know?

       

      Also - ibright - I'm not picking holes here - but if in future you published the whole script rather than excerpts - that can help tremendously resolve any queries the reader of the post might have.

       

      Cheers to both!

       

      Rich 

      • TNeuschwanger's avatar
        TNeuschwanger
        Champion Level 2

        Hello richie,

         

        Like you, I needed to make a few assumptions about the question...   Since it is ibright very first post, I though making some assumptions was perfectly ok.  🙂

         

        The 'Get Data' feature is a menu item available from the groovy script editor (Setup Script, TearDown Script, Groovy Test Step) by right clicking anywhere in the text editor area. I assumed that is where ibright acquired and defined the variable. Since the property expansion references a 'Project' level variable, it should be available whenever and wherever the line of code is executed.

           def aPIresultsfolder = context.expand( '${#Project#API results folder}' )


        ibright appears to want to use that variable very soon in their logic but might just need the syntax knowledge on how to use the value in the part of the script they already set up.

         

        So, richie, I think the short answer is yes. aPIresultsfolder is available for that project property expansion when requested. Good question for followup.


        Regards,

        Todd

         

         

  • ibright's avatar
    ibright
    New Contributor

    Thanks everyone for replying. I managed to solve it myself doing it this way:

    import groovy.io.FileType

    def aPIresultsfolder = context.expand( '${#Project#API results folder}' )
    String folderPath = aPIresultsfolder
    new File(folderPath).eachFile (FileType.FILES) { file ->
    if (file.name.contains('Forward NDF errors')) file.delete()
    }

    I changed the folderpath reference to refer to the defined field that ReadyAPI had generated automatically.