How can you change all appearances of a text response in Karate DSL without having a placeholder?

那年仲夏 提交于 2021-01-28 06:00:18

问题


I am wondering if it is possible to change all occurrences of a text in a Json response from a Karate DSL text without having a placeholder. For example , the fragment "https://thisisthepart.tochange" appears many times in the json response of karate dsl. I cannot do it with "set" because i do not know for which keys is going to appear that text.

"imageUrl": ["https://thisisthepart.tochange/idontwannachange/thispart"]

Thanks in advance


回答1:


Yes, convert to a string, use the Java API String.replace() and re-convert:

* string response = response
* json response = response.replace('foo', 'bar')

Also refer: https://github.com/intuit/karate#type-conversion

EDIT: note that you can "re-build" JSON using a loop:

* def before = { a: 'hello', b: 'world' }
* def after = {}
* def fun = function(k, v){ after[k] = v + 'edit' }
* karate.forEach(before, fun)
* match after == { a: 'helloedit', b: 'worldedit' }


来源:https://stackoverflow.com/questions/65182252/how-can-you-change-all-appearances-of-a-text-response-in-karate-dsl-without-havi

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