How to validate response value containing the number sign # - karate

My response looks like this:
[
{
"id": 1,
"name": "TEST FORMAT",
"value": "#####"
}
]
I want to validate it like this:
And match response[0] == { id: 1, name: 'TEST FORMAT', value: '#####' }
But it gives me the error below:
ERROR com.intuit.karate - assertion failed: path: $[0].value, actual: '#####', expected: '#####', reason: unknown validator

Actually Karate treats strings that start with # as special and 99% of the time you won't be affected by it.
Anyway, here is the workaround:
* def response = [ { "id": 1, "name": "TEST FORMAT", "value": "#####" } ]
* match response[0] == { id: 1, name: 'TEST FORMAT', value: '#? _ == "#####"' }
* match response[0] == { id: 1, name: 'TEST FORMAT', value: '#regex #{5}' }
I'm actually fixing this right now so that it should work as expected in future versions without needing the workaround.

Related

Having issues reusing a stored variable in Graphql query in Karate framework

I have an issue with the variable usage. Tried different options(storing variable differently, declaring, using text for defining the query, storing the query as a variable).
Still have the below error:
"errors": [
{
"message": "invalid input syntax for type uuid: \"#(queueID)\"",
"locations": [
{
"line": 1,
"column": 11
}
],
"path": [
"deleteQueue"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"name": "SequelizeDatabaseError",
"parent": {
"length": 109,
"name": "error",
"severity": "ERROR",
"code": "22P02",
"position": "34",
"file": "uuid.c",
"line": "137",
"routine": "string_to_uuid",
"sql": "DELETE FROM \"Queue\" WHERE \"id\" = '#(queueID)'"
This are my Gherkin steps:
Given request { query: 'mutation {createQueue(input: {name: "BDD-delete" }) {id} }'}
When method POST
Then status 200
And match response.data.createQueue.name == "BDD-delete"
* def queueID = response.data.createQueue.id
* print queueID
Given request { query: 'mutation {deleteQueue (id:"#(queueID)")} '}
And this is the output, when I print the queueID:
13:14:16.745 [main] INFO com.intuit.karate - [print] 758c0524-b18d-41f6-96aa-9db5eb8a7ac8
Tried using variable for the query
Given text payload =
"""
mutation {
createQueue(input: {name: "BDD-delete" })
{id, name}
}
"""
And the same tried for the deleteQueue
Feels like the issue is related with str and uuid. I must pass a uuid between the brackets in "#(queueID)"
First read this to get a sense of why this is happening: https://github.com/karatelabs/karate#rules-for-embedded-expressions
So try this:
Given request `{ query: 'mutation {deleteQueue (id:"${queueID}")} '}`
The good thing is that Karate supports JS-style placeholder replacement in strings within back-ticks.
Also refer: https://stackoverflow.com/a/69349118/143475

How to match string and ignore the case in karate?

There is a case where one value sometimes is lower case and sometimes it's upper case. this is the response coming from an API and we have to match if every field in response is correct by ignoring some values. The error text in response sometimes has one keyword in lower and some scenarios it is upper case. How we can ignore one keyword in a string to not match? I don't want to ignore whole text as it works fine if I ignore whole string but is it possible to ignore one keyword only?
Scenario: string matching
* def test =
"""
{
"sourceType": "Error",
"id": "123456",
"type": "searchuser",
"total": 0,
"value": [
{
"details": "this is the user search case",
"source": {
"sourceType": "Error",
"id": "77200203043",
"issue": [
{
"severity": "high",
"code": "678",
"message": {
"text": "No matching User details found"
},
"errorCode": "ERROR401"
}
]
},
"user": {
"status": "active"
}
}
]
}
"""
* match test ==
"""
{
"sourceType": "Error",
"id": "#present",
"type": "searchuser",
"total": 0,
"value": [
{
"details": "#present",
"source": {
"sourceType": "Error",
"id": "#ignore",
"issue": [
{
"severity": "high",
"code": "678",
"message": {
"text": "No matching User details found"
},
"errorCode": "ERROR401"
}
]
},
"user": {
"status": "active"
}
}
]
}
"""
How to ignore the case only for user here? I tried below but it treats #ignore as a value.
"text": "No matching #ignore details found"
I'm not looking at your payload dump but providing a simple example. Use karate.lowerCase():
* def response = { foo: 'Bar' }
* match karate.lowerCase(response) == { foo: 'bar' }
EDIT: you can also extract one value at a time and do a check only for that:
* def response = { foo: 'Bar' }
* def foo = response.foo
* match karate.lowerCase(foo) == 'bar'

Karate - Nested JSON object schema validation causes KarateException

Feature: Test Karate schema validation
Scenario: Test nested json objects
* def response = read('tasks.json')
* def schema = { ab: "##[] string", c: "##[] string" }
* match response ==
"""
{
id: '#string',
name: '#string',
obj1: '#(schema)' ,
obj2: '##(schema)' ,
obj3: '#(schema)' ,
obj4: '#null'
}
"""
Following is json file used (tasks.json)
{
"id": "ad:p2:53456:4634:yu",
"name": "name",
"obj1": {
"ab": [
"test"
],
"c": null
},
"obj2": null,
"obj3": {
"ab": [
"tester"
],
"c": [
"t1", "t2"
]
},
"obj4": null
}
Error: com.intuit.karate.exception.KarateException: javascript evaluation failed: string, ReferenceError: "string" is not defined in at line number 1
I have tried multiple ways like :
obj1: '#(^schema)',
obj1: '#object schema'
but not able to fix the issue.
It should be ##[] #string , read the docs: https://github.com/intuit/karate#schema-validation

Karate match each on response assertion is failing to identify missing keys in response

I have a match each assertion like below in my code. Just tried creating similar examples as my code, just to explain the issue.
Scenario: Example scenario 1
* def response =
"""
[
{
id: 1,
name: "John",
password: "abc123"
},
{
id: 2,
name: "David",
password: "abc123"
},
{
id: 3,
name: "Mike",
password: "abc123"
},
{
id: 4,
name: "Johny"
}
]
"""
* match each response[*].password contains 'abc123'
Test status : Pass
Password field is missing in object 4(where id=4). Above test is passing for me. I am expecting Karate to fail the test in this case. How can I make my test fail in this case?
Scenario: Example scenario 2
* def response =
"""
[
{
id: 1,
name: "John",
},
{
id: 2,
name: "David",
},
{
id: 3,
name: "Mike",
},
{
id: 4,
name: "Johny"
}
]
"""
* match each response[*].password contains 'abc123'
Test status : Pass
Here, there is no password field at all in response. But my test is passing.
Need a work around to fail these kind of scenarios.
Example 3 :
* def response =
"""
[
{
id: 1,
name: "John",
password: "abc123",
skills :[ "training", "management"
]
},
{
id: 2,
name: "David",
password: "abc123",
skills :[ "training", "management"
]
},
{
id: 3,
name: "David",
password: "abc123",
skills :[ "training", "coding"
]
},
{
id: 4,
name: "David",
password: "abc123",
skills :[ "training", "management"
]
}
]
"""
Considering * match each response contains { password: 'abc123' } format(mentioned by #peter) to check example 1 and 2, what if I want to check skills array having 'training' in each object under response? How can I achieve this?
you can use match each to validate the json schema
https://github.com/intuit/karate#match-each
Note that response[*].password is a JsonPath expression that will return an array of all the password key-values found and will return only 3 in your case.
What you are looking for is this:
* match each response contains { password: 'abc123' }

The notpresent keyword is not working in Karate

I am trying to assert that a key is not in the JSON response. This is the response
{
"_type": "UserAccount",
"accountName": "Account_Name",
"accountType": "regular",
"whiteList": true,
"sfAccountId": "1",
"preferredLanguage": "english",
"imageSize": "highRes",
"_id": "775a8451-6a07-42da-a199-fe98f44bdc06"
}
I have tried both of these steps according to the documentation karate#null-and-notpresent and this answer from Peter Thomas
And match response.appClientId == '#notpresent'
And match response == {appClientId:'#notpresent'}
However this is the error I keep getting for the first step above
com.jayway.jsonpath.PathNotFoundException: No results for path: $['appClientId']
and for the second step
com.intuit.karate.KarateException: path: $, actual
Thanks in advance
Maybe your version is out of date, the below works for me in 0.9.1:
* def response =
"""
{
"_type": "UserAccount",
"accountName": "Account_Name",
"accountType": "regular",
"whiteList": true,
"sfAccountId": "1",
"preferredLanguage": "english",
"imageSize": "highRes",
"_id": "775a8451-6a07-42da-a199-fe98f44bdc06"
}
"""
* match response.appClientId == '#notpresent'
* match response contains { appClientId: '#notpresent' }