Karate: Is there a way to pass variable as string in scenario outline and examples table [duplicate] - karate

This question already has an answer here:
Passing variables into Examples section [duplicate]
(1 answer)
Closed 1 year ago.
I am using latest karate v1.1.0. My feature looks like this:
Feature: Scenario outline with examples table
Background:
* url 'http://localhost:8080'
* def dummy = Java.type(karatetests.attributes)
* def test = new attributes()
* def userid = test.getuserid()
Scenario Outline: pass userid with string as parameter
Given path '<path>'
And Header Host = 'hostname'
And Header User-Agent = '<Ua-string>'
When method POST
Then status 200
Examples:
| path | Ua-string |
| api | AppleWebKit/537.36 (KHTML, like Gecko, userid)|
In cucumber: I was able to realise the variable value of 'userid' in Ua-string table with AppleWebKit/537.36 (KHTML, like Gecko, ${userid})
In karate: I tried with 'userid', "userid", "#(userid)", and '#(userid)' unfortunately was not succesfull.
Examples:
| path | Ua-string |
| api | AppleWebKit/537.36 (KHTML, like Gecko, userid)| => Result: userid string is passed not its value
| api | AppleWebKit/537.36 (KHTML, like Gecko, 'userid')| => Result: syntax error
| api | AppleWebKit/537.36 (KHTML, like Gecko, "userid")| => Result: "userid" string is passed not its value
| api | AppleWebKit/537.36 (KHTML, like Gecko, "#(userid)")| => Result: "#(userid)" string is passed not its value
| api | AppleWebKit/537.36 (KHTML, like Gecko, '#(userid)')| => Result: '#(userid)' syntax error
How can I replace the userid with its value, while passing it to Ua-string header?
Thanks

Works perfectly for me. Try this example that you can cut and paste into any feature and see that it works for yourself:
Scenario Outline:
* url 'https://httpbin.org/anything'
* header User-Agent = userAgent
* method get
Examples:
| userAgent |
| foo |
| bar |
But I think you are expecting functions and variables to work within Examples: - but sorry, that is not supported: https://stackoverflow.com/a/60358535/143475
EDIT I still don't understand what you mean by "variable" in your comment, but if you are seeing some problems with commas, try this, and refer the docs: https://github.com/intuit/karate#scenario-outline-enhancements
Scenario Outline:
* url 'https://httpbin.org/anything'
* header User-Agent = userAgent
* method get
Examples:
| userAgent! |
| 'foo, bar' |
| 'baz, ban' |
And finally, if you really want to do some "magic" using variables, you can always do that in the scenario body as shown here: https://stackoverflow.com/a/60358535/143475
Or just use string concatenation. Is that so hard ?

Related

extracting data from unstructured JSON in big query

I have JSON as a string in a big query field:
[{"name":"user_group","value":"regular"},{"name":"checkout_version","value":"2.2"},{"name":"currency","value":"EUR"},{"name":"currency_exchange_rate","value":"1"},{"name":"currency_symbol","value":"€"},{"name":"variant","value":"default"},{"name":"snowplow_id","value":"XXXXXXX"},{"name":"ip_address","value":"XXXX"},{"name":"user_agent","value":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"},{"name":"is_test_order","value":"false"}]
I'm going crazy trying to extract the value ("default") from this section:
{"name":"variant","value":"default"}
the part I want will always follow "name":"variant","value":" and have a " at the end.
I have tried json_extract but regexp_extract seems the best option, I tried this: (
select REGEXP_EXTRACT(json_string_field, r'/\{"value":"([^"]+)"/') as variant
from source_table
)
but I'm just getting nulls back...would appreciate ideas...
consider below query
WITH json_data AS (
SELECT '[{"name":"user_group","value":"regular"},{"name":"checkout_version","value":"2.2"},{"name":"currency","value":"EUR"},{"name":"currency_exchange_rate","value":"1"},{"name":"currency_symbol","value":"€"},{"name":"variant","value":"default"},{"name":"snowplow_id","value":"XXXXXXX"},{"name":"ip_address","value":"XXXX"},{"name":"user_agent","value":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"},{"name":"is_test_order","value":"false"}]' json
)
SELECT JSON_VALUE(kv, '$.value') AS value
FROM json_data, UNNEST(JSON_QUERY_ARRAY(json)) kv
WHERE JSON_VALUE(kv, '$.name') = 'variant';

How to extract data using multiple delimited values in splunk

I have the below string in logs with multiple delimiters (: = and #). I am expecting all the values in tabular formate like
tenant |countryCode |deviceType |platformID|paymentMethod1|paymentMethod2|userAgent
XYZ | US | IOS |13 |p1 |p2 |Mozilla /20.0.553 Mozilla/5.0
logs string
TrackingLogs tenant=XYZ, countryCode=US, deviceType:IOS, platformID:13,currency=USD, paymentMethods:P1 # P1 # P2 # P2 # P4 # , userAgent:Mozilla /20.0.553 Mozilla/5.0
I tried for ':' but no result
search string| rex field=_raw "deviceType\:\s+?(?<deviceType>\S+)" |table deviceType
for = I used below query it worked but don't know how to combine it with : and #
search trackinglog | rex field=tenant "(?<tenant>[^\.]*)\.[a-zA-Z]"| table _raw tenant, countryCode , currency , paymentMethods
The problem with the first query is not the separator, but the regex itself. It expects a space where none exists. This variation works:
| rex field=_raw "deviceType:\s*?(?<deviceType>\S+)" |table deviceType
For better results, however, try the extract command.
| extract pairdelim="," kvdelim=":="

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

Refer defined variable in Scenario outline example [duplicate]

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

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