Forum Discussion

AAB's avatar
AAB
Regular Contributor
6 years ago

ReadyAPI: relative path isn't recognized

Hi all,

 

In my groovyscript I wanted to insert a relative path so that all users that wants to use my code and files, can do so by saving the code and files wherever they want. That's the purpose of relative path. 

In ReadyAPI you can use something like 

// Get a test case property
def testCaseProperty = testRunner.testCase.getPropertyValue("MyProp")

but you can also do this:

 

def path = context.expand ( '${#project#projectDir}')

 

Is that correct? Because none of the both works for me.

1) I've put the path on ProjectLevel in 'Custom Properties'. 

2) I've added a groovyscript where the path needs to be fetched multiple times by declaring the variable [ def path ]

3) I get the error message  path\countries\ResultsSTAT.txt (The sytem cannot find the path specified).

 

Could someone help me please? Thanks

  • Oops, I found the solution... Stupid actually....

    The creation of the paths were correct, but when I wanted to use it in my code ,so actually call them, ReadyAPI threw me the error. Look at my first screenshot:

    1) ( '${#project#projectDir}' ): I wrote Project with a small p, should have been a capital

    2) File resultPrimary = new File ("path/Countries/ResultsSTAT.txt")  => the path declaration is wrong. Here, Groovy is a Java language, and Java doesn't concatenate paths!!!! therefore you need to 'use' the variable as a variable... :-) so like this:

     

    File resultPrimary = new File(path + "/Countries/ResultsSTAT.txt")

     

    And now it works!  :-) 

     

    richie Thanks for your time and explanation man! Appriciated it.  ;-) 

  • richie's avatar
    richie
    Community Hero

    Hey AAB 

     

    I've had a quick look and I'm probably not the best person to fix this unless I re-create your whole project and scripts and work through it that way  - but I have a couple of comments that might help.

     

    1.  I'd say quite often people have problems because ReadyAPI! is looking for backslashes (e.g. '\') in the path rather than forward slashes (e.g. '/') when using both absolute and relative paths.

     

    If you look at the Datasource teststep item if you select any of the datasource types that source a file (e.g. directory, file, excel) - it dynamically generates a path with backslashes.

     

    The error response dialogue in your 2nd image file actually displays the path with backslashes

     

    If you look at a project's 'Project Properties', you will notice the 'File' attribute (which is the full path to the project file uses backslashes).

     

    Despite the above - I am aware that  groovy scripting appears to support only forward slashes (e.g. '/') - or I just had a quick check and a path wasn't recognised as path with backslashes (e.g. '\')

     

    You've specified your projectDir property as one of your Project's custom properties.  The Project Property tab includes a Resource Root attribute (see link) that typically (if specified) has 2 options - the {projectDir} property and the {workspaceDir} property.  Are you trying to recreate something that's already embedded within ReadyAPI!'s native functionality?  That's a question - I don't know the answer - I've never used this property before.

     

    Ok - just done a bit more reading - I found here

     

    If the project directory is D:\projects\webservice\ (where the project file is resides).

    The absolute path to the file is D:\data\testdata.xls.

    To use the file path relative to the project directory, use ${projectDir}\data. For example:

    D:\projects\webservice\data\testdata.xls

    To use the file path relative to the directory that contains the workspace file, use ${workspaceDir}\data. For example:

    D:\projects\data\testdata.xls

    So follow the above to make sure you have everything located in the correct directories.

     

    The path you specify as the projectDir property uses forward slashes (e.g. C:/Users/CDRBK89/Documents/Bosa/Put Data at the Center/Automation/WORKSPACE-PDC/Concepts) - I'm always dubious about including paths that 1. are too long (which is quite likely if you're using a path within your windows user's profile) and 2. includes spaces or any special characters.  I've had problems getting a groovyscript to run that appeared perfect, but simply didn't work until there were zero spaces, special chars and I reduced the length.  Other times I have had paths that included these two points and there wasn't a problem.  It was always too much hassle cos I never knew if it was going to work - so I just take it as a rule now - short paths/no spaces or special chars.

     

    Finally - you provided a screenshot of some of your groovy when you get the error dialogue - I'd drop in a couple of logging commands in there to just verify what variables you are specifying is correct.

     

    you've got 'def path = context.expand ( '${#project#projectDir}')' in your script and you say it isn't working?  try capitalising the #Project# scope so rather than '${#project#projectDir}' instead it's '${#Project#projectDir}'

     

    you need the above working so it resolves otherwise it's not going to work - after that I'd also add some logging in where you declare the resultPrimary and resultTarget variables.  It seems to be at this point where it's not finding the path correctly

     

    As I said I'm not the best placed person to fix this without reproducing your whole project - but the above is the sort of thing I'd go through to try and identify and resolve the problem.

     

    Hope this helps........Unsure it will! :)

     

    rich

     

     

  • AAB's avatar
    AAB
    Regular Contributor

    Oops, I found the solution... Stupid actually....

    The creation of the paths were correct, but when I wanted to use it in my code ,so actually call them, ReadyAPI threw me the error. Look at my first screenshot:

    1) ( '${#project#projectDir}' ): I wrote Project with a small p, should have been a capital

    2) File resultPrimary = new File ("path/Countries/ResultsSTAT.txt")  => the path declaration is wrong. Here, Groovy is a Java language, and Java doesn't concatenate paths!!!! therefore you need to 'use' the variable as a variable... :-) so like this:

     

    File resultPrimary = new File(path + "/Countries/ResultsSTAT.txt")

     

    And now it works!  :-) 

     

    richie Thanks for your time and explanation man! Appriciated it.  ;-)