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
Related
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(_)'
I'm trying to write some karate tests that check a response for some expected structure. For example, there is an object that looks like this:
{
'true|true':'disabled',
'true|false':'locked',
'false|false':'enabled',
'false|true':'disabled',
'default':'enabled'
}
However, the keys and values could be any strings. There must be a way to specify this, but I'm at a loss for how to do this in a generic way.
Essentially, what I would like to test is that 1) the object is not empty, 2) each of the keys and values is a string (not an object and not null).
The basic match looks like this:
* def objects = $fields..objects
And match each objects ==
"""
{
key1: '#? isString(_)',
key2: '#? isString(_)'
}
"""
It looks like the names of the keys must be known in advance, so perhaps match expressions are not the appropriate way to test this.
You can extract all keys like this:
* def keys = karate.keysOf(object)
So once you have that, you are in business:
* def foo = { a: 1, b: 2 }
* def keys = karate.keysOf(foo)
* match each keys == '#string'
* assert keys.length > 0
See JSON tranforms for other ideas, e.g. karate.forEach(): https://github.com/intuit/karate#json-transforms
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.
Is there any way to validate output of a request with one of multiple templates?
for example:
{
"pendingInvitesCount":#number,
"acceptedInvitesCount":#number,
"rejectedInvitesCount":#number
}
or
[]
Not really recommended as testing best practice, but this should give you some ideas:
* def actual = []
* def expected = (actual.size() == 0 ? '#[0]' : { a: '#number', b: '#number' })
* match actual == expected
I'm new to Groovy and Grails. I think this problem probably has an easy answer, I just don't know it.
I have a database table:
id | category | caption | image | link
I have a query that lets me retrieve one row for each distinct item in 'category.'
I'm trying to return a map where each row is an array named by it's category.
e.g., If I select the rows:
[{category='foo', caption='stuff', ...} {category='bar', caption='things', ...}]
I want to be able to:
return [foo:foo, bar:bar]
where:
foo = [caption='stuff', ...]
bar = [caption='things', ...]
Thanks for any help.
You can transform your list using collect in Groovy, however, the result depends on the source data. I could not infer from your post that if you are returning one item per category or multiple.
def ls = Category.list()
def newList = ls.collect {
[(it.category):['caption':it.caption,'image':it.image,'link':it.link]]
}
will result in something like :
[
[bar:[caption:BarCaption-2, image:BarImage-2, link:BarLink-2]],
[bar:[caption:BarCaption-1, image:BarImage-1, link:BarLink-1]],
[bar:[caption:BarCaption-0, image:BarImage-0, link:BarLink-0]],
[foo:[caption:FooCaption-2, image:FooImage-2, link:FooLink-2]],
[foo:[caption:FooCaption-1, image:FooImage-1, link:FooLink-1]],
[foo:[caption:FooCaption-0, image:FooImage-0, link:FooLink-0]]
]
If you have multiple items per each category you probably want to return the list of each.
def bars = newList.findAll { it.containsKey 'bar' }
def foos = newList.findAll { it.containsKey 'foo' }
[foos:foos,bars:bars]
If I understand your question correctly (I think you mean to put ':' instead of '=' for your maps) then I think the following will work. If new to Groovy, you might find the Groovy web console at http://groovyconsole.appspot.com/ useful. You can try snippets of code out easily, like the following:
def listOfMaps = [[category:'foo', caption:'stuff', something:'else'], [category:'bar', caption:'things', another:'thing']]
def mapOfMaps = [:]
listOfMaps.each { mapOfMaps += [(it.remove('category')) : it] }
assert mapOfMaps == [foo:[caption:'stuff', something:'else'], bar:[caption:'things', another:'thing']]