引入js的库到服务器,比如jsencrypt。之后在metersphere脚本中引入这个库。想知道应该使用什么方法引入?也可以拓展一下其他的,python、beanshell、js,都给整体梳理一下方法和案例

ERROR e3afd954 1-1 Problem in JSR223 script JSR223Sampler, message: {}
ReferenceError: “require” is not defined. (eval#35) in eval at line number 35 at column number 0
javax.script.ScriptException
at org.mozilla.javascript.engine.RhinoScriptEngine.eval(RhinoScriptEngine.java:122)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:231)
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:237)
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:672)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:590)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:502)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:265)
at java.base/java.lang.Thread.run(Thread.java:840)

JavaScript导入三方库:MeterSphere接口测试-接口参数CryptoJS加密 - 知乎
Python导入三方库:在 MeterSphere 前后置脚本中引用 Python 第三方库 · TesterHome
BeanShell在项目设置-文件管理里上传jar包导包就可以了

按着js的操作,操作完之后提示找不到文件,如下。
代码:
g = new Packages.org.mozilla.javascript.tools.shell.Global(Packages.org.mozilla.javascript.Context.getCurrentContext());
this.load = g.load;
var JSEncrypt = load(“/opt/metersphere/data/body/jsencrypt.js”);

// 登录加密 公钥
const PUBLIC_kEY =
‘MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ==’;

// 明文
const plainText = ‘YourPlainTextHere’;

const en_crypt = (txt) => {
const encryptor = new JSEncrypt();
encryptor.setPublicKey(PUBLIC_kEY); // 设置公钥
return encryptor.encrypt(txt); // 对需要加密的数据进行加密
};
en_crypt(plainText);
执行结果:
ERROR ffd79027 1-1 Problem in JSR223 script JSR223Sampler, message: {}
Couldn’t read source file “/opt/metersphere/data/body/jsencrypt.js”: /opt/metersphere/data/body/jsencrypt.js (No such file or directory). (eval#10) in eval at line number 10 at column number 0
javax.script.ScriptException
at org.mozilla.javascript.engine.RhinoScriptEngine.eval(RhinoScriptEngine.java:122)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:231)
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:237)
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:672)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:590)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:502)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:265)
at java.base/java.lang.Thread.run(Thread.java:840)

你的MeterSphere是部署在/opt/metersphere目录下么?

这个报错咋解决,求助

JMETER: 227 - JMETER-LOG 2024-05-22 14:30:00 ERROR b4678b1b 1-1 Error while processing sampler: ‘JSR223Sampler’.
Cannot invoke “org.mozilla.javascript.Scriptable.has(String, org.mozilla.javascript.Scriptable)” because “obj” is null
java.lang.NullPointerException
at org.mozilla.javascript.ScriptableObject.getBase(ScriptableObject.java:2370)
at org.mozilla.javascript.ScriptableObject.putConstProperty(ScriptableObject.java:2220)
at org.mozilla.javascript.ScriptRuntime.setConst(ScriptRuntime.java:2158)
at org.mozilla.javascript.gen.eval_214._c_script_0(eval:16)
at org.mozilla.javascript.gen.eval_214.call(eval)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:380)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3868)
at org.mozilla.javascript.gen.eval_214.call(eval)
at org.mozilla.javascript.gen.eval_214.exec(eval)
at org.mozilla.javascript.Context.evaluateString(Context.java:1137)
at org.mozilla.javascript.engine.RhinoScriptEngine.eval(RhinoScriptEngine.java:118)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:231)
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:237)
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:672)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:590)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:502)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:265)
at java.base/java.lang.Thread.run(Thread.java:840)

这个报错是怎么出现的?用例中使用了脚本吗?

按着js的操作,rancher报错,报错信息是上面的评论,代码是下面的代码。
代码:
g = new Packages.org.mozilla.javascript.tools.shell.Global(Packages.org.mozilla.javascript.Context.getCurrentContext());
this.load = g.load;
var JSEncrypt = load(“/opt/metersphere/data/body/jsencrypt.js”);

// 登录加密 公钥
const PUBLIC_kEY =
‘MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ==’;

// 明文
const plainText = ‘YourPlainTextHere’;

const en_crypt = (txt) => {
const encryptor = new JSEncrypt();
encryptor.setPublicKey(PUBLIC_kEY); // 设置公钥
return encryptor.encrypt(txt); // 对需要加密的数据进行加密
};
en_crypt(plainText);