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

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

  • 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 

      • ibright's avatar
        ibright
        New Contributor

        Sorry, I was wary of including the folder directory that had been hard coded into the script.