Forum Discussion

_Oliver_'s avatar
_Oliver_
Contributor
3 years ago
Solved

Passing an array element in the "value" field of a test step

Hi all,

I need to call a service with a parameter value that should be an array.

The parameter name is officialLanguages and I need the generated URL to look like that

xxxxxxxxxx?officialLanguages=EN&officialLanguages=FR

I cannot figure out how to put these two elements in the value field of the test step. For example, this (see below image) does not work because the whole ["FR","EN"] is considered as one single string and not as an array of two strings.

 

I tried various combination with and without quotes, with and without brackets etc... but nothing worked.

I found a workaround by disabling URL encoding for this parameter value and by defining the value as shown here :

But that looks to me like an unorthodox solution...

 

Is there an easier/cleaner way to do that ?

 

Regards

Oliver

 

  • I'm not sure about a cleaner way in that screen, but another method would be to use a Groovy script step prior to the service call test step which builds the URL based on the array..

     

    E.g. 

    def rootUrl = "Whttps://www.yyyyy.com/";
    def languageArray = ["FR","EN","US"];
    
    def newUrl = rootUrl;
    
    // Iterate over the language array to add each language.
    // Note, this is purely an example and needs more work to include the ? and & 
    // as appropriate.
    
    languageArray.each{val ->
        newUrl = newUrl + "officialLanguages=" + val;
    }
    
    // Setting the next test step Endpoint to be the 'newUrl'....
    testRunner.testCase.testSteps["myServiceRequest"].properties['Endpoint'].value = newUrl;

     

     

4 Replies

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    I'm not sure about a cleaner way in that screen, but another method would be to use a Groovy script step prior to the service call test step which builds the URL based on the array..

     

    E.g. 

    def rootUrl = "Whttps://www.yyyyy.com/";
    def languageArray = ["FR","EN","US"];
    
    def newUrl = rootUrl;
    
    // Iterate over the language array to add each language.
    // Note, this is purely an example and needs more work to include the ? and & 
    // as appropriate.
    
    languageArray.each{val ->
        newUrl = newUrl + "officialLanguages=" + val;
    }
    
    // Setting the next test step Endpoint to be the 'newUrl'....
    testRunner.testCase.testSteps["myServiceRequest"].properties['Endpoint'].value = newUrl;

     

     

    • _Oliver_'s avatar
      _Oliver_
      Contributor

      Thanks a lot ChrisAdams 

      I thought of that, I am just thinking (maybe wrongly) that this is a basic thing and I am therefore assuming that there should be an easier way to handle that without having to use groovy.

      But thanks for your script anyway, I am sure I'm going to use, for this issue or for another one 👍

      • ChrisAdams's avatar
        ChrisAdams
        Champion Level 3

        It is basic, and buiIding dynamic URLs is common, but I genuinely cannot think of a way of doing this without Groovy script.

        The problem with this issue is that you're passing in an array so you don't know how many elements hence the slightly more complex looking Groovy.