Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How to read the values/content from FILE datasource

How to read the values/content from FILE datasource and use in Response tab groovy scripting?
I was able to successfully read the values from EXCEL datasource using the below step in Response tab Groovy script.
variable = context.getValues("data source name", "column name")

I tried to use the below steps to read the values from FILE datasource but it did not work.
variable = context.getValues("data source name", "File Name")
and
variable = context.getValues("data source name", "${File Path}")
and
variable = context.getValues("${File Path}")

Please suggest me, how to read the values from File Datasource.

Tagged:

Comments

  • jakubiak
    jakubiak Posts: 795 admin

    What do you mean by "it didn't work"? Did you get an error message? Empty value?

  • When I use context.getValue("${File Path}") I got "null" response.
    When I try with context.getValues("${File Path}"), I got -- Error occurred when processing a request to /myPath, check data source settings, any external file reference and configurations for possible problems.

  • Is there any reference documentation or examples on how to use the File data source.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    Those methods should work. I am able to reproduce the issue, for a table data source getValue returns the value from the column but the File Data Source does not. I was able to get "getValues" to work with the File Data Source for the name and path but that is returning an array. In the "getValues" case I used the name of columns without "${" or "}" to get the array of all values.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    I did some more testing, looks like I just had something misconfigured. I am able to get the values from the data source columns. I am attaching an example that can be unzipped into the "Virtual Assets" project.

    Then just send it a request using the following url
    http://localhost:9080/fileDataSource?file=file1.txt

    You should see a response with the file's contents.

  • Thank you so much for the details. Is there way I can read the files without passing the URL parameters? Like how Excel data source is allowing to read the columns.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    In your excel case you are not using a data source correlation? The URL parameter is only there to act as the data source correlation to set the specific row.

  • No, I am not using the data source correlation for excel data source. In my requirement I don't have the file names to pass it in the request. I wil be only sending dates in my request.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Could you share the groovy script you are using that works with excel? I am not sure why it would work for one data source type but not another. If you could attach the script that would be very helpful.

  • Here is the script that I am using to read the excel data source.

    def readData(context) {
    date = context.getValues("myexcel","Date")
    date1=context.getValues("myexcel","Date1")
    return date + date1
    }

    In the excel,
    A1 = Date,
    B1 = Date1,
    A2 = abc,
    B2 = xyz

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    What data source is your responder configured to use? When I configure the response to use the File Data Source without correlations I am able to use "getValues" to get the column data.

  • It worked now. Thank you so much for supporting. I still checking why my script did not work before.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Glad it worked!

  • Is there a way I can read the data from the particular file?
    If I use context.getValues("datasource","File Contents"), it will read all the file contents. I can use .get(0) to read the first file and so on. But I want to read the file based on the file name. How to achieve that.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    I think you can determine the index based on the file name. I don't know of a method that could resolve the file name to the contents automatically.
    Since you are scripting this could your script just read from the file directly? Without using the file data source?

  • I tried to read the file directly without using the data source but it returns null.
    context.getValue("fileName")

    How to determine the index based on the file name?

    Is there any method which will take the file name and get the contents, something like
    context.getValue("File Contents").getFileName("fileName")

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Here is an example of finding the index based on the name. The context.getValue api is general because it has to support many different types of data sources. The methods that more directly work with the file data source are not api and subject to change in future releases.

    Let me know if this helps.

  • That helped me a lot. Thank you so much.