开发者问题收集

在jmeter中加载两个javascript文件后,我无法使用javascript文件的功能

2020-01-02
1563

我使用的是 jmeter 版本 5.2.1。为了加载我的 javascript 文件,我使用了 jsr223 采样器,语言为 javascript。 我的问题是我必须从 jmeter 登录 cognito,而不使用外部 phantomjs 驱动程序。因此,我导入了用于登录 cognito 的 cognito javascript 文件,并提供了正确的详细信息。以前,当我通过外部创建的 js 文件使用 phantom js 登录时,此代码可以正常工作。但在 jsr223 采样器中移动我的代码时,它给出错误“AWSCognito”未定义。 我还将 jsr223 采样器的大小增加到 600,即 jsr223.compiled_scripts_cache_size=600

在此处输入图片描述

我正在执行以下代码

load("D:/apache-jmeter-5.2.1/bin/aws-cognito-sdk.min.js");
load("D:/apache-jmeter-5.2.1/bin/amazon-cognito-identity.min.js");
//var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;

var userData = {    user_email: '[email protected]',    user_pw: 'test',    UserPoolId: 'test', ClientId: 'test',}

    var authenticationData = {
        Username : userData.user_email,
        Password : userData.user_pw,
    };
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);    
    var poolData = {
        UserPoolId : userData.UserPoolId, 
        ClientId : userData.ClientId 
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var userData = {
        Username : userData.user_email,
        Pool : userPool
    };
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            // console.log('access token + ' + result.getAccessToken().getJwtToken());
            //document.getElementById("successAndErrorMessages").innerHTML=result.getAccessToken().getJwtToken(); 
            log.info(result.getAccessToken().getJwtToken());
        },

        onFailure: function(err) {
            log.info(err);
        },

    });

执行脚本时出现以下错误

2020-01-02 10:47:59,037 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2020-01-02 10:47:59,038 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2020-01-02 10:47:59,039 INFO o.a.j.t.JMeterThread: Thread started: AutomationR&D 1-1
2020-01-02 10:48:00,381 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler (load js)path, message: javax.script.ScriptException: ReferenceError: "AWSCognito" is not defined in <eval> at line number 15
javax.script.ScriptException: ReferenceError: "AWSCognito" is not defined in <eval> at line number 15
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155) ~[nashorn.jar:?]
    at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_221]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:225) ~[ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) [ApacheJMeter_core.jar:5.2.1]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
Caused by: jdk.nashorn.internal.runtime.ECMAException: ReferenceError: "AWSCognito" is not defined
    at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) ~[nashorn.jar:?]
    at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319) ~[nashorn.jar:?]
    at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291) ~[nashorn.jar:?]
    at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1442) ~[nashorn.jar:?]
    at jdk.nashorn.internal.scripts.Script$1312$\^eval\_.:program(<eval>:15) ~[?:?]
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637) ~[nashorn.jar:?]
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494) ~[nashorn.jar:?]
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449) ~[nashorn.jar:?]
    ... 11 more

请帮助我解决这个问题

2个回答

使用 load() 函数 加载多个 JavaScript 文件应该没有问题,例如:

  1. 假设我的 JMeter 安装的“bin”文件夹中有 foo.js 文件,其代码如下:

    function foo() {
    log.info('foo')
    }
    
  2. 还有 bar.js 文件,其代码如下:

    function bar() {
    log.info('bar')
    }
    
  3. 我可以从 JSR223 Sampler 类似:

    load('foo.js')
    load('bar.js')
    foo()
    bar()
    

    在此处输入图片说明

因此,我建议检查 aws-cognito-sdk.min.js 文件的完整性,甚至可以从 Github 获取它,而不是依赖本地副本,例如:

load("https://raw.githubusercontent.com/amazon-archives/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js")

还要注意 从 JMeter 3.1 开始建议使用 JSR223 测试元素 Groovy 语言 进行脚本编写,因此请考虑迁移到 aws-java-sdk 并使用 groovy 语言获取令牌,您可以使用 aws-cognito-java-desktop-app 作为参考。

Dmitri T
2020-01-03

看来您的库导致

"AWSCognito" is not defined in <eval> at line number 15
出现脚本错误。

解决此问题将修复您的功能。

BRO_THOM
2020-01-03