Can we pass dynamic values within values in Scenario Outline in Karate - api

Scenario Outline: User payload validation
Given url usermessagesAPI
* request
"""
{
"first_name" : "<first_name>",
"last_name" : "<last_name>",
"transaction_id" : "<transaction_id>,
"user_message" : "<user_message>"
}
"""
When method POST
Then assert responseStatus == 202
Examples:
|first_name|last_name|transaction_id|user_message|
|xyz|xyz|87690|<?xml version=\"1.0\" encoding=\"UTF-8\"?><MsgId>201060024</MsgId><CreDtTm>2020-04- 14T13:45:02</CreDtTm>|
|abc|abc|76565|<?xml version=\"1.0\" encoding=\"UTF-8\"?><MsgId>7858757</MsgId><CreDtTm>2022-04-14T13:45:02</CreDtTm>| */
In the above example, how do I pass random values for MsgId and CreDtTm which are within the XML message

The data within the Examples: is fixed and cannot be changed at run-time. You can however do modifications within the Scenario Outline block. So I think you can achieve what you want. Here is an example:
Feature:
Scenario Outline:
* def rand = java.lang.System.currentTimeMillis()
* xml payload = message
* karate.set('payload', '/root/CreDtTm', rand)
* print payload
Examples:
| message |
| <root><MsgId>201060024</MsgId><CreDtTm></CreDtTm></root> |
There are other ways to modify an XML, for example using replace: https://github.com/karatelabs/karate#replace

Related

Use map object in karate framework

I am trying to create a scenario where:
Scenario Outline: Create a request
Given print 'reason=<reason>, detail=<detail>, metainfo=<metainfo>'
When call create_request
Then match response.message == "#notnull"
* call json_to_proto request
* print 'response \n', response
Examples:
reason | detail | metainfo
test | Testing | { foo: bar }
My concern is metainfo is defined as a map, "metainfo": "#(karate.get('metainfo', {}))" how do I set values for it as the current logic gives me error: org.graalvm.polyglot.PolyglotException: Expect a map object but found...
Please read this section of the docs: https://github.com/karatelabs/karate#scenario-outline-enhancements
You can use JSON like this. And note that you don't need the <foo> placeholder system. Normal variables work:
Scenario Outline: ${payload.foo}
* match payload == { foo: 'bar' }
Examples:
| payload! |
| { foo: 'bar' } |

how to pass a string in parm request and loop it in my request path without saving to a file and creating table

so i created/def a object with
And def memeberIDs = $.users[*].id
And print memeberIDs
and i get a list like so
[
"eOVbGI0XSiabeiLZtB-ROQ",
"iK-Fz_NRSbSt-7AdodjDrA",
"dS2czwVFRZmy8a6oxO7JKw",
"yxH3bmF3THuCgLWLGh7eeQ",
"bqD41MgvSlC94EQH-r1H-Q",
"MrQihYjiR_WmWHA2-Y2cZQ",
"9N7uZdT_RTmePdJ5DMZ7sg",
"qo76PFcSS3-61FwgKCz6Ng",
"tjo3sdj3RMe8RoSBR3U_Ng",
"OuFxQR7KThCBPv2wHtKzhg",
"YmQKkv69Ts2yQ32LIjJN-g",
"A7kRauysQsCtle9G-bMV0g",
"zinHreHLTluUWdFzavssEA",
"UhZt8B_JSVaPBAJdIcPBsw",
"MtT_yR1iQqmDWMWlxXsWmQ",
"y2c9aK17Qouune_c_ZGlYQ",
"xGHOe7wxQru-NruIBPHL7Q",
"sTe8TADNTQ6nmg4UJeXiOQ",
"qC2FJUJeQJmOJPKkE_iehQ",
"4V0a93O8TCK-jjDujVVH-A",
"sk1tDsUmRYWUjyCMWFOqDw",
"ChmxrwHwTcS9I3u-RveBQA",
"ZL3uSx1oQbOc2c5qrFYRew",
"qjxFk-x9SVe-8XkzMbLrBw",
"7uIQVAXuSVe4tcWdv6S7MQ",
"V4mdzUj2SpyVpKlJfSdOEg",
"dU61-sNoQu6Q87hItkGcHw",
"enhFrWkfTDuaFqQCwPetAw",
"Txte_5FtTiaSfsc5k7-HmA",
"7tkMxEglS5qgx6bKObByKg",
"-uv0OuoNRlinnI4HkGsSSg",
"tLxWhEqSSiOcqyGJKsG6tQ",
"8tqnoZ1DQrq0DGzU_4OVmg",
"E9Rjy2euRbaKeaP1INxvGA",
]
My next request looks like
Given url 'https://api.someurl.com/v2/users//meetings?type=all&page_size=30&page_number=1'
Take some time and study this example. And read this part of the documentation: https://github.com/intuit/karate#dynamic-scenario-outline
Background:
* def list = ['foo', 'bar']
* def data = karate.mapWithKey(list, 'name')
Scenario Outline:
* url 'http://httpbin.org'
* path 'anything'
* param test = name
* method get
Examples:
| data |
This example will make 2 requests,
1) GET http://httpbin.org/anything?test=foo
2) GET http://httpbin.org/anything?test=bar

