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
Related
I just started to use Karate Gatling for performance tests and facing following problem:
I have a call for the search and would like to evaluate different types of search depending on the parameter e.G. https://example.com/search/facetedSearch
'*'
'keyword1'
'keyword1, keyword2' etc.
The feature file looks something like this:
#performance
Feature: Search
Background:
* url 'https://example.com/'
Scenario Outline: Search -> Simple search for a single word
Given path '/search/facetedSearch'
And param facetedSearchAdditionalFilter[searchAreaID] = -1
And param facetedSearchAdditionalFilter[searchKey] = '<SearchTermSimple>'
When method post
Then status 200
And assert iNumHits >= iNumHitsExpected
Examples:
| read('../testData/performanceTestData.csv') |
Scenario: Search -> Simple search for *
Given path '/search/facetedSearch'
And param facetedSearchAdditionalFilter[searchAreaID] = -1
And param facetedSearchAdditionalFilter[searchKey] = '*'
When method post
Then status 200
And assert iNumHits >= iNumHitsExpected
Scenario Outline: Search -> Search for multiple words
Given path '/search/facetedSearch'
And param facetedSearchAdditionalFilter[searchAreaID] = -1
And param facetedSearchAdditionalFilter[searchKey] = '<SearchTermMultiple>'
When method post
Then status 200
And assert iNumHits >= iNumHitsExpected
Examples:
| read('../testData/performanceTestData.csv') |
I would like to evaluate different types of search separately, as the performance is significantly different. What gatling does - it aggregates all different types of search in one result - "POST /search/facetedSearch".
Is there a possibility to let evaluate every type of search individually in one run?
Thanks in advance,
Sergej
Yes, refer the docs on using a custom nameResolver: https://github.com/karatelabs/karate/tree/master/karate-gatling#nameresolver
For your case you should be able to call req.getParam("facetedSearchAdditionalFilter[searchKey]")[0] or something similar. Or you could choose to use an additional header.
I am testing a search API. I need to send different query params depending on the test case name. Code like below works, but it sends both params for all test cases.
Given path '/search'
And param merchantId = 'abc'
And param email = 'abc#gmail.com'
I want one param to be sent with one test case and another param with another test case. So I tried like below, but the question mark after "search" gets encrypted and sent to the server.
* if('<testcaseName>' == 'search by merchantId') karate.set('pathVar','/search?merchantId=' + merchantId);
* if('<testcaseName>' == 'search by email') karate.set('pathVar','/search?email=' + email);
Given path pathVar
Is there any other way to send them?
Use the params keyword: https://github.com/intuit/karate#params
Any null values will NOT be sent. See this example: dynamic-params.feature
So you can do something like this:
Given path '/search'
And params { merchantId: '#(a)', email: '#(b)' }
So depending on the values of the variables a and b you can control your test the way you want.
i have one response as below
"data":{"books":{"isbn":"97895113132","Bookid":10196333}}
sometimes i will get Bookid as null.
my question is, i have to take this bookid only if it is not null, and then pass to next api as query parameter.
please help to find the solution for this using karate framework
Easy, if you use the params keyword, any value of null is not sent as a param. You can try this:
* def temp = null
Given url 'http://httpbin.org'
And path 'get'
And params { foo: '#(temp)' }
When method get
Then status 200
If you are looking for conditional-logic - please read: https://github.com/intuit/karate#conditional-logic
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
I need to pass multiple query parameters with the same name in a URL, but I am having problems getting it to work with Karate. In my case, the URL should look like this:
http://mytestapi.com/v1/orders?sort=order.orderNumber&sort=order.customer.name,DESC
Notice 2 query parameters named "sort". I attempted to create these query string parameters with Karate, but only the last "sort" parameter gets created in the query string. Here are the ways I tried to do this:
Given path 'v1/orders'
And param sort = 'order.orderNumber'
And param sort = 'order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/orders'
And params sort = { sort: 'order.orderNumber', sort: 'order.customer.name,DESC' }
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/order?sort=order.orderNumber&sort=order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
The first two ways provide the same query string result: ?sort=order.customer.name%2CDESC
The last example does not work because the ? get encoded, which was expected and explained in this post - Karate API Tests - Escaping '?' in the url in a feature file
It's clear that the second "sort" param is overriding the first and only one parameter is being added to the URL. I have gone through the Karate documentation, which is very good, but I have not found a way to add multiple parameters with the same name.
So, is there a way in Karate to set multiple URL query parameters with the same name?
Yes you can generate multiple query parameters with the same name in karate
All values of similar key should be provided in an array.
Given path 'v1/orders'
And params {"sort":["order.orderNumber","order.customer.name,DESC"]}
And header Authorization = authInfo.token
And method get
Then status 200
And for setting single parameter using param it will be like
And param sort = ["order.orderNumber","order.customer.name,DESC"]