apache / apache/jmeter

Add ability for RegexExtractor to fail sampler result if there is no match

Open
#3,364 8 comments 0 reactions 0 assignees View on GitHub
enhancement os: All P2 wontfix
Dominant language
Java
Stars
9.5k
Forks
2.3k
Avg merge
1d 22h
Merged PRs (30d)
5

Description

**Michael Zhilin** ([Bug 56431](https://bz.apache.org/bugzilla//show_bug.cgi?id=56431&redirect=false)):
Often there is need to stop iteration / fail sampler result if RegexExtractor doesn't find matches. To resolve this problem usually we need to add Assertion together with RegexExtractor. It duplicates code, increase processing time and make script's author less happy.

This fix is very simple, it makes RegexExtractor to add Assertion failure in case of regexp mismatch.

Also fix is posted on github: https://github.com/Cka3o4Huk/jmeter/commit/47a46ba39ddd4d3470869d6821336d59d410de02

Created attachment [fix.diff](https://apache.github.io/jmeter-bugzilla-attachments/31/56431/31542/fix.diff): SVN Diff
fix.diff

````diff
Index: src/components/org/apache/jmeter/extractor/RegexExtractor.java
===================================================================
--- src/components/org/apache/jmeter/extractor/RegexExtractor.java (revision 1588500)
+++ src/components/org/apache/jmeter/extractor/RegexExtractor.java (working copy)
@@ -23,7 +23,9 @@
import java.util.Collections;
import java.util.List;

+import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.processor.PostProcessor;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.AbstractScopedTestElement;
@@ -84,6 +86,10 @@

private static final String UNDERSCORE = "_"; // $NON-NLS-1$

+ private static final String FAILIFNOTFOUND = "RegexExtractor.fail_if_not_found";
+
+ private static final String FAILMESSAGE = "RegexExtractor.fail_message";
+
private transient List template;

/**
@@ -117,6 +123,10 @@
try {
pattern = JMeterUtils.getPatternCache().getPattern(regex, Perl5Compiler.READ_ONLY_MASK);
List matches = processMatches(pattern, regex, previousResult, matchNumber, vars);
+
+ if(matches.isEmpty() && isFailIfNotFound()){
+ failResult(previousResult);
+ }
int prevCount = 0;
String prevString = vars.get(refName + REF_MATCH_NR);
if (prevString != null) {
@@ -169,6 +179,14 @@
}
}

+ private void failResult(SampleResult previousResult){
+ previousResult.setSuccessful(false);
+ AssertionResult res = new AssertionResult(getName());
+ res.setFailure(true);
+ res.setFailureMessage(StringUtils.defaultIfEmpty(getFailMessage(), "Pattern not found - " + getRegex()));
+ previousResult.addAssertionResult(res);
+ }
+
private String getInputString(SampleResult result) {
String inputString = useUrl() ? result.getUrlAsString() // Bug 39707
: useHeaders() ? result.getResponseHeaders()
@@ -469,4 +487,21 @@
public void setUseField(String actionCommand) {
setProperty(MATCH_AGAINST,actionCommand);
}
+
+ public void setFailIfNotFound(Boolean isFailing){
+ log.warn("Setting fail as " + isFailing);
+ setProperty(FAILIFNOTFOUND,isFailing);
+ }
+
+ public boolean isFailIfNotFound(){
+ return getPropertyAsBoolean(FAILIFNOTFOUND);
+ }
+
+ public void setFailMessage(String message){
+ setProperty(FAILMESSAGE, message);
+ }
+
+ public String getFailMessage(){
+ return getPropertyAsString(FAILMESSAGE);
+ }
}
Index: src/components/org/apache/jmeter/extractor/gui/RegexExtractorGui.java
===================================================================
--- src/components/org/apache/jmeter/extractor/gui/RegexExtractorGui.java (revision 1588500)
+++ src/components/org/apache/jmeter/extractor/gui/RegexExtractorGui.java (working copy)
@@ -26,6 +26,7 @@
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
+import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
@@ -68,6 +69,10 @@
private JRadioButton useMessage;

private ButtonGroup group;
+
+ private JCheckBox failResult;
+
+ private JLabeledTextField failMessage;

public RegexExtractorGui() {
super();
@@ -97,6 +102,8 @@
defaultField.setText(re.getDefaultValue());
matchNumberField.setText(re.getMatchNumberAsString());
refNameField.setText(re.getRefName());
+ failResult.setSelected(re.isFailIfNotFound());
+ failMessage.setText(re.getFailMessage());
}
}

@@ -127,6 +134,8 @@
regex.setTemplate(templateField.getText());
regex.setDefaultValue(defaultField.getText());
regex.setMatchNumber(matchNumberField.getText());
+ regex.setFailIfNotFound(failResult.isSelected());
+ regex.setFailMessage(failMessage.getText());
}
}

@@ -154,10 +163,21 @@
box.add(makeTitlePanel());
box.add(createScopePanel(true));
box.add(makeSourcePanel());
+ box.add(makeNotMatch());
add(box, BorderLayout.NORTH);
add(makeParameterPanel(), BorderLayout.CENTER);
}

+ private JPanel makeNotMatch(){
+ JPanel panel = new JPanel(new BorderLayout(5, 0));
+ panel.setBorder(BorderFactory.createTitledBorder("Matching options"));
+ failResult = new JCheckBox("Fail if nothing found with message");
+ failMessage = new JLabeledTextField();
+ panel.add(failResult, BorderLayout.WEST);
+ panel.add(failMessage, BorderLayout.CENTER);
+ return panel;
+ }
+
private JPanel makeSourcePanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(JMeterUtils.getResString("regex_source"))); //$NON-NLS-1$
````

OS: All

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.