Refer defined variable in Scenario outline example [duplicate] - karate

This question already has answers here:
Can we parameterize the request file name to the Read method in Karate?
(2 answers)
Closed 1 year ago.
Feature: Test Type
Background:
* url host
* def name = 'test_name'
* def label = name
Scenario Outline: Test 2
Given url homeLinks.groupTypesUrl
And headers { tenant: #(tenantId), Authorization: #(authToken) }
* def name = <name>
* def description = <description>
* def label = <label>
* json data = read('path/to/file/create_group_type_request.json')
And request data
When method POST
Then status 400
Examples:
| name | label | description |
| '\u0000' | 'label' | 'description' |
| #(name) | '\u0000'| 'description' |
I need to refer global name defined inside examples map. How to get that reference?
Getting Javascript evalution error when I tried to like above piece of code.

Yes, Examples do not support JS eval and variables. Use the table form and loop over it with a call to a second feature: https://github.com/intuit/karate#calling-other-feature-files
Or you can try to use a dynamic Scenario Outline by initializing the table in the background: https://github.com/intuit/karate#dynamic-scenario-outline

Related

Karate : Dynamic text value in xml file

I want to provide dynamic values in the XML
test.xml:
<name>
<first>#(first)</first>
<last>#(last)</last>
<version>this is the #(version) in the file</version>
</name>
I have a .csv file:
first,last,version
abc,pqr,1
lma,qwe,2
Feature file:
call the csv and xml file
For first and last variable it works but for version it doesn't take version value
Yes, changing the tag name (or key name in JSON) is an advanced operation which "embedded expressions" cannot be used for. I think you should just use XPath. You should also take some time to read the examples linked from the documentation.
Here is just one way to do it, there are many more:
Scenario Outline:
* def payload =
"""
<name>
<first>#(_first)</first>
<last>#(_last)</last>
</name>
"""
* set payload /name/version = _version
* match payload == <name><first>foo</first><last>bar</last><version>1</version></name>
Examples:
| _first | _last | _version |
| foo | bar | 1 |

Scenario Outline for json in feature with Karate

Is it possible to use Scenario Outline as in this mode (which is really great!!):
Scenario Outline:
* print 'hello <name>'
Examples:
| (read('cats.json')) |
but with a json or a list in Background? Ex:
Background:
* json temp = cats_ids (ids that I get from an external job as here [111,222,333...])
or
* def temp = cats_ids
Scenario Outline:
* path id from temp
* method get
...
Examples:
| temp |
Yes, please look at karate.mapWithKey() explained here: https://github.com/intuit/karate#json-transforms
* def temp = karate.mapWithKey(cats_ids, 'id')
Scenario Outline:
* print id

How to pass response variable in path through examples table in karate

I am trying to pass a variable value in URL path which is further stored in examples table.
Unfortunately, it gives me error.
Can any one please help.
Background:
* def challengeID = res.challengeID
* def version = '2'
Given url dispatch And path '/api/fire/v' + version + '/sms/otp/' + <challengeID>
And param code = <code>
And header Content-Type = 'application/json'
When method GET Then status 400
Examples:
| challengeID | code |
| #(challengeID) | 2121211 |
| 3434343434343 | 111111 |
Sorry, the Examples: table cannot be dynamic. This is standard "Cucumber" behavior. Use table instead: https://github.com/intuit/karate#table
But I think you are over-complicating things. You should just do this:
And path 'api/fire/v' + version + '/sms/otp', res.challengeID

Use variables in json path expressions [duplicate]

This question already has an answer here:
How to use a variable in JsonPath filter
(1 answer)
Closed 2 years ago.
I have a feature file to retrieve the data from csv file for the given parameter. In order to do it, I need to use a variable in the JsonPath expression to retrieve data for given parameter. I tried may ways, but using variable in jsonPath is not working. I'm using 0.9.4 version
I tried the below ways:
* def userId = get[0] testData[?(#.UserType=='${userType}')].UserId
* def userId = get[0] testData[?(#.UserType==userType)].UserId
* def userId = get[0] testData[?(#.UserType=='#(userType)')].UserId(I suppose this can only be used in json/xml)
Below hard coded value works fine:
* def userId = get[0] testData[?(#.UserType=='SuperAdmin')].UserId
Called feature:
Feature: Utility to extract the various types of data from excel datasource
Background:
* def DataUtility = Java.type('com.org.utils.DataUtility')
* def dataUtils = new DataUtility()
* def testData = read('classpath:testdata/TestData.csv')
Scenario: Retrieve userId for a given user type
* def userId = get[0] testData[?(#.UserType=='${userType}')].UserId
Calling feature:
* table params
| userType |
| 'SuperAdmin' |
* def extractedData = call read('DataExtractor.feature') params
* def userID = extractedData[0].userId
I have tried using variables in Json Path.
Example:
String elementPath = "$.[" + i + "].item.value";
In this statement, i is a loop variable. After this pass it to the read api of DocumentContext.
It has worked perfectly fine for me.
I am using JsonPath & DocumentContext from jayway to achieve this.
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;

How to deserialize json payload passed to other feature file which accept multiple arguments

I am sending multiple arguments to .feature file one of the argument is request json payload generated by using karate table. How to iterate through request payload so that post request will get one payload at a time.
Scenario: post booking
* table payload
| firstname | lastname | totalprice | depositpaid |
| 'foo' | 'IN' | 10 | true |
| 'bar' | 'out' | 20 | true |
#date will calculate using js function in background and baseURL is configured in karate.config.js file
* set payload[*].bookingdates = { checkin: '#(date())', checkout: '#(date())' }
* def result = call read('createrecord.feature') {PayLoad: #(payload) , URL: #(baseURL)}
######################################
createrecord.feature file will have
#ignore
Feature: To create data
Background:
* header Accept = 'application/json'
Scenario:
Given url __arg.URL
And path 'booking'
And request __arg.PayLoad
When method post
Then status 200
Here in createrecord.feature file how I can iterate through passed payload so that single payload will be passed to post request.
The simple rule you are missing is that if the argument to call is a JSON array (of JSON objects) it will iterate automatically.
Read the docs carefully please: https://github.com/intuit/karate#data-driven-features
So make this change:
* def result = call read('createrecord.feature') payload
And baseURL will be available in createrecord.feature so you don't need to worry about passing it.
Note that this may not work: * set payload[*].bookingdates refer this answer: https://stackoverflow.com/a/54928848/143475