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

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

Related

How can I put several extracted values from a Json in an array in Kusto?

I'm trying to write a query that returns the vulnerabilities found by "Built-in Qualys vulnerability assessment" in log analytics.
It was all going smoothly I was getting the values from the properties Json and turning then into separated strings but I found out that some of the terms posses more than one value, and I need to get all of them in a single cell.
My query is like this right now
securityresources | where type =~ "microsoft.security/assessments/subassessments"
| extend assessmentKey=extract(#"(?i)providers/Microsoft.Security/assessments/([^/]*)", 1, id), IdAzure=tostring(properties.id)
| extend IdRecurso = tostring(properties.resourceDetails.id)
| extend NomeVulnerabilidade=tostring(properties.displayName),
Correcao=tostring(properties.remediation),
Categoria=tostring(properties.category),
Impacto=tostring(properties.impact),
Ameaca=tostring(properties.additionalData.threat),
severidade=tostring(properties.status.severity),
status=tostring(properties.status.code),
Referencia=tostring(properties.additionalData.vendorReferences[0].link),
CVE=tostring(properties.additionalData.cve[0].link)
| where assessmentKey == "1195afff-c881-495e-9bc5-1486211ae03f"
| where status == "Unhealthy"
| project IdRecurso, IdAzure, NomeVulnerabilidade, severidade, Categoria, CVE, Referencia, status, Impacto, Ameaca, Correcao
Ignore the awkward names of the columns, for they are in Portuguese.
As you can see in the "Referencia" and "CVE" columns, I'm able to extract the values from a specific index of the array, but I want all links of the whole array
Without sample input and expected output it's hard to understand what you need, so trying to guess here...
I think that summarize make_list(...) by ... will help you (see this to learn how to use make_list)
If this is not what you're looking for, please delete the question, and post a new one with minimal sample input (using datatable operator), and expected output, and we'll gladly help.

How to generate more than one random UUID and use in scenario outline examples in karate [duplicate]

This question already has an answer here:
How to use cucumber table when it is code driven
(1 answer)
Closed 1 year ago.
I am new to Karate API, pardon me for the mistakes if any.
I want to generate multiple random UUID and then use them in scenario outline examples
Example:
Background:
def UUID = function() {return java.util.UUID.randomUUID() + ''}
Scenario outline: to do post call
Given url 'http://localhost:8080'
def UID = UUID()
print UID
And request {CID:"", name :""}
When method POST
Then status 201
Examples:
|CID| name|
|UID1| james|
|UID2| rahul|
Here in above 'Examples' I wanted to use randomly generated UUID in data table of examples so that I can run multiple scenarios for UUID with one POST API call.
First question: How can I generate multiple random UUID ?
Second question: once multiple UUID gets generated how can i call in scenario outline examples and use them?
Can anyone suggest me on this?
Please try running the following simple example.
Feature:
Background:
* def uuid = function(){ return java.util.UUID.randomUUID() + '' }
Scenario Outline:
* url 'https://httpbin.org/anything'
* param foo = uuid()
* request { item: '#(item)' }
* method post
Examples:
| item |
| first |
| second |
It will make 2 requests, and each request will use a different param called "foo" and the URL will be like this:
https://httpbin.org/anything?foo=c1b6ab3d-5952-413b-827c-d9579a0a93b6
So it is simple. Think of the Examples: as like a "loop". Each time the Scenario Outline runs, we are calling the uuid() function again, which will return a different, random value.

Karate- Good way to send custom headers with each request

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

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