I have a json response in 1 request like this:
{"total":1,"page":1,"records":2,"rows":[{"id":1034,"item_type_val":"Business
Requirement","field_name":"Assigned To","invalid_value":"Jmeter
System","dep_value":"","dep_field":""},{"id":1033,"item_type_val":"Risk","field_name":"Category","invalid_value":"Energy","dep_value":"Logged
User","dep_field":"Assigned To"}]}
and in 2nd request like this:
{"total":1,"page":1,"records":2,"rows":[{"id":1100,"item_type_val":"Business
Requirement","field_name":"Assigned To","invalid_value":"Jmeter
System","dep_value":"","dep_field":""},{"id":1111,"item_type_val":"Risk","field_name":"Category","invalid_value":"Energy","dep_value":"Logged
User","dep_field":"Assigned To"}]}
Both are same but different id's. I need to verify the 1st json response from 2nd json response and compare both that both are same or not. here both are same but having different id's which should be acceptable. how can i do this by regex so i can ignore the id's and match whole content?
Not sure if you can do it with a single regex but other way out is you can create a map of it and then compare everything except 'id'
I believe the easiest way would be just discarding these id entries using JSR223 PostProcessor and Groovy language which comes with JSON support
Add JSR223 PostProcessor as a child of the sampler, which returns your JSON
Put the following code into the JSR223 PostProcessor's "Script" area
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def slurper = new JsonSlurper()
def jsonResponse = slurper.parseText(prev.getResponseDataAsString())
jsonResponse.rows.findAll { it.remove("id") }
def newResponse = new JsonBuilder(jsonResponse).toPrettyString()
//depending on what you need
vars.put("responseWithoutId", newResponse) // store response withou ID into a JMeter Variable
prev.setResponseData(new String(newResponse)) // overwrite parent sampler response data
log.info(newResponse) // just print the new value to jmeter.log file
So you have the following choices:
vars.put("responseWithoutId", newResponse) - stores the new JSON (without these id) into a ${responseWithoutId} JMeter Variable
prev.setResponseData(new String(newResponse)) - after this line execution parent sampler data won't contain any "id"
log.info(newResponse) - just prints JSON without "id" to jmeter.log file
I don't know your test plan design, personally I would store responses from 2 requests into 2 different JMeter Variables i.e. ${response1} and ${response2} using above approach and compare them with the Response Assertion like:
Related
I want to perform validation of JSON response against DB data.
For example I have a student table with column as "StudentID" , "StudentName" and "StudentAddress"
and In JSON response we have element as "StudentNumber", "StuName" and "StuAddress" (Name is different in both JSON and DB)
Question 1: How can I compare entire JSON against the DB data to match it in JMeter.
Question 2: If I want to perform validation like if in Database "StudentID"=1 then in JSON response "StudentNumber" should be equal to "A". How can I validate it in JMeter in a single script
If you want to compare entire JSON to the result of the database query which returns more than 1 row - the only option is going for JSON Assertion and implementing your pass/fail criteria in Groovy.
Groovy has built-in JSON support so it can parse the response as JSON and convert it to maps/lists/values
Groovy can iterate the ResultSet object containing the query results, see Debugging JDBC Sampler Results in JMeter for more details
If you want to compare individual entries:
In the JDBC Test Element specify variable name to store the value from the database
Use JSON Extractor or JSON JMESPath Extractor to store the value from JSON
Compare 2 variables using Response Assertion
Following is a sample code used within a JSR223 element to process JSON response
import groovy.json.JsonSlurper
try{
def responseJSON = prev.getResponseDataAsString()
if(responseJSON) {
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(responseJSON)
def itemCount = object['items']['id'].size()
log.info("Items $itemCount")
for (i in 0..<itemCount){
log.info("Student Number ${object['items']['id'][i]}")
log.info("Student Name ${object['items']['studentName'][i]}")
log.info("Address ${object['items']['address'][i]}")
}
}catch (anything){
log.info("Error ")
}
Groovy has support for working with JSON. Documentation.
import com.jayway.jsonpath.JsonPath
def idCSV = new File('id.csv')
def index = [fileOne.json, fileTwo.json]
def jsonString
index.each { file ->
jsonString = ________
def ids = JsonPath.read(jsonString, '$..id')
ids.each { id ->
idCSV << id << newLine
}
}
How to fill the jsonString = ____, so that I can json file into string and parse the string to extract ids and some information from the json string.
And I don't to do it in http request-> GET-> file format.
Previously i have extraced jsonString from http response and it worked well now I want to do it this way.
Use JsonSlurper:
def jsonString = new groovy.json.JsonSlurper().parseText(new File("json.txt").text)
My expectation is that you're looking for File.getText() function
jsonString = file.text
I have no full vision why do you need to store the values from JSON in a CSV file, however there is an alternative way of achieving this which doesn't require scripting as your approach will work with 1 concurrent thread only, if you will add more users attempting writing into the same file - you'll run into a race condition :
You can read the files from the folder into JMeter Variables via Directory Listing Config
The file can be read using HTTP Request sampler
The values cane be fetched using JSON Extractor, they will be automatically stored into JMeter Variables so you will able to use them later on
If you need the values to be present in the file (although I wouldn't recommend this approach cause it will cause massive disk IO and potentially can run your test) you can go for the Flexible File Writer
I've a JSON response like this:
{
"id":"1",
"key":"123145"
}
I need to use the value of "key" as input for a payload request:
param=1¶m=2¶m3=$key
I would like to write the request like :
param=1¶m=2¶m3=${keyValue}
so the result should be
param=1¶m=2¶m3=12345
I've tried with Property Transfer but doesn't works.
Any suggestion?
You could use a property transfer test step like this:
Here you're transferring a value to a test-case level property called myProperty (which you created previously). Then, you can reference this property in your URL as ${#TestCase#myProperty}
So you can proceed like below
1) Fetch value you want from json using groovy step
2) Store the fetched value from Groovy Step into TestCase Property
3) Use that value in the request you want
import groovy.json.JsonSlurper
def input=context.expand('${SampleRequest#Request}')
log.info input
def jsonObj=new JsonSlurper().parseText(input)
def key=jsonObj.key
log.info key
testRunner.testCase.setPropertyValue("Key",key)
The value stored can be used as below in a request. Syntax to access Property Value
${#TestCase#Key}
I have a REST service which I am testing with SoapUI. The first step in my TestSuite returns the Response (Json): 'policyID' then I need to put this in another request ( i.e, test step) to get the final response - which is newly created policy number. I have written a code in groovy
import groovy.json.*
def response = context.expand('${CreateApplication#Response}')
def json = new JsonSlurper().parseText(response) --- Error line
log.info json.policyId
context.testCase2.setPropertyValue("id",json.policyID.toString())
I am getting error
Java.lang.ilegal.arguementexception :the JSON INPUT TEXT should should
neither be null nor empty
This error is coming at the line above marked as error, could you guys please help as I am not able to figure it out
If I understood the question correctly:
You can do below steps:
create a property transfer step.
Select source as ur first REST step response
Select Property Raw Response
Select Path Language as JsonPath
Use Jsonpath $.id.
In Target section use your testcase name
Create a test case level property "id"
Go to Property transfer step and select the same property name "id" in property section
save the project.
In the next step (REST) use id like below :
{ "id": "${#TestCase#id}", "issued": "1999-01-01", "policyId": "1011, "name":"test321"}
Hope this is clear?
I am trying to learn SoapUI and I have not found any good guides on how to perform a transfer property from a Rest GET request to a Rest POST request.
I have the following payload returned from a REST GET request
{
"a":"a",
"b": { "b1":"b1", "b2":"b2" },
"c":"c"
}
I want to take this JSON and remove c, then submit the rest to a POST request. I wish to post
{
"a":"a",
"b": { "b1":"b1", "b2":"b2" },
}
I am trying to do all this in SoapUI, but have had no success. I am able to get individual values by saying in the source property is RseponseAsXML and the target property is Request. Then I use the command //*:a. But it only returns that value.
I do not want this to be xml though, I am working strictly with JSON.
Thank you.
If you want to manipulate the JSON response to use in other TestStep it's easier to use groovy TestStep instead of Transfer testStep. So create a groovy TestStep and manipulate the response using the following code:
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
// get the response using the name of your test step
def response = context.expand('${REST Test Request#Response}')
// parse response
def jsonResp = new JsonSlurper().parseText(response)
// remove "c" element
jsonResp.remove("c")
// parse json to string in order to save it as a property
def jsonAsString = JsonOutput.toJson(jsonResp)
log.info jsonAsString // prints --> {"b":{"b1":"b1","b2":"b2"},"a":"a"}
// save the json response without "c" in a testCase property
testRunner.testCase.setPropertyValue('json_post',jsonAsString)
With the code above you parse the response, remove the c element and save as a property in the testCase then in your REST POST you can use the follow code to get your new JSON:
${#TestCase#json_post}
Hope this helps,