首页 > 解决方案 > 从空手道中的 HREF URL 中提取值

问题描述

我在空手道中有一个查询,它将返回对象的 self Href URL

"href": "https://domain.xyz/version/system/hwlayer/suppliers/supplier/locations/location/machines/machine"

部分响应应作为下一次测试的输入数据。我需要能够提取

供应商定位机

而且它们的长度不是固定的。我查找了帮助,但找不到我要查找的内容

标签: karate

解决方案


这不是一个karate问题。建议你下次找懂JS或者正则表达式的人帮忙。这是一种可能的解决方案:

* def link = 'https://blah/suppliers/supplier/foo/location/bar/machine'
* def text1 = '/supplier/'
* def text2 = '/location/'
* def pos1 = link.lastIndexOf(text1)
* def pos2 = link.lastIndexOf(text2)
* def supplier = link.substring(pos1 + text1.length, pos2)
* match supplier == 'foo'
* def pos3 = link.lastIndexOf('/')
* def location = link.substring(pos2 + text2.length, pos3)
* match location == 'bar'

编辑 - 另见:https ://stackoverflow.com/a/67331307/143475


推荐阅读