how to call a feature file to generated a Get request , for each element in an ArrayList or JsonArray ( Created by Java function ) - karate

basically I want to achieve below result , where the list used in Examples: is dynamic
Scenario Outline:
def ss = 'https:testingurl/'+ < spaceCode > + 'trailPath';
Given url ss
And header Authorization = autGetToken()
When method get
Then status 200
Examples:
| spaceCode |
| space1|
| spac2|
| spaceAbc05|
| space.o2|
| spacesacc|
| spacere |
So I created one.feture and tried to call it from other feature file
one.feature
Background:
def ss = 'https:testingurl/'+ < spaceCode > + 'trailPath';
Scenario: need to run this scenario for each member of the spaceList ArrayList / JsonArray
Given url ss
And header Authorization = autGetToken()
When method get
Then status 200
* print response
* def count = response.value.length
* print count
* karate.set('total', karate.get('total') + karate.get('count') )
* print total
question
how to run above file for each element of the "spaceList" which is an Java ArrayList, i am creating this ArrayList by calling method getSpaceList()
i have tried to call it from other feature file like below
Method1
caller.feature
Background:
* def total = 0
* def helper = Java.type('shared.Helper')
Scenario Outline: calling other feature file for each element of the spaceList Array
def result = call read('one.feature')
Examples:
| spaceCode |
| helper.getSpaceList() |
Method2
caller.feature
Background:
* def total = 0
* def helper = Java.type('shared.Helper')
* def spaceList = helper.getSpaceList() // this returns around 20 different elements from a sql
Scenario Outline: calling other feature file for each element of the spaceList Array
def result = call read('one.feature')
Examples:
| spaceCode |
| spaceList |
i have also tried karate.forEach and karate.setUp

Try this example. Note how it will make two POST requests. Once you understand this, you should be able to do what you want.
Feature:
Scenario Outline:
* url 'https://httpbin.org/anything'
* request __row
* method post
* status 200
Examples:
| [{ a: 1 }, { a: 2 }] |

Related

Is it possible to dynamically change an url with values in the array without storing the values in separate feature file

I need to array values say 10 values from the previous url response to get replace in the next url
*def k = [1,2,3]
Given url 'https://www.' + k + 'zzzz'
Tried this:
*def k = [1,2,3]
Given url 'https://www.' + k[*] + 'zzzz'
Expecting:
Given url 'https://www.+ 1 + zzzz'
need its response to store
Given url 'https://www.+ 2 + zzzz'
need its response to store
Given url 'https://www.+ 3 + zzzz'
need its response to store
Yes, in Karate 1.3.1 onwards: https://github.com/karatelabs/karate#setup
Try the below and see it work.
Feature:
#setup
Scenario:
# do some http stuff here and prepare the data to use in the loop
* def data = [{ myNum: 1 }, { myNum: 2 }, { myNum: 3 }]
Scenario Outline:
* url `https://httpbin.org/anything/${myNum}`
* method get
Examples:
| karate.setup().data |

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

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

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

Object.keys(data) Js fu not worked when pass Json object as def in Karate 0.9.5.RC5

Js File with following function
function fn(config) {
config.OAuth2 = function (data) {
var keys = Object.keys(data);
return keys;
}
return config;
}
Feature file with following steps
Feature: Sample Feature
Scenario Outline: Sample Scenario
#This is works
* def a = OAuth2({firstName: '<firstName>',lastName: '<lastName>'})
* print a
#This do not work
And def req = {firstName: '<firstName>',lastName: '<lastName>'}
* def b = OAuth2(req)
* print b
Examples:
| firstName | lastName |
| a | a1 |
Giving error at var keys = Object.keys(data);
javascript evaluation failed: OAuth2(req), TypeError: {firstName=a, lastName=a1} is not an Object in <eval>
Please don't use Object.keys() - use karate.keysOf() instead.

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 |