On karate 0.6.0, the following code returned an array with all the ids:
def get = call read('wishlist-products-get.feature') id
def wishlist = get.response.wishlist_products
ids = wishlist[*].product_info.id
now on version 0.9.0 the same returns the following error:
wishlist[*].product_info.id, :1:9 Expected an operand but found *
Can someone tell me what change?
Thanks!
You must use the get keyword to save the results of a JsonPath expression as described in the docs.
* def ids = get wishlist[*].product_info.id
Related
I'm asserting a response like this using karate.
"Ids": ["123456","123456","123457"]
Now I want to assert that my list doesn't contain the duplicate values (If there is a duplicate value, it should fail the test-case), is there any in built function which is supported by Karate or is there any JS who does the trick?
Here you go: https://github.com/karatelabs/karate#karate-distinct
* def response = ["123456","123456","123457"]
* match response == karate.distinct(response)
Karate version - 1.0.0
I want to get the queryparams and url and want to concat it and save it to a variable.. I'm using following syntax which doesn't work. when I use param in variable it says param not defined. Does anyone have any work around for this?
When I use the following -
Given path '/test/1'
And param product = "abc"
And param country = "usa"
* print param
org.graalvm.polyglot.PolyglotException: ReferenceError: "param" is not defined
How do I concat url and param and get in a variable.
Many thanks!
Following is the example with the corresponding request url & query params for which you can execute this scenario to get the response and GET Request as,
Scenario: Sample File
* def ScenarioEngine = Java.type('com.intuit.karate.core.ScenarioEngine');
Given url 'https://reqres.in'
And path '/api/users'
And param page = 2
And param pages = 1
When method get
And print ScenarioEngine.get().getRequest().getUrl()
For which the response would be printed along with the GET url and query param as,
[print] https://reqres.in/api/users?pages=1&page=2
We have a feature A with several scenarios. And we need one scenario from that file. Can we call it in our feature B?
No. You need to extract that Scenario into a separate*.feature file and then re-use it using the call keyword.
EDIT: Karate 0.9.0 onwards will support being able to call by tag as follows:
* def result = call read('some.feature#tagname')
To illustrate the answer from Karate-inventor Peter Thomas with an example:
Given a feature-file some.feature with multiple scenarios tagged by a tag-decorator:
#tagname
Scenario: A, base case that shares results to e.g. B
// given, when, then ... and share result in a map with keyword `uri`
* def uri = responseHeaders['Location'][0]
#anotherTag
Scenario: X, base case that shares results to e.g. B
// given, when, then ... and share result in a map with keyword `createdResourceId`
* def createdResourceId = $.id
In another feature we can call a specific scenario from that feature by its tag, e.g. tagname:
Scenario: B, another case reusing some results of A
* def result = call read('some.feature#tagname')
* print "given result of A is: $result"
Given path result.uri + '/update'
See also: demo of adding custom tags to scenarios
Example:
Scenario: test
* def response =
"""
[
"YEN01",
"DP258",
"SA661",
"BT202",
"UR809"
]
"""
* def subset = response.[:2]
* print subset
I tried response..[:2] . and also tried with enclosing in ().
Let me know if any one got this working.
Just adding one character will fix your problem !
* def subset = $response.[:2]
Karate defaults to JavaScript and when you want JsonPath evaluation you need to give a little hint to Karate. This is explained in the docs: https://github.com/intuit/karate#get-short-cut
We have a feature A with several scenarios. And we need one scenario from that file. Can we call it in our feature B?
No. You need to extract that Scenario into a separate*.feature file and then re-use it using the call keyword.
EDIT: Karate 0.9.0 onwards will support being able to call by tag as follows:
* def result = call read('some.feature#tagname')
To illustrate the answer from Karate-inventor Peter Thomas with an example:
Given a feature-file some.feature with multiple scenarios tagged by a tag-decorator:
#tagname
Scenario: A, base case that shares results to e.g. B
// given, when, then ... and share result in a map with keyword `uri`
* def uri = responseHeaders['Location'][0]
#anotherTag
Scenario: X, base case that shares results to e.g. B
// given, when, then ... and share result in a map with keyword `createdResourceId`
* def createdResourceId = $.id
In another feature we can call a specific scenario from that feature by its tag, e.g. tagname:
Scenario: B, another case reusing some results of A
* def result = call read('some.feature#tagname')
* print "given result of A is: $result"
Given path result.uri + '/update'
See also: demo of adding custom tags to scenarios