English 中文(简体)
Karate.get() method does not return default value when given jsonpath is not present
原标题:
  • 时间:2023-07-15 05:27:16
  •  标签:
  • karate

Karate official document mentions that it returns null if variable/jsonpath is not found. The default value can be overriden using second argument.

When I tried this then for the absent variable I see it is giving null and able to override null. But the same did not go as expected for jsonpath expression. It gives #notpresent which is not overridden by the second argument.

JSON Document

{
"store": {
    "book": [
        {
            "category": "reference",
            "author": "Nigel Rees",
            "title": "Sayings of the Century",
            "price": 8.95
        },
        {
            "category": "fiction",
            "author": "Evelyn Waugh",
            "title": "Sword of Honour",
            "price": 12.99
        },
        {
            "category": "fiction",
            "author": "Herman Melville",
            "title": "Moby Dick",
            "isbn": "0-553-21311-3",
            "price": 8.99
        },
        {
            "category": "fiction",
            "author": "J. R. R. Tolkien",
            "title": "The Lord of the Rings",
            "isbn": "0-395-19395-8",
            "price": 22.99
        }
    ],
    "bicycle": {
        "color": "red",
        "price": 19.95
    }
},
"expensive": 10

}

// Working example with variables
* def df = 12
* def v741 = karate.get("df", "100")
* print "v741", v741 // output = 12 bcz variable  df  exists
* v741 = karate.get("df1", "100")
* print "v741", v741 // output = 100 bcz variable  df1  does not exists


// non-working example with jsonpath
* def v74 = karate.get("$jsonObject.store.book[1].author1", 100)
* print "v74", v74 // Output = #notpresent

enter image description here

More Update on the issue -

It is certainly a bug. Sorry, I reverted the accepted answer upvote.

* print karate.get("jsonObject.store.book[*].author", 100) // It should give an array of authors but gave 100.  
* print karate.get("$jsonObject.store.book[*].author", 100) // It gives an array of authors

You are saying "In other words, use $ only when you want to run JsonPath "searches" or "queries". As per doc, it should return the default value when jsonpath did not return a result.

问题回答

Don t use a $ and it will work as you expect:

* def v74 = karate.get("jsonObject.store.book[1].author1", 100)

In other words, use $ only when you want to run JsonPath "searches" or "queries".

You are welcome to contribute a PR if you feel the docs need improving.





相关问题
How do I get karate-gatling example project to compile?

I downloaded the karate-gatling example project from here: https://github.com/karatelabs/karate/tree/master/karate-gatling I opened the project in IntelliJ and ran the following command: mvn clean ...

Reading Data with from csv file in Karate

Below is the data, I have maintained in the csv file Data "[{ label : Karate is the only open-source tool to combine API test-automation, mocks, performance-testing and even UI automation into ...

Conditional logic for Karate UI

I am trying to add a conditional logic for karate UI feature that I am trying to build. The requirement is this: There are 5 fields; select(‘select[id=currency]’, ‘EUR’) // has a random logic to ...

热门标签