首页 > 解决方案 > 在读取 *.feature 文件中的属性时,Karate properties.json 会引发 ReferenceError

问题描述

最近将空手道从 0.6.2 更新到 0.9.5,我在整个测试用例中使用的 properties.json 中有许多 ReferenceError。

我有以下设置:

测试属性.json

{
    "headers": {
        "x-client-ip": "192.168.3.1",
        "x-forwarded-for": "192.168.3.1"
    }
}

test-auth.feature

  Background:
      * def props = read('properties/test-properties.json')

然后props我在第一个场景中进一步使用:

And header User-Agent = props.headers.Accept-Language
And header X-Forwarded-For = props.headers.x-forwarded-for

但是,在运行此程序时,我遇到以下问题:

com.intuit.karate.exception.KarateException: test-auth.feature:14 - javascript evaluation failed: props.headers.Accept-Language, ReferenceError: "Language" is not defined in <eval> at line number 1

我尝试将属性文件添加到与 相同的包中test-auth.feature,但无济于事。问题似乎与读取 json 文件有关。我知道空手道 0.6.2 可以评估文件类型并在内部以其本机格式对其进行解析。现在还是这样吗?如果没有,从空手道 0.9.5 中的 properties.json 读取的解决方案是什么。

标签: jsonpropertieskarate

解决方案


读取 JSON 文件时应该没有任何改变。空手道将 RHS 评估为 JS,所以我认为这是解决方案:

And header User-Agent = props.headers['Accept-Language']
And header X-Forwarded-For = props.headers['x-forwarded-for']

编辑:这对我有用:

* def props = { headers: { 'Accept-Language': 'foo', 'x-forwarded-for': 'bar' } }
* url 'http://httpbin.org/headers'
* header User-Agent = props.headers['Accept-Language']
* header X-Forwarded-For = props.headers['x-forwarded-for']
* method get

导致:

1 > GET http://httpbin.org/headers
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Host: httpbin.org
1 > User-Agent: foo
1 > X-Forwarded-For: bar

因此,如果您仍然卡住,请按照以下流程操作:https ://github.com/intuit/karate/wiki/How-to-Submit-an-Issue


推荐阅读