Karate- Good way to send custom headers with each request - api

I have a scenario where I am doing a GET call and on my table I have parameters defined. Now for the headers, I want to purposefully send incorrect values to make sure the test fails as expected. Not really sure how to do it though.
I have the following setup:
* table input
| machine | version | osSystem | status |
| machineName| version | windows | 401 |
My secondary file that the above calls on, looks like this:
Given url env
And path path
And header Content-Type = 'application/xml; charset=UTF-16'
And header Accept = '*/xml; charset=UTF-16'
And header key = key
When method GET
In above, I want to send bogus values for "key" header. Total of six bogus values (random alpha string, random number string, random alphanumerical value, random guid, etc). I have obviously tried entering the values as a json under "and header key = {}" but how do I make each request run EACH header per request, instead of running them all in one request?
Thank you in advance!

Try this example and observe how it works, it will answer all your questions:
Scenario Outline:
* url 'https://httpbin.org/anything'
* header foo = header
* method get
Examples:
| header |
| one |
| two |
And for an alternate way to "loop" refer to this answer: https://stackoverflow.com/a/69422451/143475

Related

Forcing a null value as a parameter does not get included in the call [duplicate]

This question already has an answer here:
The json key is not being omitted where the cells are empty in examples of Scenario Outline .What change needs to be done?
(1 answer)
Closed 1 year ago.
I read in the Karate documentation that if one uses "null", it will result in the key being omitted from the call. However, if one still wants to force a null, they could wrap it in parentheses like: "(null)".
It does go through with the key. However, on the actual call, the key is still omitted from the parameters. How can I force it there as well? Is it LITERALLY the same thing as an empty string?
I.e:
| Environment| Application| Version | status |
| Environment| Application| (null) | 401 |
call read....
Results:
{
"Environment": "Environment",
"Application": "Application",
"Version": "null",
"status": 401
}
But the URL looks like:
GET https://?Environment=Environment&Application=Application
Please advise.
EDIT below for a better example:
In my "primary" file, I have the following:
* table requestTable
| q | um | ie | status |
| (null) | 1 | utf | 400 |
Where all three params (q, um, ie) are required parameters.
My "secondary" runner file, looks like the following:
* def requiredParams = { q: '#(q)', um: '#(um)', ie: '#(ie)'}
* def mainUrl = 'https://www.google.com'
* def apiPath = '/search'
Given url mainUrl
And path apiPath
And params requiredParams
When method GET
Then status 400
I am expecting a 400 since a required param is required. It doesn't get sent as "null", even if I use (null), it removes that field altogether in the actual call. This example obviously won't work in real life since I am using example google stuff. I apologize about that. If you still need a real life example, please show me how I can do it.
I assume that you are talking about Examples: but your question is incomplete.
Try this: https://github.com/karatelabs/karate#scenario-outline-enhancements
Examples:
| Version! |
| '' |

send array of URL param via karate api

