how to put assertion on a response value present in JSON? [duplicate] - karate

This question already has an answer here:
Error using DBUtils in first scenario [duplicate]
(1 answer)
Closed 1 year ago.
I have a test-case like this in which I'm verifying the total number of rows - I have referred the below page and tried this solution, but seems that it is not working. I want to check whether my noOfRows is greater than 2.
This is the response I'm getting for printing
Total number of rows [
{
"1": 27
}
]
* def noOfRows = result
* print 'Total number of rows ',result
* assert noOfRows == { "1": '#? _ > 2' }

If result is an array, you can do this:
* def result = [1, 2, 3]
* assert result.length > 2
What the readRows returns has nothing to do with Karate, so please work with someone who knows Java well if required: https://stackoverflow.com/a/52078427/143475
Also it looks like you are on an old version of Karate, so upgrade. And if still stuck, follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Related

How to compare each element in two different lists in Karate? [duplicate]

Is it possible to continue the execution of test step even if one of the assert/match fails?
Ex:
Scenario: Testing
* def detail = {"a":{"data":[{"message":["push","dash"]},{"message":["data","Test"]}]}}
* match detail contains {"a":{"data":[{"message":["push","dash"]}]}}
* print detail
Here match will fail but execution stop at that point.
is there a way to do a soft assertion so that next step gets executed?
EDIT in 2021 - a PR introducing a continueOnStepFailure flag was contributed by Joel Pramos here and is available in Karate 1.0 onwards. You can find more details here: https://stackoverflow.com/a/66733353/143475
If you use a Scenario Outline each "row" is executed even if one fails.
Scenario Outline: Testing
* def detail = { a: 1, b: 2, c: 3 }
* match detail contains <expected>
Examples:
| expected |
| { a: 1 } |
| { b: 2 } |
| { c: 3 } |
Note that the concept of "soft assertions" is controversial and some consider it a bad practice:
a) https://softwareengineering.stackexchange.com/q/7823
b) https://martinfowler.com/articles/nonDeterminism.html
For those looking for a way to show all mis-matches between 2 JSON objects, see this: https://stackoverflow.com/a/61349887/143475
And finally, since some people want to do "conditional" match logic in JS, see this answer also: https://stackoverflow.com/a/50350442/143475

Random errors when matching strings [duplicate]

This question already has an answer here:
Karate - match fails for inline array
(1 answer)
Closed 1 year ago.
Usually matching works just fine but I've had weird random matching errors recently :
$.userInfo | not equal | match failed for name: 'phone' (MAP:MAP)
{"givenName":"##string","familyName":"##string","phone":"##string"}
{"givenName":"Andreas","familyName":"Conroy","phone":"+19515554384"}
$.userInfo.phone | not equal (STRING:STRING)
'##string'
'+19515554384'
How is '+19515554384' not a string ?
Thanks
Sounds like you have your "expected" vs "actual" data mixed up. Here below are 2 lines you can cut and paste and verify that it works:
* def response = {"givenName":"Andreas","familyName":"Conroy","phone":"+19515554384"}
* match response == {"givenName":"##string","familyName":"##string","phone":"##string"}

How do I return the sum of array with exceptions by using a while loop in a function?

Python beginner here.
I already have the solution to the question but I'm not understanding why the "add" variable in the solution plays a role of creating exceptions to remove numbers between 6 and 9. I already tried Python Tutor but still not understanding. Many thanks in advance!
QUESTION: Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.
Sample Solution code
def summer_69(arr):
total = 0
add = True
for num in arr:
while add:
if num != 6:
total += num
break
else:
add = False
while not add:
if num != 9:
break
else:
add = True
break
return total
Sample answers:
summer_69([1, 3, 5]) --> 9
summer_69([4, 5, 6, 7, 8, 9]) --> 9
summer_69([2, 1, 6, 9, 11]) --> 14
You can think of the variable "add" as a flag. I think that might be a better name for this variable in this instance.
It is only being used to tell if you have run into a 6 within the sequence of numbers in the array, then once it has been set it goes through an arbitrary amount of numbers in the array until it gets a 9 and then it resets the flag.
It may help to rename the variable "add" as "flag". Have your new variable "flag" default to False and then if you run into a 6 set "flag" to true. Once the flag is on do not add any trailing numbers in the sequence until you run into the number 9 then reset to false.
Perhaps that will help the readability. Naming variables is the hardest part of programming.

Karate API - How we can get the specific array element for which index will be dynamic. I need value for last array element which is not fixed

I read that variable will not work as a index in array when we try to access array[i] or something like that.
so How I can or get a value for last element of the array which is dynamic. It will change after every API call.
Variables will work in JS. They won't work in JsonPath (e.g. match Left Hand Side), read the docs to understand the difference:
* def foo = [1, 2, 3, 4]
* def size = karate.sizeOf(foo)
* def last = foo[size - 1]
* match last == 4

How can I match response X or Y? [duplicate]

This question already has an answer here:
Is it possible to use karate 'match' inside conditional statement?
(1 answer)
Closed 2 years ago.
I wanted to match my response is equal to X or Y. I saw many json specified samples but I couldn't utilize those samples. I have a string that I gathered from response and I want to match it with X or Y. It should be ok if it is equal to X or Y.
My reference language value that I defined above of the code = profile_language The language value that I defined from json response = response_content_language And match response_content_language contains ('en',profile_language)
Language value should be profile_language or if it is not then it should be value as 'en'.
And match response_content_language contains ('en',profile_language)
I took error as I expected.
You could use something like
* def table = [ "en", #(profile_language) ]
And match table contains response_content_language