I cannot get the inner nested key's value of this json object (it gets response from our API call, I just only copy the response from karate)
[
{
"code": 200,
"result": "[{\"distinct\":false,\"operatetime\":\"2019-05-17 17:01:01\",\"personid\":\"e8edec61-fd1a-4c69-8b60-fb8d21d06095\",\"sampleid\":\"1c9410cd-608d-4eb1-8d12-c8f2faf7fca4\"}]"
}
]
And def tempreponse = [{"code":200,"result":"[{\"distinct\":false,\"operatetime\":\"2019-05-17 17:01:01\",\"personid\":\"e8edec61-fd1a-4c69-8b60-fb8d21d06095\",\"sampleid\":\"1c9410cd-608d-4eb1-8d12-c8f2faf7fca4\"}]"}]
And def temp1 = tempreponse[0].result <- this sentence works
And def temp1 = tempreponse[0].result[0] <- however, this doesn't work, the print of temp1 is blank
In fact, I need to get the value of personid and sampleid, but failed
Yeah your response looks really wrong, a string within a JSON and all. But even if that is the expected response, Karate can handle it. Refer the documentation on type conversions: https://github.com/intuit/karate#type-conversion
* def response =
"""
[
{
"code": 200,
"result": "[{\"distinct\":false,\"operatetime\":\"2019-05-17 17:01:01\",\"personid\":\"e8edec61-fd1a-4c69-8b60-fb8d21d06095\",\"sampleid\":\"1c9410cd-608d-4eb1-8d12-c8f2faf7fca4\"}]"
}
]
"""
* json result = response[0].result
* def personId = result[0].personid
* match personId == 'e8edec61-fd1a-4c69-8b60-fb8d21d06095'
Related
peter had provided me a solution to use karate 0.9.3 to apply assertion from examples
Trying to do some assertion from request, which will be present in response
i was wondering if it is possible to assert a value from request instead of full request.
**Background:**
* configure headers = read('classpath:merchantServiceheaders.js')
Given url MservUrl
And path 'XXXXXX'
And request Data
When method POST
Then status 200
And match response != null
And match $ contains {serviceName: 'XXXXX'Service', responseMessage:
'Success' }
And match each $.clauses..responseMessage == 'Success'
And match each $..predicate != null
And match each $..predicate == '#present'
And match each $..predicate == '#regex [^0-9]+[0-9]*$'
And match $..predicate contains Data.clauses.subject
Examples:
|Data! |
|'{"clauses":[{"subject":"XXXX","predicate":"999999"},
{"subject":"XXXXX","predicate":"99999"}]}'|
what i am trying to do is on the And match $..predicate contains Data.clauses.subject
is that possible?
Yes, it is very much possible if Data is defined as a variable. But note that $..predicate will always be a JSON array: https://github.com/intuit/karate#get-plus-index
If you want help, please create a proper simple working example.
Sample Code:
Feature: Validation
Scenario:
* def resp = {predicate : "4325325456545646"}
* def data =
"""
{
"clauses": [
{
"subject": "5432154321543210",
"predicate": "4432154321543210"
},
{
"subject": "4325325456545646",
"predicate": "4325325456545646"
}
]
}
"""
* def sublist = get data.clauses[*].subject
* print sublist
* match sublist contains resp.predicate
payload.json
{
"channelData": {
"data": "CHANNEL_DATA",
"salesChannel": "WEB",
"createdBy": "WEBSITE",
"accountId": "#(accountId)",
"sessionId": "#(accountname)"
}
}
Request.feature
#ignore
Feature:
Scenario:
* def Request = read('../payload.json')
* def headersData = { "Content-Type" : "application/json"}
Given url BaseUrl + '/account/'+'#(accountId)'
And request Request
And headers headersData
When method post
Then status 200
account.feature
#ignore
Feature:
Scenario:
* print temp
* def headersData = { "Content-Type" : "application/json"}
Given url BaseUrl + '/accountdata/'
And request temp
And headers headersData
When method post
Then status 200
Account-token.feature
Feature:
Scenario: identify the reference account
* def reqRes = karate.call('../Request.feature')
* def temp = karate.pretty(reqRes)
* def account = karate.call('../requests/account.feature',{temp : temp })
I want to pass response of Request.feature as a request payload of account.feature. When I try above case, value for temp is printed in an array. [#response of Request.feature] how to resolve this.
Please study this example carefully, it will answer all the questions you have and you will see where you are going wrong: call-feature.feature
For example, to get the response out of a call result, you have to "unpack" it:
Scenario: create kittens
* def result = call read('create-two-cats.feature')
* def temp = result.response
I get 2 different responses from an endpoint depending on it's state and either one of them is fine.
first response:
{"available":'#boolean',"collection":'#boolean'}
second response:
{"code": "#string","message": "#string"}
I'm trying the following but it's not working:
def firstSchema = {"available":'#boolean',"collection":'#boolean'}
def secondSchema = {"code": "#string","message": "#string"}
match response contains any (firstSchema, secondSchema)
Any ideas how to best get this working so either response is fine?
Thanks
Try this:
* def first = { available: true, collection: true }
* def second = { code: 'foo', message: 'bar' }
* def response = second
* def expected = response.code ? { code: '#string', 'message': '#string' } : { available: '#boolean', collection: '#boolean' }
* match response == expected
Also refer to the documentation on "Conditional Logic". You can use JsonPath to "check" what shape the response is and then define expected results.
I get 2 different responses from an endpoint and either one of them is fine.
first response:
response1.xml
second response:
response2.xml
I tried below assertion but it's not working:
And match response == read('response1.xml') || response == read('response2.xml')
My response looks as follows :
{
"data":[
{
"foo":bar1
"user_email":"user#user.com",
"user_id":1
},
{
"foo":bar2
"user_email":"user#user.com",
"user_id":1
}
]
}
* def DBOutput = #fetching values from DB
* match response.data[*].foo contains [DBOutput1[0][0],DBOutput1[1][0]]
DBOutput1 has data as follows : [["bar1"],["bar2"]]
This match fails, for some reasons value passed into the expected list in match statement is DBOutput1[0][0]
This is the error I am getting actual: ["bar1","bar2"], expected: 'DBOutput1[0][0]',
You have some seriously malformed JSON in your example above. The below snippet works, just paste it into a new Scenario:
* def response =
"""
{
"data":[
{
"foo": "bar1",
"user_email":"user#user.com",
"user_id":1
},
{
"foo": "bar2",
"user_email":"user#user.com",
"user_id":1
}
]
}
"""
* match response.data[*].foo contains ['bar1', 'bar2']
Now it is up to you to fix your test, without knowing what your DBOutput is no one can help further.
I iterated over the DBoutput and stored them in a new array list. I then matched the response to the list and it worked.
match response.data[*].foo contains ListFromDB
I got a response like this.
"{\"statusCode\":204,\"output\":\"{\\n \\\"Id\\\" : \\\"884d39b8-4afc-4ee3-807a-9d6dbde8c390\\\",\\n \\\"temp\\\" : \\\"33\\\",\\n \\\"lastUpdateAuthor\\\" : null\\n}\"}"
How can I parse this response and do something like
* def expectedOutput =
"""
{
"Id": "884d39b8-4afc-4ee3-807a-9d6dbde8c390",
"temp": "33",
"lastUpdateAuthor": null
}
Scenario: Testing a PUT endpoint with request body
Given path 'v0'
And request input
When method put
Then match status 200
And match JSON.parse(JSON.parse(response).output) == expectedOutput
The last line of code got skipped by Karate.
Any input?
Figured that out.
* def expectedOutput =
"""
{
"Id": "884d39b8-4afc-4ee3-807a-9d6dbde8c390",
"temp": "33",
"lastUpdateAuthor": null
}
"""
Scenario: Testing a PUT endpoint with request body
Given path 'v0'
And request input
When method put
Then match status 200
And json convertJSON = $
And json convertJSON = convertJSON.output
And match convertJSON.latitude == expectedOutput
It worked.