如何在空手道特定的JSON模式中使用模板

如何在空手道特定的JSON模式中使用模板

本文介绍了如何在空手道特定的JSON模式中使用模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在空手道下面尝试.

I am trying below in Karate.

我在.json文件中有一个json模式(用于响应验证).在许多模式中,很少有常见的REGEX.我想将它们作为键值对提取到一个通用文件中,并在其他架构中使用它.是否可以?如果是这样,我该怎么办?JSON模式中允许模板化吗?

I have a json schema ( for response validation) in .json file. There are few REGEXs that are common in many schemas. I want to extract them into one common file as key value pairs and use it across other schemas. Is it possible? if so, how can I do that? Is templating allowed in json schema?

{
  "response": {
    "name": "#string",
    "amount": "#regex ^(-?)([0]|[1-9][0-9]{0,15})[.][0-9]{2}$"
  }
}

功能文件

Feature: Example feature

  Background:

  * def sampleResponse = read('./sample-response.json');



  Scenario: Example scenario

    When url 'https://someurl.com'
    And method get
    Then status 200
    And  match response == sampleResponse

我想做什么?

我想将正则表达式的金额作为可重用变量存储在json文件中,并在json文件中使用模板替换它.是否可以?

What would I like to Do?

I would like to store the amount regex in json file as a reusable variable and use templating in json file to replace it.Is it possible?


{
  "response": {
    "name": "#string",
    "amount": "{{get regex from this template}}"
  }
}

推荐答案

是.即使在读取文件.

这样做:

{
  "response": {
    "name": "#string",
    "amount": "#(amount)"
  }
}

然后执行以下操作:

Background:
* def amount = 100
* def sampleResponse = read('sample-response.json')

如果您希望 amount 来自另一个JSON文件,为什么不呢,在下面说这是 data.json :

If you want the amount to come from another JSON file, why not, say this below is data.json:

{ "amount": 100 }

然后您执行以下操作:

Background:
* def data = read('data.json')
# you don't need the next line if you use "data.amount" as the embedded expression
* def amount = data.amount
* def sampleResponse = read('sample-response.json')

这篇关于如何在空手道特定的JSON模式中使用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 11:30