Forum Discussion

bdre's avatar
bdre
Occasional Contributor
7 years ago

how to access a datasource result from calling groovy script

I've created a Groovy datasource that returns information in the results associative array available when the datasource script runs.

Now I want to run that datasource from another groovy script, but don't know how to access the datasource results information after calling the datasource.  The datasource looks something like this:

 

def row = testRunner.testCase.testSteps["MyDataSource"].getCurrentRow()
if (row < 100) {
    result["email"] = "user-" + String.format("%04d", row+1) + "@company.com"
}

The calling script looks like this:

 

testRunner.testCase.testSteps["MyDataSource"].run(testRunner, context)

 

Is it possible to access the "email" key created by the data source step?  I've tried a lot of methods, none of which worked.

 

Thanks!

 

  • Hi,

     

    Each DataSource step has at least 1 property. 

     

     

    In order to reference that property you can use the syntax:

     

    ${DataSource#testProperty}

    If you want to reference that property from a groovy script you need to use:

     

    context.expand( '${DataSource#testProperty}' )

    But I am not sure I understand your question... Normally I would use a datasource step in combination with a datasource-loop step.

     

     

    So there will be an execution for each element in the datasource. Why would you call yours with another groovy script? What is the use case?

  • Lucian's avatar
    Lucian
    Community Hero

    Hi,

     

    Each DataSource step has at least 1 property. 

     

     

    In order to reference that property you can use the syntax:

     

    ${DataSource#testProperty}

    If you want to reference that property from a groovy script you need to use:

     

    context.expand( '${DataSource#testProperty}' )

    But I am not sure I understand your question... Normally I would use a datasource step in combination with a datasource-loop step.

     

     

    So there will be an execution for each element in the datasource. Why would you call yours with another groovy script? What is the use case?

  • bdre's avatar
    bdre
    Occasional Contributor

    That didn't work for me initially.  Eventually I noticed menus not opening, so I restarted ReadyAPI app and now it works.

     

    The use case:  I wanted to use a data source loop, but I need to have a nested data source and the inside data source needs to generate a variable number of objects.

     

    I have two data sources: a parent object and a variable number of child objects.  The calling script will create one parent object, then a variable number of children.  Would it be better to have a single data source to create both of these?  Can my data source return a list?  If so then I can put the data source inside a data source loop and the row count (at least for the parent objects) will be handled by the loop.  But I may still have a problem with the row count for the child objects.

     

    I would appreciate any suggestions you have for this case.

    Thanks!

     

     

  • Lucian's avatar
    Lucian
    Community Hero

    Not sure if I understand it correctly but you want a nested loop. I would simply use:

     

     

    So for instance... Say you want to send the names of some products from a grocery store. The execution would look like this:

    1. Fruits

        1A. Oranges

        1B. Apples

        1C. Pears

    2. Coffee

        2A. Black

        2B. Latte