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,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$
List<String> matches = new ArrayList<String>();
try{
if (isScopeVariable()){
String inputString=vars.get(getVariableName());
if(inputString != null) {
if(inputString.length()>0) {
Document d = parseResponse(inputString);
getValuesForXPath(d,getXPathQuery(),matches);
}
} else {
log.warn("No variable '"+getVariableName()+"' found to process by XPathExtractor '"+getName()+"', skipping processing");
}
} else {
List<SampleResult> samples = getSampleList(previousResult);
for (SampleResult res : samples) {
Document d = parseResponse(res.getResponseDataAsString());
getValuesForXPath(d,getXPathQuery(),matches);
}
}
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
final String errorMessage = "IOException on ("+getXPathQuery()+")";
log.error(errorMessage,e);
AssertionResult ass = new AssertionResult(getName());