I am new to karate and read through most of the examples, but can't quite crack this. Your help is much appreciated! Assuming I have an array and I need to call a service and pass each value of this as a param, how do I do that please?
Thanks in advance.
How about this:
Scenario Outline:
Given url 'http://httpbin.org'
And path 'get'
And param foo = '<value>'
When method get
Then status 200
Examples:
| value |
| hello |
| world |
And in Karate 0.9.0 onwards you can do this:
Background:
* def data = [{ value: 'hello' }, { value: 'world' }]
Scenario Outline:
Given url 'http://httpbin.org'
And path 'get'
And param foo = '<value>'
When method get
Then status 200
Examples:
| data |
EDIT: and if you need to transform an existing primitive array:
Background:
* def array = ['hello', 'world']
* def data = karate.map(array, function(x){ return { value: x } })
Scenario Outline:
Given url 'http://httpbin.org'
And path 'get'
And param foo = '<value>'
When method get
Then status 200
Examples:
| data |
Related
Scenario Outline: Explore Karate '<ID>'
* karate.set($attributesFirstRun[*].created_timestamp,'#present')
* karate.set($attributesSecondRun[*].created_timestamp,'#present')
* match attributesFirstRun == attributesSecondRun
Examples:
| read('Sample.csv') |
I tried this. But I'm getting this error
org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:1:42 Expected an operand but found *
I think you are over-thinking this. Karate is actually just plain JS. And sounds like you are trying to do a "bulk update" via JsonPath - sorry, that's not supported, perhaps you would be interested in contributing code.
Here's the solution for updating all elements of an array:
* def before = [{ a: 1 }, { a: 2 }]
* def after = before.map(x => ({ a: x.a * 5 }))
* match after == [{ a: 5 }, { a: 10 }]
Keep in mind updating JSON is easy:
* def data = {}
* data.a = 1
* match data == { a: 1 }
Refer the docs: https://github.com/karatelabs/karate#json-transforms
Like if I have two JSON as below and I want to check the mismatch between those
JSON 1:
{
name:'john',
contact:'123',
country:'america'
}
JSON 2:
{
name:'vishal',
contact:'123',
country:'India'
}
Now it will return me with the mismatch between name and country not only the name?
No this is not supported. We feel this is not needed, because in your regular CI runs you only care if the test passed or failed, and you see the details in the log.
Also note that you can simulate this if you really want using a Scenario Outline: https://stackoverflow.com/a/54108755/143475
Finally, if you care so much about this, kindly contribute code, this is open-source after all.
EDIT: you can easily do this by iterating over keys. Here is the code:
EDIT2: Setting up data via a Background is no longer supported in version 1.3.0 onwards, please look at the #setup tag: https://github.com/karatelabs/karate#setup
Feature:
Background:
* def json1 = { name: 'john', contact: '123', country: 'america' }
* def json2 = { name: 'vishal', contact: '123', country: 'India' }
* def keys = karate.keysOf(json1)
* def data = karate.mapWithKey(keys, 'key')
Scenario Outline: <key>
* match (json1[key]) == json2[key]
Examples:
| data |
And here is the report:
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
I am new to karate and read through most of the examples, but can't quite crack this. Your help is much appreciated! Assuming I have an array and I need to call a service and pass each value of this as a param, how do I do that please?
Thanks in advance.
How about this:
Scenario Outline:
Given url 'http://httpbin.org'
And path 'get'
And param foo = '<value>'
When method get
Then status 200
Examples:
| value |
| hello |
| world |
And in Karate 0.9.0 onwards you can do this:
Background:
* def data = [{ value: 'hello' }, { value: 'world' }]
Scenario Outline:
Given url 'http://httpbin.org'
And path 'get'
And param foo = '<value>'
When method get
Then status 200
Examples:
| data |
EDIT: and if you need to transform an existing primitive array:
Background:
* def array = ['hello', 'world']
* def data = karate.map(array, function(x){ return { value: x } })
Scenario Outline:
Given url 'http://httpbin.org'
And path 'get'
And param foo = '<value>'
When method get
Then status 200
Examples:
| data |
how to parameterize the item block in code :-
Scenario Outline: parameterization
* text query =
"""
{
"add":"Product",
"item":[
{"pn":"12345","qn":1,"m":"mk"}
]
}
"""
Given url baseURL
And request { query: '#(query)' }
And header Accept = 'application/json'
When method post
Then status 200
Examples:
| item_num |
| 12345 |
| 67890 |
Scenario Outline:
* def json = { add: 'Product', item: [{ pn: '<itemNum>', qn: 1, m: 'mk'}]}
* print json
Examples:
| itemNum |
| 12345 |
| 67890 |