Forum Discussion

Taylor_Pack's avatar
Taylor_Pack
Occasional Contributor
15 years ago

Access datasource with groovy

I can access a property from groovy but I seem to be missing the point on how to do the same thing with a datasource.

If my test has:

myDataSource
myGroovyScript

And the datasource has: field1, field2 field3

wouldn't the following print field3:

def ds = testRunner.testCase.testSteps['myDataSource'];
log.info "$a = " + ds.getPropertyValue("field3")


I have looked around a bunch but where would be the best place to find out more information like this?
  • Taylor_Pack's avatar
    Taylor_Pack
    Occasional Contributor
    I figured it out. I took too much code from a previous example. I shouldn't have included the "$a = " + from the code I was stealing.
  • Taylor_Pack's avatar
    Taylor_Pack
    Occasional Contributor
    Well, I am not quite there. I have access to the datasource but now when I try to set the property it tells me it is read only. How do I reset the value?

    Here is the code I am using.

    def remitCSV = testRunner.testCase.testSteps['remittanceCSV'];
    def date = remitCSV.getProperty("Remittance Date").getValue()

    java.text.SimpleDateFormat dateNew = new java.text.SimpleDateFormat("MM/dd/yyyy");

    date = dateNew.parse(date)

    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
    date = sdf.format(date)

    remitCSV.setPropertyValue("Remittance Date", date)
  • All the values from a DataSource are read only. If for some reason, you want to change the value, we recommend copying the property value, and changing the copy.

    /Nenad
    http://eviware.com
  • Taylor_Pack's avatar
    Taylor_Pack
    Occasional Contributor
    Interesting. I have a data source with 2000 rows imported from a CSV file but the date is in the wrong format for my request. I was writing a groovy script to reformat it into the correct format. My plan was to just write it back to the datasource but I guess I can't do that. The next teststep with loop through the datasource. So if I am to write the reformatted date to a new properties table how do I do that? If I create a properties step there is no method, that I can find, to add a property to it. What is the best way to do this?
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    You can access a properties step like this:

    [tt:pjgh5d1f]testRunner.testCase.getTestStepByName('Properties').setPropertyValue("MyProperty","123")[/tt:pjgh5d1f]

    If the property does not exist it will be created, else overwritten.

    Do you need to convert all the rows at once?
  • Taylor_Pack's avatar
    Taylor_Pack
    Occasional Contributor
    That is some good information. Thanks for that.

    As I looked at this more I don't think I need to convert the rows all at once. If I put a datasource look in I think I can get by just fine.