Karate v1.3无法读取Scenario Outline文件引用的Bug反馈
Karate v1.3升级后Scenario Outline Examples中无法访问Background定义的变量
问题现象
使用同一Feature文件测试无效URL端点时,Karate v1.2运行正常,但仅将版本升级至v1.3后,出现endpointFile未定义错误,无法加载端点文件。
Feature文件代码
Feature: Common failures across all endpoints Background: PreRequisites * def dataSourceRow = karate.get('karate.dsRow', 1) * def dataSourceEnv = karate.get('karate.env', 'dev') * callonce read('../../../../service-config.js') { dsRow: '#(dataSourceRow)' , env: '#(dataSourceEnv)' } * if (skipDomainFlag) {karate.logger.debug('skipDomainFlag is set to true so Domain calls cannot be ran!'); karate.abort(); } * callonce read('classpath:adapters/domainsession.feature') * configure afterFeature = function(){ karate.call('classpath:adapters/logout.feature'); } * url baseUrlDomain + domainPath * configure headers = domainHeaders * header host = domainHost * def endpointFile = '_v3endpoints.json' ##********** ## Common Domain failures shared for all endpoints ##********** Scenario Outline: Scenario for <name> endpoint - Invalid endpoint * def endpoint = '<endpoint>' * replace endpoint | token | value | | endpointEnd | 'x' | | pathVariable | validPathVariable | | pathVariable2 | validPathVariable2 | Given path endpoint And request <payload> When method <endpointMethod> Then status 404 Examples: | read(endpointFile) |
v1.2运行结果
Karate version: 1.2.0 ====================================================== elapsed: 12.04 | threads: 1 | thread time: 8.07 features: 1 | skipped: 0 | efficiency: 0.67 scenarios: 1 | passed: 1 | failed: 0 ======================================================
v1.3运行结果
Karate version: 1.3.0 ====================================================== elapsed: 2.36 | threads: 1 | thread time: 0.00 features: 1 | skipped: 0 | efficiency: 0.00 scenarios: 1 | passed: 0 | failed: 1 ====================================================== >>> failed features: js failed: >>>> 01: read(endpointFile) <<<< org.graalvm.polyglot.PolyglotException: ReferenceError: "endpointFile" is not defined - <js>.:program(Unnamed:1)
端点文件示例
[ { "name": "v3/lob/{lineOfBusiness}", "endpoint": "v3/lob<endpointEnd>/<pathVariable>", "invalidPathVariable": "test", "invalidPathVariable2": "", "validPathVariable": "Internet", "validPathVariable2": "Internet", "endpointMethod": "GET", "payload": "{}" } ]
问题原因
Karate v1.3对Scenario Outline的Examples解析逻辑做了调整:Examples部分的表达式会在Background执行之前解析,而v1.2中Examples是在Background执行后才解析的。因此Background里定义的endpointFile变量在Examples解析时还未初始化,导致变量未定义错误。
解决方案
有三种可行的解决方式:
方式1:将变量定义移到Feature最顶部(Background之外)
把endpointFile的定义放在Feature的开头,确保Examples解析时变量已经存在:
Feature: Common failures across all endpoints * def endpointFile = '_v3endpoints.json' Background: PreRequisites * def dataSourceRow = karate.get('karate.dsRow', 1) * def dataSourceEnv = karate.get('karate.env', 'dev') * callonce read('../../../../service-config.js') { dsRow: '#(dataSourceRow)' , env: '#(dataSourceEnv)' } * if (skipDomainFlag) {karate.logger.debug('skipDomainFlag is set to true so Domain calls cannot be ran!'); karate.abort(); } * callonce read('classpath:adapters/domainsession.feature') * configure afterFeature = function(){ karate.call('classpath:adapters/logout.feature'); } * url baseUrlDomain + domainPath * configure headers = domainHeaders * header host = domainHost
方式2:直接在Examples中使用文件路径
跳过变量,直接把文件路径写在read()方法中:
Examples: | read('_v3endpoints.json') |
方式3:在karate-config.js中定义全局变量
在karate-config.js里添加endpointFile的全局定义,这样所有Feature都能访问:
function fn() { return { // 其他全局变量 endpointFile: '_v3endpoints.json' }; }
内容的提问来源于stack exchange,提问作者mike
相关产品推荐
相关产品推荐

