-1

I'm trying to evaluate expressions with arithematic operators in karate using javascript. As an alternative to javascript eval(), I'm using karate.get() method to perform the evaluations. In All cases, I'm able to evaluate the expression and get a boolean value. But when performing evaluation on JSON object, to check if it null, karate.get is returning first value of the expression instead of a boolean value. Any help would be appreciated. Thanks in advance. Note: I'm facing this issue only when I have added karate-core dependency in my pom.xml.

* json input1 = '{ "id": 4068, "name": "Rep. Draupadi Asan"}'
* def input2 = null
* def operator = "!="

* def Dataiterate =
      """
          function(input1, input2,operator){ 
            
          let stringifiedInput1 = JSON.stringify(input1);
          let stringifiedInput2 = JSON.stringify(input2);
          let valid = karate.get(stringifiedInput1 + operator + stringifiedInput2)
          karate.log(valid)
           if(valid)
           {
            karate.log("true")
           }
           else
           {
            karate.log("false")
           }
           
       }
     """
 * def final = Dataiterate(input1,input2,operator)

Snapshot of output: snapshot

1 Answers1

0

First let me say that there are so many problems and mis-understandings in your code. I feel you are trying to use Karate in ways that it is not designed for and not possible.

Here is my best guess of interpreting what you are trying to do. If this does not make sense, I suggest you look for some other framework.

* def concat =
  """
  function(arg1, arg2, op) {
    return karate.toString(arg1) + " " + arg2 + " " + op;
  }
 """
* def input1 = { "id": 4068, "name": "Rep. Draupadi Asan" }
* def input2 = null
* def operator = "!="
* def final = concat(input1, input2, operator)
* match final == '{"id":4068,"name":"Rep. Draupadi Asan"} null !='
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248