diff --git a/src/components/src/main/java/org/apache/jmeter/assertions/BSFAssertion.java b/src/components/src/main/java/org/apache/jmeter/assertions/BSFAssertion.java index 57ea8ae84cb..a8cd865e842 100644 --- a/src/components/src/main/java/org/apache/jmeter/assertions/BSFAssertion.java +++ b/src/components/src/main/java/org/apache/jmeter/assertions/BSFAssertion.java @@ -37,7 +37,7 @@ public AssertionResult getResult(SampleResult response) { BSFManager mgr =null; try { mgr = getManager(); - mgr.declareBean("SampleResult", response, SampleResult.class); + mgr.declareBean("sampleResult", response, SampleResult.class); mgr.declareBean("AssertionResult", result, AssertionResult.class); processFileOrScript(mgr); result.setError(false); diff --git a/src/components/src/main/java/org/apache/jmeter/assertions/BeanShellAssertion.java b/src/components/src/main/java/org/apache/jmeter/assertions/BeanShellAssertion.java index e80d2c724f3..5cc35551578 100644 --- a/src/components/src/main/java/org/apache/jmeter/assertions/BeanShellAssertion.java +++ b/src/components/src/main/java/org/apache/jmeter/assertions/BeanShellAssertion.java @@ -88,6 +88,7 @@ public AssertionResult getResult(SampleResult response) { // Add SamplerData for consistency with BeanShell Sampler bshInterpreter.set("SampleResult", response); //$NON-NLS-1$ + bshInterpreter.set("sampleResult", response); //$NON-NLS-1$ bshInterpreter.set("Response", response); //$NON-NLS-1$ bshInterpreter.set("ResponseData", response.getResponseData());//$NON-NLS-1$ bshInterpreter.set("ResponseCode", response.getResponseCode());//$NON-NLS-1$ diff --git a/src/components/src/main/java/org/apache/jmeter/assertions/JSR223Assertion.java b/src/components/src/main/java/org/apache/jmeter/assertions/JSR223Assertion.java index ffffe0de725..26c93735b97 100644 --- a/src/components/src/main/java/org/apache/jmeter/assertions/JSR223Assertion.java +++ b/src/components/src/main/java/org/apache/jmeter/assertions/JSR223Assertion.java @@ -46,6 +46,7 @@ public AssertionResult getResult(SampleResult response) { ScriptEngine scriptEngine = getScriptEngine(); Bindings bindings = scriptEngine.createBindings(); bindings.put("SampleResult", response); + bindings.put("sampleResult", response); bindings.put("AssertionResult", result); processFileOrScript(scriptEngine, bindings); result.setError(false); diff --git a/src/functions/src/main/java/org/apache/jmeter/functions/BeanShell.java b/src/functions/src/main/java/org/apache/jmeter/functions/BeanShell.java index f72284c877b..bea72f3d639 100644 --- a/src/functions/src/main/java/org/apache/jmeter/functions/BeanShell.java +++ b/src/functions/src/main/java/org/apache/jmeter/functions/BeanShell.java @@ -90,6 +90,7 @@ public synchronized String execute(SampleResult previousResult, Sampler currentS if (previousResult != null) { bshInterpreter.set("SampleResult", previousResult); //$NON-NLS-1$ + bshInterpreter.set("sampleResult", previousResult); //$NON-NLS-1$ } // Allow access to context and variables directly diff --git a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java index 16154997b3a..ffe1eebe182 100644 --- a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java +++ b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java @@ -82,6 +82,7 @@ public SampleResult sample(Entry e)// Entry tends to be ignored ... try { initManager(mgr); mgr.declareBean("SampleResult", res, res.getClass()); // $NON-NLS-1$ + mgr.declareBean("sampleResult", res, res.getClass()); // $NON-NLS-1$ // N.B. some engines (e.g. Javascript) cannot handle certain declareBean() calls // after the engine has been initialised, so create the engine last diff --git a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java index 3607a55692e..3006ae9c179 100644 --- a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java +++ b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java @@ -114,6 +114,7 @@ public SampleResult sample(Entry e)// Entry tends to be ignored ... } try { bshInterpreter.set("SampleResult", res); //$NON-NLS-1$ + bshInterpreter.set("sampleResult", res); //$NON-NLS-1$ // Set default values bshInterpreter.set("ResponseCode", "200"); //$NON-NLS-1$ diff --git a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java index 22ffd43dee1..c12edb10203 100644 --- a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java +++ b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java @@ -69,6 +69,7 @@ public SampleResult sample(Entry entry) { ScriptEngine scriptEngine = getScriptEngine(); Bindings bindings = scriptEngine.createBindings(); bindings.put("SampleResult",result); + bindings.put("sampleResult",result); Object ret = processFileOrScript(scriptEngine, bindings); if (ret != null && (result.getResponseData() == null || result.getResponseData().length==0)){ result.setResponseData(ret.toString(), null); diff --git a/src/protocol/java/src/test/java/org/apache/jmeter/protocol/java/sampler/JSR223SamplerTest.java b/src/protocol/java/src/test/java/org/apache/jmeter/protocol/java/sampler/JSR223SamplerTest.java index f2478c7e4d6..f94e42f6454 100644 --- a/src/protocol/java/src/test/java/org/apache/jmeter/protocol/java/sampler/JSR223SamplerTest.java +++ b/src/protocol/java/src/test/java/org/apache/jmeter/protocol/java/sampler/JSR223SamplerTest.java @@ -21,14 +21,17 @@ import org.apache.jmeter.samplers.SampleResult; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; class JSR223SamplerTest { - @Test - void sampleWithEndTimeSet() { + @ParameterizedTest + @ValueSource(strings={"sampleResult", "SampleResult"}) + void sampleWithEndTimeSet(String sampleResultVariableName) { JSR223Sampler sampler = new JSR223Sampler(); sampler.setName("jsr223Test"); - sampler.setScript("SampleResult.setEndTime(42); 'OK'"); + sampler.setScript(sampleResultVariableName + ".setEndTime(42); 'OK'"); sampler.setScriptLanguage("groovy"); SampleResult sampleResult = sampler.sample(null); assertEquals(42, sampleResult.getEndTime()); diff --git a/xdocs/changes.xml b/xdocs/changes.xml index f50ac08b508..b2028a21901 100644 --- a/xdocs/changes.xml +++ b/xdocs/changes.xml @@ -68,6 +68,7 @@ Summary
sampleResult
to reference SampleResult in all JSR223/BeanShell/JavaScript listeners, samplers and assertions.FileName
- the file name, if anyParameters
- text from the Parameters fieldbsh.args
- the parameters, split as described aboveSampleResult
- pointer to the current SampleResult
sampleResult
- pointer to the current SampleResult
ResponseCode
defaults to 200
ResponseMessage
defaults to "OK
"IsSuccess
defaults to true
When the script completes, control is returned to the Sampler, and it copies the contents
- of the following script variables into the corresponding variables in the SampleResult
sampleResult
ResponseCode
- for example 200
ResponseMessage
- for example "OK
"The SampleResult ResponseData is set from the return value of the script.
If the script returns null, it can set the response directly, by using the method
- SampleResult.setResponseData(data)
, where data is either a String or a byte array.
+ sampleResult.setResponseData(data)
, where data is either a String or a byte array.
The data type defaults to "text
", but can be set to binary by using the method
- SampleResult.setDataType(SampleResult.BINARY)
.
+ sampleResult.setDataType(SampleResult.BINARY)
.
The SampleResult
variable gives the script full access to all the fields and
+
The sampleResult
variable gives the script full access to all the fields and
methods in the SampleResult
. For example, the script has access to the methods
- setStopThread(boolean)
and setStopTest(boolean)
.
+ setStopThread(boolean)
and setStopTest(boolean)
.
Here is a simple (not very useful!) example script:
+or +Another example:
ensure that the property beanshell.sampler.init=BeanShellSampler.bshrc
is defined in jmeter.properties
.
@@ -1155,12 +1157,12 @@ Beware however that misuse of any methods can cause subtle faults that may be di
The JSR223 Sampler allows JSR223 script code to be used to perform a sample or some computation required to create/update variables.
-
${JMeterThread.last_sample_ok}
),
and ThreadGroup "Action to be taken after a Sampler error" (since JMeter 5.4)
FileName
- the file name, if anyParameters
- text from the Parameters fieldargs
- the parameters, split as described aboveSampleResult
- pointer to the current sampleResult
- pointer to the current sampler
- (ctx
- vars
-
The null
, it can set the response directly, by using the method
-SampleResult.setResponseData(data)
, where data is either a String or a byte array.
+sampleResult.setResponseData(data)
, where data is either a String or a byte array.
The data type defaults to "text
", but can be set to binary by using the method
-SampleResult.setDataType(SampleResult.BINARY)
.
+sampleResult.setDataType(SampleResult.BINARY)
.
-The SampleResult variable gives the script full access to all the fields and
+The sampleResult
variable gives the script full access to all the fields and
methods in the SampleResult. For example, the script has access to the methods
setStopThread(boolean)
and setStopTest(boolean)
.
Response
Currently the only way to changes these is via the SampleResult methods:
-SampleResult.setSuccessful(true/false)
-SampleResult.setResponseCode("code")
-SampleResult.setResponseMessage("message")
+sampleResult.setSuccessful(true/false)
+sampleResult.setResponseCode("code")
+sampleResult.setResponseMessage("message")
@@ -4798,7 +4800,7 @@ These are strings unless otherwise noted:
log
- the Logger Object. (e.g.) log.warn("Message"[,Throwable])
- SampleResult
, prev
- the SampleResult Object; read-write
+ sampleResult
, prev
- the SampleResult Object; read-write
Response
- the response Object; read-write
Failure
- boolean; read-write; used to set the Assertion status
FailureMessage
- String; read-write; used to set the Assertion message
@@ -4973,7 +4975,7 @@ vars.getObject("OBJ2");
props.get("START.HMS");
props.put("PROP1","1234");
-SampleResult
, prev
- (SampleResult ) - gives access to the previous SampleResult (if any)
+sampleResult
, prev
- (SampleResult ) - gives access to the previous SampleResult (if any)
sampler
- (Sampler ) - gives access to the current sampler
OUT
- System.out
- e.g. OUT.println("message")
AssertionResult
- (AssertionResult ) - the assertion result
diff --git a/xdocs/usermanual/functions.xml b/xdocs/usermanual/functions.xml
index 5e4078ea48c..dc179db872d 100644
--- a/xdocs/usermanual/functions.xml
+++ b/xdocs/usermanual/functions.xml
@@ -942,7 +942,7 @@ The following variables are set before the script is executed:
props
- JMeterProperties (class java.util.Properties
) object
threadName
- the threadName (String)
Sampler
- the current Sampler , if any
-SampleResult
- the current SampleResult , if any
+sampleResult
- the current SampleResult , if any
(*) means that this is set before the init file, if any, is processed.
Other variables vary from invocation to invocation.