I have a requirement in my project to run a Scenario file on two different urls. All the request headers for both the URL's are exactly the same.
Planning to use similar to below
Scenario Outline: Test
Given url <testurls>
And path 'test'
When method GET
Examples:
|testurls |
|'https://test1.com' |
|'https://test2.com' |
Is there any other better way to handle it in Karate ?
This should be the simplest way.
Related
This question already has an answer here:
How do I invoke my JS Function for a particular column in a csv file in the Karate Framework?
(1 answer)
Closed 1 year ago.
Trying to pass dynamic path parameters from CSV for a GET method.
Referring to Karate: how to pass dynamic path parameters?
we are trying to pass dynamic parameter in path from a CSV file, but it doesn't work well when method is GET, although for POST we don't have an issue using CSV.
here elaborating with help of a open API
Feature: Get users details based on valid and invalid inputs from csv file
Background:
* url 'https://reqres.in'
Scenario Outline: Get single user based on id value - Valid & invalid
Given path '/api/users/<id>'
When method <method>
Then status <status>
* print response
##* match response.data.id contains <id>
Examples:
|id|status|method|Scenario|
|2|200|GET|Valid id as input|
|4|200|GET|Valid id as input|
|5|200|GET|Valid id as input|
|&|404|GET|Special character as Input|
| |404|GET|Empty space as Input|
|A|404|GET|Alphabet as input|
|999|404|GET|Invalid Number as input|
|-1|404|GET|Invalid Number as input|
This works well, but when I try to pass same data with following it doesn't work
|read('../../Data/users.csv')|
this is how this CSV looks
users data csv file
It is hard to say from the data given, and it doesn't work is not helpful. Maybe special characters cause problems. The best thing would be to follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
In general I discourage using a Scenario Outline for negative cases. Some guidance can be found here: https://stackoverflow.com/a/54126724/143475
To troubleshoot the CSV, you can try * print __row to see the values in each row.
This works for me:
Scenario Outline:
* url 'https://httpbin.org'
* path 'post'
* request __row
* method post
Examples:
| read('test.csv') |
I have a use-case where I need to first call an API to get a list of ID's. From this response a random ID is chosen. Next I call a 2nd API which uses the random ID as a component in the path.
It's possible the 2nd API call can return an empty response, therefore I want to utilize retry until but use a different random ID in the path per retry iteration.
I've tried a couple of things:
First "in-lining" the JS function in the path to get a random ID:
Given path firstPart, myGetRandomId(idList), lastPart
And retry until response.length > 1
Second, tried putting the JS function in a Examples: as part of a Scenario Outline:
Given path firstPart, <ID>, lastPart
And retry until response.length > 1
Examples:
| ID |
| myGetRandomId(idList) |
The general issue I can't figure out is how to get the JS function to evaluate in either of this "in-line" kind of approaches.
And ideas/suggestions appreciated.
The way that the Karate retry until works is that it will re-play the request as-is and you can't modify it.
So you have to take a different approach. Use a JS loop. Look at this example in the demos:
https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/polling/polling.feature
Suppose I have test scenario with exact same requirements but one path variable change as follows:
Scenario: Some scenario
Given path /mypath/param1
When method get
Then status 200
I need to run the same test for /mypath/param2, /mypath/param3 as well.
Is there a simpler way to do it without requiring to separate the feature into a separate file and using data driven test?
Yes, use the Scenario Outline which is a standard Cucumber pattern.
Scenario Outline: Some scenario
Given path /mypath/<param>
When method get
Then status 200
Examples:
| param |
| foo |
| bar |
Xmlsample.feature
Feature: test A
Scenario: test apple
* table test_apple_one
| payload_file |
| 'sample/ball.xml' |
* def result = call read('classpath:........./samplereq.feature') test_apple_one
Jsonsample.feature
Feature: test B
Scenario: test Mango
* table test_Mango_one
| payload_file |
| 'sample/cat.`enter code here`json' |
samplereq.feature
Feature: sample
Background:
#Common Config load
* def sampleURL = baseURL+'/sample/test'
* xml payload = read('classpath:.....'+payload_file)
#OAuth Signature generator
* def authorization = "oauth string"
Scenario: Make the sample API call
Given url sampleURL`enter code here`
Given header Authorization = authorization
Given request payload
And header Content-Type = 'application/xml;charset=UTF-8'
And header Host = host
And header Accept = content_type
When method post
Then match header Content-Type contains 'application/xml'
I am using the above feature samplereq.feature for the XML API request and
I Want to keep this feature samplereq.feature as generic and use the same for the Json Api request.can I do the same with JSON(Jsonsample.feature) API request ,please suggest
I read your question multiple times and I am sorry I can't understand what you are trying to do at all. Also I think you have not understood Karate properly. So please listen to my suggestion and take it in the right spirit.
I think you are trying to un-necessarily complicate your test. My sincere suggestion is - please don't try for this extreme re-use. I have observed that when teams attempt to create a super-generic re-usable test script - it just complicates things and becomes hard to maintain.
So please use different feature files for JSON and XML. For each test you can have multiple scenarios. Now, the scenario data can be the same for JSON and XML and you can read a common JSON file. Refer to this example on how you can create XML out of JSON: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml.feature
If you still insist on the JSON and XML both in a generic feature, please take a look at this example: https://github.com/intuit/karate/tree/master/karate-demo/src/test/java/demo/loopcall
The above also has an example of calling a JavaScript function. Also please read the docs carefully.
I am running into this situation (similar to this) when trying to do the following:
Scenario: Create User
Given path 'user'
And request read('user.json');
When method post
Then status 200
And def user_id = response.userId
Scenario: Test A
Given path 'user/' + user_id <- Received javascript error here
...
Scenario: Test B
Given path 'user/' + user_id <- Received javascript error here
...
Scenario: Test A
Given path 'user/' + user_id <- Received javascript error here
...
Basically what i am trying to do is create a user in my database first, then run it through a series of test and use a last scenario to delete this user. Therefore, i need to share the user_id value across multiple scenarios. Background doesn't work for me because that means i have to create a new user for each scenario. I have seen in the demo that a simple way would be to put all test under 1 scenario but i don't feel that it is right to put all tests on 1 scenario
I have reviewed the karate demo but i did not come across any sample code that will help my situation. May i know the right way to do this in Karate? Thanks.
I think you are missing the callonce keyword. Understand it and then look at the demos, for example, this one: callonce.feature.
You will need to move the 'common' code into a separate feature, but that is the right thing to do, because you will typically want it re-usable by multiple feature files as well.