0
votes

I am using jmeter version 5.2.1. For loading my javascript file I am using jsr223 sampler with language as javascript. My problem is that I have to login on cognito from jmeter without using external phantomjs driver. So I have imported the cognito javascript files that are used for login on cognito and provided the correct details. Previously this code was working correctly when I was login using phantom js through external created js files. But on moving my code in jsr223 sampler it is giving error "AWSCognito" is not defined in . I have also increased the size of jsr223 sampler to 600 i.e jsr223.compiled_scripts_cache_size=600

enter image description here

I am executing the following code

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);
        },

    });

on executing my script I got the following error

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

Please help me in resolving this issue

2

2 Answers

0
votes

There should be no problem to load multiple JavaScript files using load() function, for example:

  1. Given I have foo.js file in "bin" folder of my JMeter installation with the following code:

    function foo() {
        log.info('foo')
    }
    
  2. And bar.js file with the following code:

    function bar() {
        log.info('bar')
    }
    
  3. I can call both functions from the JSR223 Sampler like:

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

    enter image description here

So I would recommend checking integrity of your aws-cognito-sdk.min.js file and maybe even take it from the Github instead of relying on the local copy like:

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

Also be aware that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating to aws-java-sdk and use groovy language for getting the token, you can use aws-cognito-java-desktop-app as a reference.

0
votes

It seems your library is causing a script error at

"AWSCognito" is not defined in <eval> at line number 15

Resolving this issue will fix your functionality.