does karate support send an array of parameter in URL param as one of my API was working as this way. Below was one of my API test url concept. It working fine when i do manually on postman. Not sure whether karate support this kind of format or not.
https<URL>?param={"firstname":"XXX","lastname":"XXX"....}
i have tried with param and params. however params will give me & instead of ,. while in param , it will show ?param=%7B%firstname%22%3A%22abc...
Karate can support it, I am still not clear what your request is from your question but let me try. Note that as per the HTTP spec - some special characters WILL be URL-encoded.
Try these:
And param param = '{"firstname":"XXX","lastname":"XXX"}'
And param firstName = ['XXX', 'XXX']
See this demo example for more ideas: params.feature
EDIT: if you need to create dynamic JSON that is possible, please read the docs for the set keyword. And below the JSON is being converted to a string - because that is what it looks like from your description (which I really doesn't make sense to me)
* set data
| path | value |
| firstName | 'XXX' |
| lastName | 'XXX' |
* string data = data
* param param = data

How to pass variable into the title of a scenario/scenario title in Karate framework

While using data-driven feature in Karate framework, I see the generated report just show the title as configured in Scenario Outline NOT attached the value using in Example table. It causes the Tester confuse which data is using, and take time to expand each scenarios to know which data is using; so I want the report can pass variable into the title - Scenario/Scenario Outline. Please take a look at the example below.
E.g.
Feature: Login Feature
Background:
* configure headers = { 'Webapp-Version': '1.0.0'}
Scenario Outline: As a <description> user, I want to get the corresponding response_code <status_code>
Given def path = 'classpath:features/Authentication/authentication.feature'
And def signIn = call read(path) {username: '<username>', password: '1234567890'}
Then match signIn.status == <status_code>
Examples:
|username | status_code| description |
|test#gmail.com | 200 | valid user |
|null | 400 | invalid user|
My expected result, the generated report should fill the value on table for field "status code" and "description" fields.
-> As a valid user user, I want to get the corresponding response_code 200.
Please share your ideas and comments on it.
Thanks,
Learn.
Not supported. Just use the print syntax and you will see it in the report.
EDIT: okay this will be possible in the next version: https://github.com/intuit/karate/issues/553

How to put a value in Scenario Outline Examples (karate framework)?

In my feature I have:
* def loc = responseHeaders['location'][10]
* def id = loc.substring(loc.lastIndexOf('/') + 1)
And I would like to use id in scenario outline examples:
Scenario Outline: fkdfslqknfd
Given url 'foo.com'
And path <bar>
When method get
......
Examples:
|bar |
|(id)|
|"id"|
|'id'|
|id |> The last example is ok.
But instead of receiving 'foo.com/13' (assuming that id is 13) I have 'foo.com/id'. I tried with #, but it doesn't work. How I can replace this id? I need to test this id put in String format. Thanks
At least within JSON parameter, it was working for me
Examples:
| request_body |
| {username: '#(email)', password: '#(password)'} |
This is a known limitation of Cucumber, that the Examples cannot be dynamic. Refer to this last paragraph of the documentation: https://github.com/intuit/karate#the-karate-way
If you are really trying to loop over a feature with different values, again, refer to the above doc, and there are plenty of examples if you look around. Look at all the ones that start with call- here: https://github.com/intuit/karate/tree/master/karate-demo

How to use data in a file with postman?

I have a list of IDs and Values in a file and would like to use postman to connect to an API and update the records found in this file.
I tried with the Runner but am stuck in writing the syntax.
The answer is pretty simple and very well explained on this page
You can start with the a basic "put/post" - try to modify one single data set with static values to determine how the final query needs to be build. In my case the API accepted only RAW JSON formated data payloads.
As soon as you have your static postman query running - you can start automating it by determining which parts should be replaced. This data should be found in a data file (JSON or CSV). The schema is important for postman to understand the data. As reference I state the example as if I would like to replace an ID and a Value. My data document has one more column which is not a problem.
+--------+--------+--------+
| id | email | value |
+--------+--------+--------+
| data 1 | data 1 | data 1 |
+--------+--------+--------+
| data 2 | data 2 | data 2 |
+--------+--------+--------+
| data 3 | data 3 | data 3 |
+--------+--------+--------+
Column two (aka email) will be ignored and not be used. Notice how "id" and "value" are written in the header.
I would like to replace the ID which needs to be attached to the API endpoint and like to update a value which is within the dataset of this ID. Replacing the static parts with variables like {{variable}} allows Postman to understand that it needs to fill dynamic data here.
Notice that the variable attached to the URL says that it is not defined in the environment - if you did not set it up in the environment, this is correct and will work with data files.
I used simple tests to confirm if the data of the file made it into my query:
tests["URL has ID"] = responseURL.has(data.id);
tests["Body contains SFID"] = responseBody.has(data.value);
If you reach this point - all there is left to do is to go to the runner page, select the query to run, add the data file (you should preview if everything looks okay) and run it.