简体   繁体   中英

In Karate DSL, how can I use the replace text for other data types such as int, float, Big, etc.?

I found the below example on github.

  • def text = 'hello world bye'

  • replace text | token | value | | one | 'cruel' | | two | 'good' |

  • match text == 'hello cruel world good bye'

What If the value I want to replace can only accept integers or other data types? For example,

  • replace text | token | value| | hours | 90 | | price | 123.45 | | quantity | 999999999999 |

I was not able to put the token inside another file because the json validator does not like the <> without the double quotes. any suggestions?

Replace is meant for text not JSON, read the doc carefully please. First, there is no problem with numbers and replace:

* def text = 'hello <name> how many <hours>'
* replace text
    | token  | value    |
    | name   | 'John'   |
    | hours  | 200      |
* match text == 'hello John how many 200'

Now, if you are trying to fiddle with JSON, just use the set keyword.

* def json = { hello: '', hours: null }
* set json
    | path   | value    |
    | hello  | 'John'   |
    | hours  | 200      |
* match json == { hello: 'John', hours: 200 }

Note that the above would work even if you omit the first line. Also refer to embedded expressions as another way to substitute values in JSON, refer to the doc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM