Calling Karate feature files based on SOAP response status code 200 or 500 - karate

I have a requirement where I needed to call multiple feature files based on status code 200, 201, 500 etc. I tried few examples listed in stackoverflow as well but didn't help.
E.g.
Let's say I created 3 feature files. File1.feature, File2.feature & File3.feature.
1) File1.feature may give response status code 200 or 500
2) Based on response code 200, I need to call File2.feature to do certain tests.
3) Based on response code 500, I need to call File3.feature to do certain tests.
Appreciate your help on this.

After any request, there is a "magic variable" called responseStatus that will hold the HTTP status code value. You can easily use it to do conditional calls.
* if (responseStatus == 200) karate.call('some.feature')
Refer: https://github.com/intuit/karate#conditional-logic

Related

Can we use two status validation like for example * match 200 and 208 at same time in karate api testing automation [duplicate]

I am using Karate to test other people's implementations of an API. When checking response status codes I often need to accept more than one response. For example in response to a PUT I may see 200, 201, 205 - they are equally valid. I know I can use
Then assert responseStatus >= 200 && responseStatus < 300
to check for success but the shortcut really helps readability of the tests.
Would you consider an enhancement to the language to support response classes such as:
success (meaning 200-299)
redirect (meaning 300-399)
fail (meaning 400-499)
2xx
3xx
4xx
If I were to look at submitting a PR for this would you agree it is useful and would you have a preferred mechanism? Would these classes be best as parsed symbols or strings that force a different match to be implemented when it detects the status is not a number?
Yes, my first reaction is not to add a new keyword. Also to be honest, this seems to be a rare requirement - never had this ask before, I guess API testing would generally mean predictable responses.
My proposal is that you can write a custom function:
* def statusSuccess = function(){ var status = karate.get('responseStatus'); return status >= 200 && status < 300 }
* url 'https://httpbin.org'
* path 'status', 200
* method get
* assert statusSuccess()
EDIT - also see this: https://twitter.com/KarateDSL/status/1364433453412851714

Can I use the status shortcut in Karate to check a response class instead of just one code

I am using Karate to test other people's implementations of an API. When checking response status codes I often need to accept more than one response. For example in response to a PUT I may see 200, 201, 205 - they are equally valid. I know I can use
Then assert responseStatus >= 200 && responseStatus < 300
to check for success but the shortcut really helps readability of the tests.
Would you consider an enhancement to the language to support response classes such as:
success (meaning 200-299)
redirect (meaning 300-399)
fail (meaning 400-499)
2xx
3xx
4xx
If I were to look at submitting a PR for this would you agree it is useful and would you have a preferred mechanism? Would these classes be best as parsed symbols or strings that force a different match to be implemented when it detects the status is not a number?
Yes, my first reaction is not to add a new keyword. Also to be honest, this seems to be a rare requirement - never had this ask before, I guess API testing would generally mean predictable responses.
My proposal is that you can write a custom function:
* def statusSuccess = function(){ var status = karate.get('responseStatus'); return status >= 200 && status < 300 }
* url 'https://httpbin.org'
* path 'status', 200
* method get
* assert statusSuccess()
EDIT - also see this: https://twitter.com/KarateDSL/status/1364433453412851714

How to pass multiple json records using data driven approach in Karate DSL?

We have gone through the Karate documentation where we can compare the exact JSON object as a response (which contains multiple data records) but how can we pass and read the JSON in a single scenario?
Below is my sample.JSON and I want to read this in the request payload.
[{"name":"John","salary":"10000","age":"25"},
{"name":"Maria","salary":"20000","age":"27"}]
I have tried the JSON structure in the above format, however, I am getting below exception. Kindly help me in this.
status code was: 400, expected: 200, response time: 4315
Kindly suggest how to read and pass it in request payload of single scenario.
Thank you.
Status code 400 means you have made some other mistake with the request. Karate is working fine, it is just an HTTP client, maybe the request was not in the "shape" the server was expecting. Talk to the server-side team if you can or check the documentation of the API.
Here is a simple example that works, paste it and try:
* def body = [{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]
* url 'https://httpbin.org/post'
* request body
* method post
* status 200
EDIT: for looping, please read the documentation.
The below example is just one way of doing it - please identify what best you are comfortable with: https://github.com/intuit/karate#data-driven-tests
Feature:
Background:
* def data = [{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]
Scenario Outline:
* url 'https://httpbin.org/post'
* request __row
* method post
* status 200
Examples:
| data |

How to check if all status codes are 200 in Mulesoft 4?

Say for example, I created a flow for scatter-gather and I want to check if all endpoints are returning same result status code 200 or throw an error if not.
Configure the Response Validator (General > Response > Response Validator) for each HTTP Request so only 200..299 responses are considered valid.
You can use try block for every HTTP request on wrap whole scatter gather. If one fails, capture HTTP status code in on error propogate and log the results.
I suggest you wrap each request into try block, if you already have a global error handler defined, it should pick up status code 500 etc. Otherwise, capture response code into dataweave

How to retry a POST call based on response code, how and what are the ways to capture response data in Apache Jmeter?

1) Applying retry POST/auth based on response status if its 302, call GET/token and all other otherwise exit? Want to atleast retry POST call for 3 times.
Have While controller+CSV data set with username,password and accountid with 5 different test users.
Have a -Thread group
|-GET/auth
|POST/auth
|GET/token
|GET/profile/profileid
|GET/profileid/address
2)How and ways to Capture profileid from response of GET call?Don't want to save it into CSV. But more prefer into console.