jmeter运行脚本报错,跟java版本有关,做个记录。
1. 问题记录:
执行登录接口测试,登录失败。点击jmeter右上角【黄色!】,查看错误日志。显示如下:
2022-09-23 10:29:53,642 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 预处理程序
javax.script.ScriptException: Cannot find engine named: 'nashorn', ensure you set language field in JSR223 Test Element: JSR223 预处理程序
at org.apache.jmeter.util.JSR223TestElement.getScriptEngine(JSR223TestElement.java:100) ~[ApacheJMeter_core.jar:5.4.1]
at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.4.1]
at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:965) [ApacheJMeter_core.jar:5.4.1]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) [ApacheJMeter_core.jar:5.4.1]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.1]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.1]
at java.lang.Thread.run(Thread.java:833) [?:?]
2022-09-23 10:29:53,692 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 预处理程序
javax.script.ScriptException: Cannot find engine named: 'nashorn', ensure you set language field in JSR223 Test Element: JSR223 预处理程序
at org.apache.jmeter.util.JSR223TestElement.getScriptEngine(JSR223TestElement.java:100) ~[ApacheJMeter_core.jar:5.4.1]
at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.4.1]
at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:965) [ApacheJMeter_core.jar:5.4.1]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) [ApacheJMeter_core.jar:5.4.1]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.1]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.1]
at java.lang.Thread.run(Thread.java:833) [?:?]
2. 问题分析:
执行脚本中有一个javascript的脚本(security.js),而高版本java已经移除了nashorn javascript引擎。可推断security.js脚本含有nashorn javascript引擎,因为不支持,所以会报错。
//jmeter JSR223 预处理程序
load("security.js");
//pwd1 = vars.get("passwd");
function RSA(){
var exponent = "10001";
var modulus = vars.get("publicKeyModulus");
var passwd = "1";
RSAUtils.setMaxDigits(200);
var publicKey = new RSAUtils.getKeyPair(exponent,"",modulus);
return RSAUtils.encryptedString(publicKey,passwd);
}
var data = RSA();
vars.put("Password",data);
查看脚本运行环境的Java版本
C:\Users\Zyl>java -version
java version "18.0.1.1" 2022-04-22
Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)
3. 解决办法:
将本地Java版本换为较低版本,如:jdk1.8.0_151
C:\Users\Zyl>java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
重启jmeter后,再次执行登录接口测试。测试通过