How to use eval in karate?

家住魔仙堡 提交于 2020-01-05 06:50:45

问题


I already gone through the karate documentation and tried below code:

 * eval if (nearby.content[?(@.title == 'Nearby Malls & Restaurants')] == '#notnull') karate.call(* def nearByMallsRestraurants = get nearby.content[?(@.title == 'Nearby Malls & Restaurants')].items[?(@.name)])

But its throwing error.I am trying to extract all Nearby Malls & Restaurants names if Nearby Places and Landmarks Array contains Nearby Malls & Restaurant.Also if possible can you please tell me how to use collection.sort to get sorted Nearby Malls name.

My json looks like :

"nearby": {
        "title": "Nearby Places and Landmarks",
        "content": [
          {
            "title": "Nearby Malls & Restaurants",
            "items": [
              {
                "name": "Forum Mall, Koramangala",
                "distance": 1.8
              },
              {
                "name": "Eggzotic",
                "distance": 2.4
              },
              {
                "name": "Kerala Pavilion Restaurant",
                "distance": 2.4
              },
              {
                "name": "New Shanthi Nagar",
                "distance": 2.5
              },
              {
                "name": "Venus Biryani",
                "distance": 2.8
              }
            ]
          },
          {
            "title": "Closest Airport, Railway Station & Bus Stand",
            "items": [
              {
                "name": "Madiwala Ayyappa Temple Bus Stop",
                "distance": 2.1
              },
              {
                "name": "Kalasipalyam Bus Stand",
                "distance": 5.7
              },
              {
                "name": "Bangalore Cantonment Railway Station",
                "distance": 6.5
              },
              {
                "name": "Kempegowda/ Majestic bus station",
                "distance": 7
              },
              {
                "name": "KSR Bengaluru City Railway Station",
                "distance": 7.5
              },
              {
                "name": "KR Puram Railway Station",
                "distance": 8.5
              },
              {
                "name": "KSRTC Mysore Road Satellite Bus Stop",
                "distance": 9
              }
            ]
          }
        ]
      }

回答1:


Please note - JsonPath is not JS and you are mixing things here. My advice is don't over-complicate things and split it into multiple steps. This should answer all your questions, including a sort:

* def malls = $nearby.content[?(@.title=='Nearby Malls & Restaurants')]
* def names = $malls..name
* def Collections = Java.type('java.util.Collections')
* eval Collections.sort(names)
* print names

output:

[
  "Eggzotic",
  "Forum Mall, Koramangala",
  "Kerala Pavilion Restaurant",
  "New Shanthi Nagar",
  "Venus Biryani"
]


来源:https://stackoverflow.com/questions/51037311/how-to-use-eval-in-karate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!