Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: ReferenceError: "setTimeout" is not defined

Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: ReferenceError: "setTimeout" is not defined

我有一个要求,我需要 运行 一个 JS 文件来获取令牌。 JS 文件引用了变量,如“window”、“setTimeout”函数,这些函数在浏览器加载时可用。

但我需要加载这个 Jmeter,并提取令牌。问题是 JSR233 说它没有引用“window”或“setTimeout”函数(否则在浏览器中可用)。

当我 运行 它时,我得到以下错误。有什么方法可以在 Jmeter 中获取这些函数的句柄。

021-06-24 13:11:08,246 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: ReferenceError: "setTimeout" is not defined in /Users/pprabhak/performance/apache-jmeter-5.4.1/bin/BehaviorAnalytics-2.2.0.min.js at line number 12 javax.script.ScriptException: ReferenceError: "setTimeout" is not defined in /Users/pprabhak/performance/apache-jmeter-5.4.1/bin/BehaviorAnalytics-2.2.0.min.js at line number 12 at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:477) ~[jdk.scripting.nashorn:?] at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:433) ~[jdk.scripting.nashorn:?] at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:521) ~[jdk.scripting.nashorn:?] at javax.script.CompiledScript.eval(CompiledScript.java:89) ~[java.scripting:?] at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:217) ~[ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72) [ApacheJMeter_java.jar:5.4.1] at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638) [ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) [ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:?] at java.lang.Thread.run(Thread.java:834) [?:?] Caused by: jdk.nashorn.internal.runtime.ECMAException: ReferenceError: "setTimeout" is not defined at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.objects.Global.noSuchProperty(Global.java:1616) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.scripts.Script$Recompilation7368435452$BehaviorAnalytics_2_2_0_min$cu10$restOf.VAAP$:split-2(/Users/pprabhak/performance/apache-jmeter-5.4.1/bin/BehaviorAnalytics-2.2.0.min.js:12) ~[?:?] at jdk.nashorn.internal.scripts.Script$Recompilation386052$BehaviorAnalytics_2_2_0_min.VAAP(/Users/pprabhak/performance/apache-jmeter-5.4.1/bin/BehaviorAnalytics-2.2.0.min.js:1) ~[?:?] at jdk.nashorn.internal.scripts.Script$Recompilation388$BehaviorAnalytics_2_2_0_min$cu1$restOf.:program(/Users/pprabhak/performance/apache-jmeter-5.4.1/bin/BehaviorAnalytics-2.2.0.min.js:1) ~[?:?] at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.runtime.Context.evaluateSource(Context.java:1438) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.runtime.Context.load(Context.java:962) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.objects.Global.load(Global.java:1720) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.scripts.Script$Recompilation383$^eval_/0x0000000800b68040.:program(:3) ~[?:?] at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?] at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?] at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:428) ~[jdk.scripting.nashorn:?] ... 9 more 2021-06-24 13:11:08,249 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1

Window对象是与浏览器密切相关的东西,你将无法在JSR223测试元素中使用。

有两种处理方式:

  1. 使用WebDriver Sampler plugin其中:

  2. 在 JSR223 测试元素中复制 JavaScript 文件的逻辑(注意 Groovy language is recommended for scripting), if the script requires some parameters you should be able to get them from the previous response using JMeter's Post-Processors