[英]Jmeter: wrapper for jmeter obects in groovy script
H, I have code like this in JSR223 Assertion H,我在 JSR223 断言中有这样的代码
import groovy.json.JsonSlurper;
import org.apache.jmeter.services.FileServer;
failureMessage = "";
sampleLabel = SampleResult.getSampleLabel();
isResponseCodeCorrect("204");
def isResponseCodeCorrect(String expCode)
{
String actualCode = prev.getResponseCode();
if(!expCode.equals(actualCode)){
failureMessage += "ERROR code: Expected <"+ expCode +"> but we got instead " + actualCode + ";";
}
}
The question is possible to create the some code library in groovy or java and inject the objects form executed sampler (like public variables: ctx, vars, props, SamplerResult)?问题是可以在 groovy 或 java 中创建一些代码库并注入对象形式执行的采样器(如公共变量:ctx、vars、props、SamplerResult)?
The goal is to inject objects like SampleResult, AssertionResult (to invoke -> SampleResult.getSampleLabel()), AssertionResult.setFailureMessage("failureMessage); AssertionResult.setFailure(true); ) and own functions.目标是注入像 SampleResult、AssertionResult(调用 -> SampleResult.getSampleLabel())、AssertionResult.setFailureMessage("failureMessage); AssertionResult.setFailure(true); )和自己的函数之类的对象。
Like to have the wrapper (some object) with methods to play with Assertion, saving data to CSV ... The wrapper is going to be imported to JSR223 Assertion and then I would like to call some specific methods.喜欢让包装器(某个对象)与方法一起玩断言,将数据保存到 CSV ......包装器将被导入到 JSR223 断言,然后我想调用一些特定的方法。
The expected wrapper:预期的包装器:
@groovy.transform.MapConstructor
class Foo {
SampleResult results,
AssertionResult assertionResult
isResponseCodeCorrect(String expCode) {
String actualCode = prev.getResponseCode();
if(!expCode.equals(actualCode)){
failureMessage += "ERROR code: Expected <"+ expCode +"> but we got instead " + actualCode + ";";
assertionResult.setFailureMessage(failureMessage);
assertionResult.setFailure(true);
log.error(sampleLabel + " Error is going to be saved to the logs");
PrintLogToFile();
}
}
}
JSR223 Assertion: JSR223 断言:
import org.foo. wrapper
Foo wrapper = new Foo();
wrapper.isResponseCodeCorrect("200");
Or some useful things to avoid code duplication between calls或者一些有用的东西来避免调用之间的代码重复
You can compile your Groovy or Java code into a .jar file and drop it intoJMeter Classpath您可以将 Groovy 或 Java 代码编译为.jar 文件并将其放入JMeter Classpath
Once done you will be able to access the functions available in the .jar file from anyJSR223 Test Element and/or from __groovy() function .完成后,您将能够从任何JSR223 测试元素和/或从__groovy() 函数访问 .jar 文件中可用的函数。
I can think only of one class which you need: JMeterContext an instance of which can be obtained as simple as:我只能想到一个你需要的类: JMeterContext一个实例可以像这样简单地获得:
def ctx = org.apache.jmeter.threads.JMeterContextService.getContext()
once done you will be able to access whatever you need like:完成后,您将能够访问您需要的任何内容,例如:
SampleResult
== ctx.getPreviousResult()
SampleResult
== ctx.getPreviousResult()
AssertionResult
== ctx.getPreviousResult().getAssertionResults()
AssertionResult
== ctx.getPreviousResult().getAssertionResults()
vars
== ctx.getVariables()
vars
== ctx.getVariables()
props
== ctx.getProperties()
props
== ctx.getProperties()
Check out How to Reuse Your JMeter Code with JAR Files and Save Time article for more details if needed.如果需要,请查看如何使用 JAR 文件重用 JMeter 代码和节省时间一文了解更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.