Embedded expressions not replaced if surrounded by dot and underscores in it - karate

The embedded expressions are not replaced when appended, prepended or surrounded by characters in the following way
* def RADName = 'IntegrationFirstRAD'
* def tenantID = '1452119626'
* def out =
"""
{
"nsName": "fld_<tenantID>_stage00.rad.<RADName>_.resources:<RADName>_resource"
}
"""
* print out
Executing the scenario returns:
"nsName":"fld_1452119626_stage00.rad.<RADName>_.resources:<RADName>_resource
In the above scenario 'RADName' is not replaced with the value

Please use the replace keyword:
* def out = { nsName: 'fld_<tenantID>_stage00.rad.<RADName>_.resources:<RADName>_resource' }
* replace out.RADName = 'IntegrationFirstRAD'
* replace out.tenantID = '1452119626'
* match out == { "nsName": "fld_1452119626_stage00.rad.IntegrationFirstRAD_.resources:IntegrationFirstRAD_resource" }
You seem to be confused between embedded expressions and Scenario Outlines.
I guess it is worth saying this again, you really really really should read the documentation fully once.

Related

How to assert a list in which each value should be one the values of the expected list?

I am trying to assert a list using match contains any, match each but it is not working.
def actualList = ["CABLE_MODEM","SET_TOP_BOX","SET_TOP_BOX","CABLE_MODEM","CBE"]
def expectedList = ["CABLE_CARD","SET_TOP_BOX","CABLE_MODEM","MTA","OTHER","IP_SET_TOP_BOX"]
match each actualList contains any expectedList
Basically, every value of the actualList should be any one of the expectedList value. But it is directly comparing the first value of the 2 lists. Kindly help me
I'm not sure I understand the question. But sometimes these crazy assertions are best done in JS:
* def actualList = ["CABLE_MODEM","SET_TOP_BOX","SET_TOP_BOX","CABLE_MODEM","CBE"]
* def expectedList = ["CABLE_CARD","SET_TOP_BOX","CABLE_MODEM","MTA","OTHER","IP_SET_TOP_BOX"]
* def unexpected = actualList.filter(x => !expectedList.includes(x))
* match unexpected == []
Please take the help of a friend who knows JS if the above is not clear.
EDIT: for completeness, here's the "karate style" way to solve this. For more complex custom checks, karate.match() can be used.
* def valid = function(x){ return expectedList.includes(x) }
* match each actualList == '#? valid(_)'

How to remove the unwanted characters from integer array in karate framework

Below is the function and its returning the integer values with %.
def functions = []
eval for(var i=0; i<response.functions.length; i++) functions.add(response.functions[i].value)
*print functions
It's return the below output.
['2.16%','1.34%','1.32%','1.25%','0.65%','0.48%','0.42%','0.26%','0.14%','0.06%','0.03%','0%']
I want to remove the single codes and % symbol. After removing it should be like below.
[2.16,1.34,1.32,1.25,0.65,0.48,0.42,0.26,0.14,0.06,0.03,0]
Please help me to remove the extra characters from Integer array
Here you go, just do a map()
* def response = ['2.16%','1.34%','1.32%']
* def cleaned = response.map(x => x.replace('%', '') * 1)
* match cleaned == [2.16, 1.34, 1.32]
Please refer to the docs: https://github.com/karatelabs/karate#json-transforms
I am able to replace the special character, '%' in this case using below code snippet. Feel free to change the special character as required. Hope this helps.
#ReplaceChars
Scenario: Replacing the special characters
* def repalceValues =
"""
function(){
let responseVal = ['2.16%','1.34%','1.32%','1.25%','0.65%','0.48%','0.42%','0.26%','0.14%','0.06%','0.03%','0%']
const finalVal = new Array();
responseVal.forEach((val) =>{
finalVal.push(val.replace('%', ''))
})
karate.log("New Array : "+ finalVal)
}
"""
* repalceValues()

Karate - Unable to replace graphql query variables through data driven scenario outline 'examples' [duplicate]

I am not sure why replace is not working.
I have a graphql query:
mutation updateLocation{
updateLocation(input: {
address:"<address>",
id:"<storeID>",
name:"<name>",
workingHours: [
{
closingTime:"<closingTime>",
isClosed:false,
openingTime:"<openingTime>"
}
.......
And in feature file I have this:
Given def query = read ('classpath:graphQL/updateStore.graphql')
* replace query.address = "<address>"
* replace query.regionId = "<regionId>"
* replace query.name = "<name>"
* replace query.closingTime = "<closingTime>"
* replace query.openingTime = "<openingTime>"
* replace query.storeID = storeId
And request { query : '#(query)'}
When method post
Then status 200
Examples:
|address |regionId |name |closingTime |openingTime |
|Adrs1 |286 |st1 |20:00 |10:00 |
The replace works for address, regionid, and name but it does not work for closing time or opening time, these two values stay empty.
Also if I define header in background like this:
Given header Authorization = 'Bearer ' + token
I still have to add this line for each request in the same scenario, or I have been missing something?
Works for me:
* def query = 'closingTime:"<closingTime>"'
* replace query.closingTime = '20:00'
* match query == 'closingTime:"20:00"'
So please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
Note that * replace query.closingTime = closingTime should work. I recommend avoiding confusion by using different names for the Examples columns.

Is there a way to match dynamic object keys?

I'm looking for a simple technique to match objects where the key may not be known in advance (e.g. we may fetch the schema as part of the test). As a contrived example:
Scenario:
* def result = { foo: 'bar' }
* def key = 'foo'
Then match result == { '#(key)': 'bar' }
...which doesn't currently work.
Once you realize there is a JavaScript engine behind the scenes, you will get even more ideas :)
* def result = { foo: 'bar' }
* def key = 'foo'
* def expected = {}
* expected[key] = 'bar'
Then match result == expected
Also do a search for other answers [karate] dynamic you will find many interesting examples such as this one: https://stackoverflow.com/a/57226061/143475

Unable to Parse the variable value to the array variable

I was trying to pass the variable 'i' value to a array index 'locations[i]' using below karate code. but throwing an error saying unable to parse. Please suggest be for any changes.
Feature: Verify Branches
Background: For loop implementation
Given url ''
When method GET
Then status 200
* def i = 0
* def z = $.locations[i].zip
* def p = $.locations[i].phone
* def fun =
"""
function(locations){
for (var i = 0; i < locations.length; i++)
{
print(i)
print('Element at Location ' + i +':' + p)
}
}
"""
Scenario: Validate the locations
Given url ''
When method GET
Then status 200
* call fun p
It is hard to make out anything since you have not provided the value of the response. There are many things wrong here. But I'll try.
Take this line:
* def z = $.locations[i].zip
This will not work, Karate does not support variables within JsonPath by default, refer the docs: https://github.com/intuit/karate#jsonpath-filters
And I think you are un-necessarily using JsonPath where normal JavaScript would have been sufficient:
* def z = response.locations[i].zip
Also it seems you are just trying to loop over an array and call a feature. Please refer to the documentation on Data Driven Features.
Take some time and read the docs and examples please, it will be worth your time. One more tip - before I leave you to understand Karate a little better. There is a way to convert a JSON array into another JSON array should you need it:
* def fun = function(x){ return { value: x } }
* def list = [1, 2, 3]
* def res = karate.map(list, fun)
* match res == [{ value: 1 }, { value: 2 }, { value: 3 }]
So there should never be a need for you to manually do a for loop at all.