Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a length of array in Karate framework?

Tags:

karate

I have 2 different response from the REST Api as below

1. {"type":null,"title":"Request is invalid","status":"BAD_REQUEST","detail":"Please correct request and try again.","invalidParams":[{"name":"create.arg0.name","reason":"Name is required"}]}

2. {"type":null,"title":"Unicode char u0000 is not allowed","status":"BAD_REQUEST","detail":"Unicode char u0000 is not allowed"}

I wanna write a condtion where if invalidParams present in respose, then compare the contents of array. If not, invalidParams should be null/undefined.

 Scenario Outline: Create Asset Model with missing name and status
    Given url modelsUrl
    And request somedata
    When method POST
    Then status 400
    Then assert response.type == null
    Then match response.status == 'BAD_REQUEST'
    Then match (response.invalidParams != undefined && response.invalidParams[0].reason contains <reason>) OR (response.invalidParams == undefined)

But comparing against null/undefied & length also not working. How to handle this scenario in Karate? Normal Javascript is not working.

like image 459
Santosh Hegde Avatar asked Oct 16 '25 19:10

Santosh Hegde


1 Answers

To check if a member exists or to check an array's length you should use assert, not match. I would also recomend breaking your last statement into multiple assertions for transparency.

here are some examples given a response with an array named 'arr':

check array length

...
And assert response.arr.length == 1
...

check array is present

...
And assert response.arr != null
...

check array is absent

...
And assert response.arr == null
...

reference: https://intuit.github.io/karate/#payload-assertions

like image 184
riiich Avatar answered Oct 19 '25 13:10

riiich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!