Forum Discussion

harmans's avatar
harmans
Occasional Contributor
13 years ago

Data Driven testing with JSON POST Request in REST Services

Hi,

I am testing REST web services using SOAPUI.
I am sending JSON POST requests.
It works great when i add the POST request in the text area (in the request editor).

for e.g.
{ "operation": "PayTxn.Account,CanLogin", "sessionid": 0, "parms": [{"UserName": "abcd", "Password": "password"}] }

So if i have to test manually one request at a time, its works fine but i am not sure how to do Data Driven testing. I have several POST JSON request (see below)and i would like to store all the request data in an excel or txt file.
{ "operation": "PayTxn.Account,CanLogin", "sessionid": 0, "parms": [{"UserName": "abcd", "Password": "password"}] }
{ "operation": "PayTxn.Misc,GetCountryCodes", "sessionid": 0 }
{ "operation": "PayTxn.Misc,GetAllMerchants", "sessionid": 0 }

I am familiar with DataSource and DataSource Loop (available in SOAP Pro).

So basically i want to know the best approach to do data driven testing for JSON POST requests. Thanks a lot in advance

Regards
H
  • Hi,

    here is one solution. its not very elegant, but seems to work well.
    the first step of the test case is a datasource test step that reads the values of the message into parameters. It can be a grid, spreadsheet, whatever.
    the second test step is a groovy script that reads the parameters and builds out the message and loads it to a test parameter in the next test step
    (you can just right click in the groovy editor pane and select "get data" and it will build the line of code for you.
    the third test step is the REST POST step. It has the test parameter that is loaded by the groovy script above.
    test step 4 is the datasource loop step. loop to the groovy script.

    Example of the getting a parameter value from a datasource
    def identifier_key = context.expand( '${DataSourceCSV#identifier_key}' )
    def identifier_system = context.expand( '${DataSourceCSV#identifier_system}' )
    //build your message
    def JSONMessage = '{"identifier":{"key":"' + identifier_key + '","system":"' + identifier_system + '"}"'
    //load test parameter
    testRunner.testCase.setPropertyValue( "JSONMessageProperty", JSONMessage )

    hope this helps,
    Philip
  • MauraR's avatar
    MauraR
    Occasional Contributor
    hello, first of all thanks for this post. i'm new to rest and groovy and i find this very helpful. however, i'm blocked on step 3 - post request using the loaded test parameter

    these are what i did:

    1. step 1 - datasource as Excel

    2. step 2 - groovy script, content as follows
    def zql_query = context.expand( '${Excel#zql_query}' )
    def filter_name = context.expand( '${Excel#filter_name}' )
    def desc = context.expand( '${Excel#desc}' )
    def is_favorite = context.expand( '${Excel#is_favorite}' )
    def share_type = context.expand( '${Excel#share_type}' )

    def jsonMessage = '{"ZQL_QUERY":"'+ zql_query + '","FILTER_NAME":"'+ filter_name + '","DESCRIPTION":"'+ desc + '","IS_FAVORITE":"'+ is_favorite + '","SHARE_TYPE":"'+ share_type + '"}'

    testRunner.testCase.setPropertyValue( "JSONMessageProperty", jsonMessage )

    3. step 3 - REST POST

    i have no idea how to connect step 2 to step 3. can you please help?

    thanks much!
    -MR
  • MauraR's avatar
    MauraR
    Occasional Contributor
    Please ignore my post above. I have figured it out.

    For those who needs this info - REST POST will just call the property created by the script like below

    ${#TestCase#JSONMessageProperty}

    Thanks again,
    -MR