From d837858e50625012bc96e4635699c5c815d36dd3 Mon Sep 17 00:00:00 2001 From: Pyrobow <64005534+Pyrobow@users.noreply.github.com> Date: Tue, 30 Apr 2024 20:09:17 +0100 Subject: [PATCH] Expose Additional JMeter Result Saver Properties in the DSL (#263) Co-authored-by: Will Dunlop --- .../core/listeners/ResponseFileSaver.java | 39 ++++++++++++++++++- .../core/listeners/ResponseFileSaverTest.java | 31 +++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java index e6e39d68..1e62210f 100644 --- a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java +++ b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java @@ -20,13 +20,16 @@ *

* By default, it will generate one file for each response using the given (which might include the * directory location) prefix to create the files and adding an incremental number to each response - * and an extension according to the response mime type. + * and an extension according to the response mime type. Both the incremental number and the + * extension can be set manually if skipAutoNumber and skipSuffix are set to true respectively. * * @since 0.13 */ public class ResponseFileSaver extends BaseListener { protected String fileNamePrefix; + protected boolean skipAutoNumber = false; + protected boolean skipSuffix = false; public ResponseFileSaver(String fileNamePrefix) { super("Save Responses to a file", ResultSaverGui.class); @@ -37,9 +40,41 @@ public ResponseFileSaver(String fileNamePrefix) { protected TestElement buildTestElement() { ResultSaver ret = new ResultSaver(); ret.setFilename(fileNamePrefix); + ret.setSkipAutoNumber(skipAutoNumber); + ret.setSkipSuffix(skipSuffix); return ret; } + + /** + * Allows specifying whether the ResponseFileSaver appends a number to the end of the generated file. + *

+ * By default, the ResponseFileSaver will add a number based on the samplers in the scope of the + * ResponseFileSaver test element. If set to true then no number will be appended. + * + * @param skipAutoNumber Boolean determining whether the number is added. + * @return the ResponseFileSaver for further configuration or usage. + */ + public ResponseFileSaver setSkipAutoNumber(boolean skipAutoNumber) { + this.skipAutoNumber = skipAutoNumber; + return this; + } + + + /** + * Allows specifying whether the ResponseFileSaver will append the file type to the file name. + *

+ * By default, the ResponseFileSaver will use the MIME type to append the file type to the end of the + * generated file. If this is set to true then no file type will be appended. + * + * @param skipSuffix Boolean determining whether a file type is added. + * @return the ResponseFileSaver for further configuration or usage. + */ + public ResponseFileSaver setSkipSuffix(boolean skipSuffix) { + this.skipSuffix = skipSuffix; + return this; + } + public static class CodeBuilder extends SingleTestElementCallBuilder { public CodeBuilder(List builderMethods) { @@ -49,7 +84,7 @@ public CodeBuilder(List builderMethods) { @Override protected MethodCall buildMethodCall(ResultSaver testElement, MethodCallContext context) { return buildMethodCall( - new TestElementParamBuilder(testElement).stringParam(ResultSaver.FILENAME)); + new TestElementParamBuilder(testElement).stringParam(ResultSaver.FILENAME)); } } diff --git a/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java b/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java index f8f999c7..656b7850 100644 --- a/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java +++ b/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java @@ -35,6 +35,37 @@ public void shouldWriteFileWithResponseContentWhenResponseFileSaverInPlan(@TempD assertThat(tempDir.resolve("response1.unknown")).hasContent(body); } + @Test + public void shouldWriteFileWithNoAddedNumberWithResponseContentWhenResponseFileSaverInPlanAndSkipAutoNumberTrue(@TempDir Path tempDir) throws Exception { + String body = "TEST BODY"; + ResponseFileSaver fileSaver = responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString()); + fileSaver.setSkipAutoNumber(true); + stubFor(any(anyUrl()).willReturn(aResponse().withBody(body))); + testPlan( + threadGroup(1, 1, + httpSampler(wiremockUri) + ), + fileSaver + ).run(); + assertThat(tempDir.resolve("response.unknown")).hasContent(body); + } + + @Test + public void shouldWriteFileWithNoAddedFileExtensionWithResponseContentWhenResponseFileSaverInPlanAndSkipAutoNumberTrue(@TempDir Path tempDir) throws Exception { + String body = "TEST BODY"; + ResponseFileSaver fileSaver = responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString()); + fileSaver.setSkipSuffix(true); + stubFor(any(anyUrl()).willReturn(aResponse().withBody(body))); + testPlan( + threadGroup(1, 1, + httpSampler(wiremockUri) + ), + fileSaver + ).run(); + assertThat(tempDir.resolve("response1")).hasContent(body); + } + + @Test public void shouldWriteOneFileForEachResponseWhenResponseFileSaverInPlan(@TempDir Path tempDir) throws Exception { testPlan(