Forum Discussion

nsinghal's avatar
nsinghal
New Contributor
2 years ago

Passing comma separated values for a property in Data driven test case

Hi, 

I am creating a grid data driven test case using Ready API. For one of the properties, I have to pass multiple comma separated values for a particular property in my REST request.
I am not getting how to split the property value in request editor into multiple strings as otherwise the value is being passed to request as single value. Please guide if there is any direct support for processing such data or I need to use groovy script?

The grid data looks like:

The request snippet is as below:



  • The above generates values without quotes. If that causes any trouble when API is called, then you can try below:

    "taxCodes": ["${= "${Properties#TaxCode}".split(',')*.trim().join(',"') }"],
  • The later one as I required my text to be enclosed in double quotes. Just made a minor change in join method. Instead of join(',"') I used join('","') to close the double quotes for each string.

    ["${= "${API Data Driven Source#TaxCode}".split(',')*.trim().join('","')}"]

  • I'm going to make an assumption that you need to pass into the request a string array
    so currently if the data source input is 

    TAX

    then when it goes thru to the rest api it gets set as

    "taxCodes" : ["TAX"]

    but if the data source input is 

    TAX,BMT

     then when it goes thru the rest api it gets set as

    "taxCodes" : ["TAX,BMT"]

    which would cause an error

    so one hacky way to do it, is within the data source for records that have more that one value, put double quotes around the COMMA string

     for 2 values 

    TAX","AMT

    for 3 or more 

    AMT","TAX","EXP

    another way would be to put double quotes around each variable in the data source

     for 1 value records 

    "TAX" 

     for 2 values 

    "TAX","AMT"

    for 3 or more 

    "AMT","TAX","EXP"

    then in your rest step you pass in the Datasource without the quotes

     "taxCodes" : [ ${Data Source#TaxCodes} ]

    I think the above way would work

    • nmrao's avatar
      nmrao
      Champion Level 3

      Nice hack as long as you have control over data source (with an additional work of adding quotes in the data source file with little confusion where to have quote and where not to have, by the way does such data accepted by the tool?). If the data source is coming from external parties and can't change, then it will be an issue.

  • Another idea with a Groovy script step  

    Potentially put a groovy step after the Data source to generated the parameter accordingly. if the datasource is as per your original screenshot then you could have the following groovy script

    def taxcodes = context.expand( '${Data Source grid#taxcodes}' )
    log.info taxcodes

    // Function to convert a comma-separated string to a string array
    def transformString(input) {
        def values = input.split(',')
        def transformedString = values.collect { "\"$it\"" }.join(',')
        return transformedString
    }

    // Convert input strings to string arrays
    def taxCodes1 = transformString(taxcodes)

    log.info taxCodes1

    it's not fully complete as you then would have to save the updated record from the groovy script to a new Test Case Parameter and then in your request  reference the new data (again without the double quotes)

     "taxCodes" : [ ${TestCase#TaxCodes} ]

    • nmrao's avatar
      nmrao
      Champion Level 3

      Oh, looks that additional processing and adding quotes done in this script.

      But, all this can be avoided with Dynamic Property Expansions.

  • nmrao's avatar
    nmrao
    Champion Level 3

    nsinghal 

    It can be achieved as below without any additional things.

    Basically, read the value from TaxCode of Data source, then splits by comma and trims the spaces if any. When split is applied, it will be automatically become list, no separate [, ] is needed. 

    When some additional logic/evaluate needs to be applied, the snippet has be enclosed between ${= <logic> } which is known as Dynamic Property Expansion.

     

    Just change from

    "taxCodes": [ "${API Data Driven Source#TaxCode}" ], 

    to

    "taxCodes": ${= "${API Data Driven Source#TaxCode}".split(',')*.trim() },
    • nmrao's avatar
      nmrao
      Champion Level 3

      The above generates values without quotes. If that causes any trouble when API is called, then you can try below:

      "taxCodes": ["${= "${Properties#TaxCode}".split(',')*.trim().join(',"') }"],
    • nsinghal's avatar
      nsinghal
      New Contributor

      Thanks nmrao for the help here. It indeed helped.

      Your help is much appreciated.

  • richie's avatar
    richie
    Community Hero

    Hey nsinghal 

    I can't see the rest of your Grid DataSource test step, nor can I see what objects are in  your testcase hierarchy - so it's difficult to see what is happening - you say the value is being passed as a single value currently - but I'm not too sure what you actually mean by this.

    From your description - I suspect you might be talking about the fact that the REST step executes once and only the first row in your DataGrid is being submitted - is that what you mean?

    However - then I started changing my mind and thinking perhaps you had more than one testdata value in your DataGrid - but you were injecting only a single value (like you only created one property.

    Now, I've changed my mind again and I'm thinking that you're saying each row of data in your Grid is a single record and each comma separated value should actually go to a different json attribute in your payload.

    and now I've changed my mind again and that you are expecting the discrete cell of comma separated values to populate the json array fine.

    See?  I just keep thinking of possible options.  This is my fault cos I'm too literal a lot of the time, so it means that I just need things laid out clearly.

    Some of the more clever people on the forum might be able to get you, but I'm just a bit too confused.

    If you can provide the following, I'll be able to answer your question and sort what you need.

    **A screenshot of the test case hierarchy (I'd like to see the objects in yoru test case - so I can see the flow of sequential steps in your test)

    **A Screenshot of all of the DataGrid test step AFTER you've run the DataGrid step ? (so I can see the properties on the left, the resultset at bottom and the full DataGrid content in the Configuration frame (so I can see how you have it configured and the results of the DataGrid test step running)

    **Clear explanation for a single test, what value you are expecting to populate the "taxCodes" array

    **Can you confirm you have a DataSourceLoop test step included in your test?

    If you can supply all that, I'll answer asap with your answer.

     

    Cheers,

    rich