是否可以将 args 发送到 afterScenario 函数?

Is it possible to send args to an afterScenario function?

我在文档中看到可以在后台部分定义一个函数并在每个场景之后执行它。 参见:https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/hooks/hooks.feature

但是我需要向这个函数发送参数,但没有找到任何解决方案...

在文档中:

* configure afterScenario = 
"""
function(){
  var info = karate.info; 
  karate.log('after', info.scenarioType + ':', info.scenarioName);
  karate.call('after-scenario.feature', { caller: info.featureFileName });
}
"""

我想做什么:

utils/js/afterFunc.js:

function fn(args){
  karate.log('after', args.msg);
}

myTest.feature:

* configure afterScenario = read('classpath:utils/js/afterFunc.js') {msg: 'Hello !'}

read 函数将读取 afterFunc.js 文件,但它会忽略 {msg: 'Hello !'} 参数。

Scripts can call other scripts 但您不想立即调用脚本,是吗? 您想要创建一个函数引用并将该引用分配给 afterScenario 配置。

但这还不够。你想咖喱函数 - what is currying?

据我所知,read 不直接支持此功能。

有一个解决方法。 您可以阅读 javascript 文件并创建一个函数,使用您选择的参数调用 after-scenario-function

  Background:
    * def fn = read('classpath:after-scenario-with-params.js')
    * configure afterScenario =
    """
      function() {
        fn(karate.info, 'hello world');
      }
    """

after-scenario-with-params.js包含以下js函数:

function fn(info, someParameter) {
    karate.log('called after scenario:', info.scenarioName);
    karate.log('some parameter: ' + someParameter);
}

就是这样。

我向我的空手道沙盒存储库提交了 complete running example。该回购基于 gradle 和 groovy。希望对你有帮助。