Karate中能否在if判断右侧使用call(once) read?求替代方案
Karate测试中条件判断内callonce read调用的问题
需求背景
验证mvn/runner参数与单场景选项时遇到read调用相关问题。需求如下:
- 允许测试人员在运行时定义
dataRow(用户账户信息),让当前feature文件使用专属认证 - 通过runner或mvn命令执行测试时,复用配置初始化阶段的认证结果,避免每个feature文件重复认证(该配置逻辑已正常工作)
本地可正常运行的认证前置代码
本地运行feature文件时,以下Background认证前置代码可正常执行:
Background: Authentication 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
尝试过的错误写法及问题
尝试在if语句中判断karate.dsRow是否传入,并调用read时,无法像单行调用那样正常工作,以下是几种尝试及报错情况:
1. 分两行写法
本地运行正常,但mvn/runner执行时提示ReferenceError: "domainAuth" is not defined
* if (!(karate.properties['karate.dsRow'])) domainAuth = read('classpath:adapters/domainsession.feature') * callonce domainAuth
2. 两次if判断写法
分别出现语法错误(缺失闭合引号)和方法重载不匹配错误:
写法一:
* if (!(karate.properties['karate.dsRow'])) domainAuth = read('classpath:adapters/domainsession.feature') * if (!(karate.properties['karate.dsRow'])) karate.callonce('#(domainAuth)')
写法二:
* if (!(karate.properties['karate.dsRow'])) domainAuth = read('classpath:adapters/domainsession.feature') * if (!(karate.properties['karate.dsRow'])) karate.callonce(domainAuth)
3. 合并为单行if块写法
同样出现方法重载不匹配或语法错误:
写法一:
01: if (!(karate.properties['karate.dsRow'])) { domainAuth = read('classpath:adapters/domainsession.feature'); karate.callonce(domainAuth) }
写法二:
* if (!(karate.properties['karate.dsRow'])) { karate.set('domainAuth', karate.read('classpath:adapters/domainsession.feature')); karate.callonce('#(domainAuth)'); }
诉求
希望call(once) read能支持在if判断的右侧作为单行语句使用;若该方案不可行,求其他未尝试的替代方案。
本地测试示例代码
testUrl.feature
Feature: Scenario: * def testUrl = 'http://www.google.com'
test.feature
Feature: Test for test call and callonce after a conditional Background: Authentication PreRequisites * def testUrl = karate.get('karate.testUrl', 'http://www.bing.com') #**** Calls combined for troubleshooting of the call/callonce function after a conditional #***** uncomment only 1 of the below statements # * if (!(karate.properties['karate.testUrl'])) { karate.set('testUrlFile', karate.read('testUrl.feature')); karate.call('#(testUrlFile)'); } # * if (!(karate.properties['karate.testUrl'])) { karate.set('testUrlFile', karate.read('testUrl.feature')); karate.call(testUrlFile); } # * if (!(karate.properties['karate.testUrl'])) { karate.set('testUrlFile', karate.read('testUrl.feature')); karate.callonce('#(testUrlFile)'); } # * if (!(karate.properties['karate.testUrl'])) { karate.set('testUrlFile', karate.read('testUrl.feature')); karate.callonce(testUrlFile); } #**** Calls separated for troubleshooting of the call/callonce function after a conditional # * if (!(karate.properties['karate.testUrl'])) { karate.set('testUrlFile', karate.read('testUrl.feature')) } # * if (!(karate.properties['karate.testUrl'])) { karate.log('testUrlFile is ',testUrlFile) } #***** uncomment only 1 of the below statements to accompany the above 2 lines # * if (testUrlFile) { karate.call('#(testUrlFile)'); } # * if (testUrlFile) { karate.call(testUrlFile); } # * if (testUrlFile) { karate.callonce('#(testUrlFile)'); } # * if (testUrlFile) { karate.callonce(testUrlFile); } * url testUrl Scenario: Given path '/' When method GET Then status 200
内容的提问来源于stack exchange,提问作者mike
相关产品推荐
相关产品推荐

