Remove elements from a list in karate? - karate

after using scripAll() to get list of style attribute values, I need to eliminate a few. Wanted to know if there is something like remove() in python which can be used for the same.
Example:
* def list_colors = ["rgb(245,60,86)", "rgb(245,60,86)", "rgb(245,00,00)", "rgb(245,00,00)" ]
Want to remove rgb(245,00,00) from the list. How can I do this in karate?
Thanks

I think you missed that scriptAll() can take a third "filter function" argument, please refer the docs: https://github.com/intuit/karate/tree/master/karate-core#scriptall-with-filter
* def list_colors = scriptAll('.my-css', "_.style['display']", x => !x.includes('245,00,00'))
Otherwise please refer JSON transforms: https://github.com/intuit/karate#json-transforms
* def filtered = karate.map(list_colors, x => !x.includes('245,00,00'))

Related

Is it possible to generate dynamic variable names in Karate? [duplicate]

This question already has an answer here:
How to set dynamic value as a key to json string in request
(1 answer)
Closed 1 year ago.
I am making a REST API call which returns a response like this
{"id":"726295ab-d6bc-4f09-8cb7-6f6f54fc9364", "name":"Customer Data"}
I create 5 objects like this and I want to store the ids of all the 5 objects from response in 5 different variables.
I tried using something like
* def catID_<categoryName> = $.id
and provided the name of the object in the Examples section. It works fine most of the times except when the name has spaces in it.
no step-definition method match found for: * def catID_Customer Data = $.id
Is it possible to do something like this?
* def catName = replace all spaces in the name with _
* def #(catName)_id = $.id
or is there a better way to achieve this?
You seem to be doing things Karate is not designed to do, so by default please assume that this is not supported.
Most likely, adding keys to a JSON object is a more elegant approach instead of trying to dynamically hack def. For example:
* def variables = {}
* variables['<someDynamicName>'] = $.id
# then later
* print variables['actual name']
Also note that the '< and >' are not required: https://github.com/karatelabs/karate#scenario-outline-enhancements

Fetching few elements using "$.[:2]" operator throws error in karate. might be a bug

Example:
Scenario: test
* def response =
"""
[
"YEN01",
"DP258",
"SA661",
"BT202",
"UR809"
]
"""
* def subset = response.[:2]
* print subset
I tried response..[:2] . and also tried with enclosing in ().
Let me know if any one got this working.
Just adding one character will fix your problem !
* def subset = $response.[:2]
Karate defaults to JavaScript and when you want JsonPath evaluation you need to give a little hint to Karate. This is explained in the docs: https://github.com/intuit/karate#get-short-cut

Can we call a scenario from one feature in another using karate?

We have a feature A with several scenarios. And we need one scenario from that file. Can we call it in our feature B?
No. You need to extract that Scenario into a separate*.feature file and then re-use it using the call keyword.
EDIT: Karate 0.9.0 onwards will support being able to call by tag as follows:
* def result = call read('some.feature#tagname')
To illustrate the answer from Karate-inventor Peter Thomas with an example:
Given a feature-file some.feature with multiple scenarios tagged by a tag-decorator:
#tagname
Scenario: A, base case that shares results to e.g. B
// given, when, then ... and share result in a map with keyword `uri`
* def uri = responseHeaders['Location'][0]
#anotherTag
Scenario: X, base case that shares results to e.g. B
// given, when, then ... and share result in a map with keyword `createdResourceId`
* def createdResourceId = $.id
In another feature we can call a specific scenario from that feature by its tag, e.g. tagname:
Scenario: B, another case reusing some results of A
* def result = call read('some.feature#tagname')
* print "given result of A is: $result"
Given path result.uri + '/update'
See also: demo of adding custom tags to scenarios

SQL: Use a predefined list in the where clause

Here is an example of what I am trying to do:
def famlist = selection.getUnique('Family_code')
... Where “””...
and testedWaferPass.family_code in $famlist
“””...
famlist is a list of objects
‘selection’ will change every run, so the list is always changing.
I want to return only columns from my SQL search where the row is found in the list that I have created.
I realize it is supposed to look like: in ('foo','bar')
But no matter what I do, my list will not get like that. So I have to turn my list into a string?
('\${famlist.join("', '")}')
Ive tried the above, idk. Wasn’t working for me. Just thought I would throw that in there. Would love some suggestions. Thanks.
I am willing to bet there is a Groovier way to implement this than shown below - but this works. Here's the important part of my sample script. nameList original contains the string names. Need to quote each entry in the list, then string the [ and ] from the toString result. I tried passing as prepared statement but for that you need to dynamically create the string for the ? for each element in the list. This quick-hack doesn't use a prepared statement.
def nameList = ['Reports', 'Customer', 'Associates']
def nameListString = nameList.collect{"'${it}'"}.toString().substring(1)
nameListString = nameListString.substring(0, nameListString.length()-1)
String stmt = "select * from action_group_i18n where name in ( $nameListString)"
db.eachRow( stmt ) { row ->
println "$row.action_group_id, $row.language, $row.name"
}
Hope this helps!

Matplotlib: how to set dashes in a dictionary?

Can anybody tell me how to use a custom dash sequence in a dictionary. I cannot get that running and the only thing I cannot work with (not a programmer) is the documentation =-(
def lineCycler(): #must be invoked for every plot again to get the same results in every plot
#hasy="#7b9aae"
_styles = [{'color':'#b21a6a', 'ls':'-'},
{'color':'#65a4cb', 'ls':'[5,2,10,5]'},# this shoul be some custom dash sequnece
{'color':'#22b27c', 'ls':'-.'},
{'color':'k', 'ls':'--'}
]
_linecycler=cycle(_styles)
return _linecycler
Use dashes keyword for that (and you need a list, instead of a string):
def lineCycler():
_styles = [{'color':'#b21a6a', 'ls':'-'},
{'color':'#65a4cb', 'dashes':[5,2,10,5]},
{'color':'#22b27c', 'ls':'-.'},
{'color':'k', 'ls':'--'}
]
_linecycler=cycle(_styles)
return _linecycler