Ask a Question

JSON Request Parametrization

678
Regular Contributor

JSON Request Parametrization

I am looking how to do a json Request parameterization as I am holding a hundreds of JSON if I can't create every single json as a Test case/Rest request so I want to pass JSON from Excel File / Text file - Appreciate the help I i am

9 REPLIES 9
nmrao
Community Hero

Ok.

Are you using data-driven testing?
What is the skeleton of the test case / steps with their type?


Regards,
Rao.
groovyguy
Community Hero

If you have an excel or text file, I've found a datasource step using a file as the input worked best for me.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
678
Regular Contributor

Yes I am using Data Driven testing 

 

JSON String is in Files  and Parameters are in Excel 

 

I need to Get JSON String from Files set parameters from Excel based on the json string and form a JSON Request dynamically 

678
Regular Contributor

@groovyguy

 

appreciate if you share 

groovyguy
Community Hero

@678: I don't have a direct example to share. It looks like you have two files that work together, which may not work with a single File DataSource. If you can provide a sample of the txt file and excel file, I may be able to work up a set of DataSources and/or a groovy script.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
678
Regular Contributor

Yes i am having a two files 

 

1) Json 2) Excel/csv 

 

JSON 

 

Text File name :  Name.json

"userName": "${userName}",
"password": "${password}",

 

 

2) Excel / Csv Data 

 

userName password  inputjson

kumar       kumar123 Name.json

New2API
Frequent Contributor

Hello there! we have implemented very similar approach. i.e., reading a canned JSON from a file then replacing JSON objects from Data-source value.

 

Data-driven test case structure would look like this:

SoapUI-TestCaseStr.png

 

Groovy script would read JSON file and then replace the JSON objects and build new JSON payload for your REST request.

 

 

import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder

//## Get Directories ##//
def RequestPath = "C://TestUser//Request//"

log.info "Read canned JSON payload from $RequestPath"
log.info "Parsing JSON payload..."

//## READ canned request from a file to modify ##//
def JsonPayload = new File(RequestPath + "SampleJSON.txt").text
def JSONReq = new JsonSlurper().parseText JsonPayload


//## Get data from DataSource ##//
def userName = context.expand( '${DataSource#UserName}' )
def Password = context.expand( '${DataSource#Password}' )

log.info "Updating json nodes..."

JSONReq.userName = "$userName"
JSONReq.password = "$Password"

log.info "Updating JSON Payload at testCase level property eSignJSON"

//## build json body and write to a file for reference ##//
def REQUEST = new JsonBuilder(JSONReq).toString()
    testRunner.testCase.setPropertyValue("JSONPayload", REQUEST)

log.info "JSON payload is $REQUEST"

 

 

In the Rest request point "JSONPayload" property to get the latest payload.

i..e., ${#TestCase#JSONPayload}

 

Hope this helps!

 

678
Regular Contributor

@New2API

 

 

thank you but this is for only one json payload what if i have multiple JSON payload files ?

how can i loop one file read json payload update json nodes based on data source and execute request and come back loop again read json payload - update json nodes and execute ?

New2API
Frequent Contributor

Well, by looking at your example I assumed only Username and Password fields are referring to variables (or properties). File name looked constant. My bad.

 

I will have to think over if we could loop through files from a directory.  Another workaround would be having four fields in CSV or excel.

 

Ex: 

UserName  Password  FileName   File

TestUser1   Test123    JSON1.txt  C://TestUser//JsonFiles//JSON1.txt

TestUser2   Test345    JSON2.txt  C://TestUser//JsonFiles//JSON2.txt

TestUser3   Test678    JSON3.txt  C://TestUser//JsonFiles//JSON3.txt

 

Load this data into data-source and then use same groovy script to build json payload.

 

Regards,

cancel
Showing results for 
Search instead for 
Did you mean: