You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从1.3.1升级至1.4.0+后无法执行JavaScript函数

Karate版本升级后共享作用域调用Feature时JS函数失效问题

问题说明

将Karate从1.3.1版本升级至1.4.0/1.4.1/1.5.0.RC1版本后,出现以下异常情况:

  • 使用共享作用域调用其他Feature文件(语法:* call read('classpath:CalledFeature.feature'))时,无法调用定义在DataUtils.js中的JavaScript函数
  • 这些JS函数通过karate-config.js加载,加载代码为:config = karate.call('classpath:utils/DataUtils.js', config);
  • 若改用隔离作用域调用(语法:* def cf = call read('classpath:api/CalledFeature.feature')),函数可正常运行,但UI登录场景必须依赖共享作用域来共享driver实例,无法切换为隔离作用域
  • 共享作用域调用时的报错信息:
    org.graalvm.polyglot.PolyglotException: TypeError: execute on {} failed due to: Message not supported.
    

复现代码

Test.feature

Feature: Test Upgrade from 1.3.1 to 1.4.0/1.4.1/1.5.1.RC1

    Background:

        # 这个写法可行,但属于隔离作用域,不满足UI测试需求
        #* def cf = call read('classpath:api/CalledFeature.feature')

        # 这个写法是共享作用域,能让调用方访问被调用Feature内的变量和UI测试所需的driver实例,但会触发报错
        * call read('classpath:api/CalledFeature.feature')

    Scenario: Scenario 1

        * def myString = stringOfLength(10)
        * print myString

CalledFeature.feature

Feature: Test

    Scenario: Scenario 1
        * print "I am the called Feature"

karate-config.js

function fn() {
    config = karate.call('classpath:utils/DataUtils.js', config);
}

DataUtils.js

function fn(config) {

    config.stringOfLength = function(length) {
        var stringToReturn = "";
        for (var i = 0; i < length; i++) {
                stringToReturn += "A";
            }
        return stringToReturn;
    }
    return config;
}

内容的提问来源于stack exchange,提问作者SDewar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:33:23