in src/components/src/main/java/org/apache/jmeter/extractor/XPathExtractor.java [107:193]
public void process() {
JMeterContext context = getThreadContext();
final SampleResult previousResult = context.getPreviousResult();
if (previousResult == null){
return;
}
JMeterVariables vars = context.getVariables();
String refName = getRefName();
vars.put(refName, getDefaultValue());
final String matchNR = concat(refName,REF_MATCH_NR);
int prevCount=0; // number of previous matches
try {
prevCount=Integer.parseInt(vars.get(matchNR));
} catch (NumberFormatException e) {
// ignored
}
vars.put(matchNR, "0"); // In case parse fails // $NON-NLS-1$
vars.remove(concat(refName,"1")); // In case parse fails // $NON-NLS-1$
int matchNumber = getMatchNumber();
List<String> matches = new ArrayList<>();
try{
if (isScopeVariable()){
String inputString=vars.get(getVariableName());
if(inputString != null) {
if(inputString.length()>0) {
Document d = parseResponse(inputString);
getValuesForXPath(d,getXPathQuery(), matches, matchNumber);
}
} else {
if (log.isWarnEnabled()) {
log.warn("No variable '{}' found to process by XPathExtractor '{}', skipping processing",
getVariableName(), getName());
}
}
} else {
List<SampleResult> samples = getSampleList(previousResult);
for (SampleResult res : samples) {
Document d = parseResponse(res.getResponseDataAsString());
getValuesForXPath(d,getXPathQuery(), matches, matchNumber);
}
}
final int matchCount = matches.size();
vars.put(matchNR, String.valueOf(matchCount));
if (matchCount > 0){
String value = matches.get(0);
if (value != null) {
vars.put(refName, value);
}
for(int i=0; i < matchCount; i++){
value = matches.get(i);
if (value != null) {
vars.put(concat(refName,i+1),matches.get(i));
}
}
}
vars.remove(concat(refName,matchCount+1)); // Just in case
// Clear any other remaining variables
for(int i=matchCount+2; i <= prevCount; i++) {
vars.remove(concat(refName,i));
}
}catch(IOException e){// e.g. DTD not reachable
log.error("IOException on ({})", getXPathQuery(), e);
AssertionResult ass = new AssertionResult(getName());
ass.setError(true);
ass.setFailureMessage("IOException: " + e.getLocalizedMessage());
previousResult.addAssertionResult(ass);
previousResult.setSuccessful(false);
} catch (ParserConfigurationException e) {// Should not happen
final String errrorMessage = "ParserConfigurationException while processing ("+getXPathQuery()+")";
log.error(errrorMessage,e);
throw new JMeterError(errrorMessage,e);
} catch (SAXException e) {// Can happen for bad input document
if (log.isWarnEnabled()) {
log.warn("SAXException while processing ({}). {}", getXPathQuery(), e.getLocalizedMessage());
}
addAssertionFailure(previousResult, e, false); // Should this also fail the sample?
} catch (TransformerException e) {// Can happen for incorrect XPath expression
if (log.isWarnEnabled()) {
log.warn("TransformerException while processing ({}). {}", getXPathQuery(), e.getLocalizedMessage());
}
addAssertionFailure(previousResult, e, false);
} catch (TidyException e) {
// Will already have been logged by XPathUtil
addAssertionFailure(previousResult, e, true); // fail the sample
}
}