Polling in Karate based on JSON response [duplicate]

This question already has an answer here:
Karate framework retry until not working as expected
(1 answer)
Closed 2 years ago.
I am creating an animal into a database and then attempting to retrieve the animal which I have just created. However, there is a time lag ~5-10secs in the database. Therefore, a sleep wait is not suitable for this scenario as the response time varies. 
I would like to poll the message until animalId is returned in the array. It is also important that the requestId header is re-generated when the request is re-tried. 
What is the most elegant way of achieving this? 
Scenario:
Given path '/animals'
And header requestId = uniqueString(5)
When method post
Then status 200
* def animalId = response.animalId
Given path '/animals'
And header requestId = uniqueString(5)
When method get
Then status 200
{
"animals": [
{
"animalId": "12219958",
"reference": [
"12345"
]
}
]
}
* def animalDetails = karate.jsonPath (response, "$.animals.[?(#.reference[0]== '" + animalId + "' )]")[0]
* def animalId = '12345'
The retry until syntax is what you are looking for: https://github.com/intuit/karate#retry-until
EDIT: regarding this requirement:
It is also important that the requestId header is re-generated when the request is re-tried.
You have to configure headers with a JavaScript function, so that it is fired for each re-try. Otherwise there is no-other option to tweak the body, please manually write a polling loop if needed (also explained in the docs).
EDIT: here is a simple example to see for yourself how the foo header is dynamic for each re-try. Paste this in a fresh Scenario and it will work.
* def counter = { value: 0 }
* configure headers = function(){ return { foo: counter.value } }
* url 'http://httpbin.org/get'
* retry until counter.value++ == 2
* method get

Support passing from Scenario Outline to JSON file

I am going to reuse some features and calling multiple features in a Scenario Outline.
Since the features being called are common, we would like to define its parameters in its own parameter file, while the parameter values are defined in placeholder.
We hope the placeholder can get the value from the Outline Examples.
How to make it?
Feature: verify parameter passing
Scenario Outline: verify 2 calls
* def result1 = call read('baseFeature1.feature')
* def result2 = call read('baseFeature2.feature') result1
* print result2
Examples:
| fooValue |
| value1 |
| value2 |
Feature: feature to verify the parameter passing, no input parameter
Scenario: feature 1
Given def payload = read('classpath:feature_1.json')
* print 'feature 1' + payload
Given def result = { "barValue": "barValue"}
Feature: feature to verify the parameter passing, with input parameter from last step
Scenario: feature 2
Given def payload = read('classpath:feature_2.json')
* print payload
feature_1.json
{
"foo": "#(fooValue)"
}
feature_2.json
{
"foo": "fooValue",
"bar": "#(result1.barValue)"
}
I think the version currently in development will make this possible. Can you take a look at this GitHub issue and see if this addresses your question: https://github.com/intuit/karate/issues/717
It will also be great if you can build from source and try this new capability.
Scenario Outline: magic variables with embedded expressions
* def expected = __num == 0 ? { name: 'Bob', alive: false } : { name: 'Nyan', alive: true }
* match expected == { name: '#(__row.name)', alive: '#(__row.alive)' }
* eval karate.set(__row)
# you can read from a re-usable JSON file instead of the line below
* match expected == { name: '#(name)', alive: '#(alive)' }
Examples:
| name | alive! |
| Bob | false |
| Nyan | true |

How to pass the json list response of one feature file as parameter to another feature file

My requirement is, I want to pass the response of first feature file as input to second feature file. The first feature file response is a json list, so the expectation is second feature file should be called for each value of json list.
Feature:
Scenario: identify the reference account
* def initTestData = read('../inputData.feature')
* def accountData = $initTestData.response
* print “Account Details”+accountData // output of this is a json list [“SB987658”,”SB984345”]
* def reqRes = karate.call('../Request.feature', { accountData : accountData })
In Request.feature file we are constructing the url dynamically
Given url BaseUrl + '/account/'+'#(accountId)' - here am facing issue http://10.34.145.126/account/[“SB987658”,”SB984345”]
My requirement is Request.feature should be called for each value in ‘accountData’ Json list
I have tried:
* def resAccountList = karate.map(accountData, function(x){accountId.add(x) })
* def testcaseDetails = call read('../requests/scenarios.feature') resAccountList.accountId
Result is same, accountId got replaced as [“SB987658”,”SB984345”]
my I need to call Request.feature twice http://10.34.145.126/account/SB987658 http://10.34.145.126/account/SB984345 and use the response of each call to the subsequent feature file calls.
I think you have a mistake in karate.map() look at the below example:
* def array = ['SB987658', 'SB984345']
* def data = karate.map(array, function(x){ return { value: x } })
* def result = call read('called.feature') data
And called.feature is:
Feature:
Scenario:
Given url 'https://httpbin.org'
And path 'anything', value
When method get
Then status 200
Which makes 2 requests:
https://httpbin.org/anything/SB987658
https://httpbin.org/anything/SB984345