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.
Related
How to differentiate test description for parameterized data testing with cucumber? Because for multiple testdata, the description in the scenario outline is showing same when viewing the cucumber report.
Below I am giving an example. On cucumber result, the scenario outline "Verify correct status displaying after filtering" -always visible as same for all the testdata. Is it possible to show three different description for three different testdata? Like "Verify correct verified status displaying after filtering" / "Verify correct pending status displaying after filtering" / "Verify correct rejected status displaying after filtering".
#flights
#flight01
Scenario Outline: TC003_Verify correct status displaying after filtering
Given I am in the xyz application
When I navigate to abcd page
Then Select status "<Status>" from the filter
Then Verify correct "<Status>" should be displayed
Examples:
|Status |
|Verified|
|Pending |
|Rejected|
The way to do that would be to add the parameter to your scenario outline the same way you do for your steps:
eg.
TC003_Verify "<Status>" status displaying after filtering for "<Status>"
This way your scenario results will better convey what you are testing too.
HTH.
The easy way is to just write 3 different scenarios with different titles. There is absolutely no need to use scenario outline when cuking.
When I tested for multiple test data,cucumber report shows the data itself and marked it with pass or fail colors, screenshots below . The description in scenario outline is same but it also copies all the data and it even provides the error. Imo, this eliminates the need to have separate scenario outline with data in it.
and
You have not mention which test runner are you using but the above screen shots are from cucumber 6.9.1 and Junit5 and in junit-platform.properties , you can mention cucumber.plugin = html:target/cucumber.html to have this report generated
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
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 want to print the name of each scenario as the test run. What can i call or do to get the name so that i can execute * print <scenario_name> ?
The answer for this post is exactly what i want to do: Print scenario name Is there a way to access the Scenario object?
As of now this is not supported, but will be easy to add. But here's the question - is this just to help you make sense of the logs ? Because if you are not using the Cucumber HTML report yet, you should - and that's what most teams are using: https://twitter.com/KarateDSL/status/899671441221623809
Refer to this discussion for more: https://stackoverflow.com/a/47555173/143475
If you still really need this, kindly raise a feature request.
Edit: this will be available in the next version: https://github.com/intuit/karate/issues/257
This question already has answers here:
Can we parameterize the request file name to the Read method in Karate?
(2 answers)
Closed 1 year ago.
I am using a JSON file which act as a test case document for my API testing. The JSON contain Test Case ID, Test case Description, Header and Request body details, which should be the driving factor of Automation
Currently i am looping a feature over this json file to set different header and body validations. However it will be helpful if i can set the Scenario name from JSON file while its iterating
Something like
serverpost.feature
Feature:re-usable feature to publish data
Scenario: TC_NAME # TC_NAME is avaliable in the JSON data passed to this feature. However, CURRENTLY ITS NOT TAKING THIS DATA FROM JSON FILE.
Given path TC_ID # TC ID is taken from JSON
Given url 'http://myappurl.com:8080/mytestapp/Servers/Data/uploadServer/'
And request { some: '#(BODY)' } # Request Body Details is taken from JSON
Please suggest
In my honest opinion, you are asking for a very un-necessary feature. Please refer to the demo examples, look for it in the documentation.
Specifically, look at this one: dynamic-params.feature. There are multiple ways to create / use a data table. Instead of trying to maintain 2 files - think of Karate as being both - your data table AND the test execution. There is no need to complicate things further.
If you really really want to re-use some JSON lying around, it is up to you but you won't be able to update the scenario name, sorry. What I suggest is just use the print statement to dump the name to the log and it will appear in the HTML report (refer to the doc). Note that when calling a feature in a loop using a JSON array, the call argument is ALREADY included the report, so you may not need to do anything.
Just an observation - your questions seem to be very basic, do you mind reading the doc and the examples a bit more thoroughly, thanks.