问题
I have a html page containing userIds of students. I need to extract all the userId values from this html page and use them in a parameter of JMeter HTTP request..
For example i have following data on htm page.
<input type="checkbox" StudentID="1503"
<input type="checkbox" StudentID="1504"
<input type="checkbox" StudentID="1505"
so on..
The value I need to send in parameter is like this selectedIds= 1503,1504,1505 and so on..
Now I am not sure how I can get the result that i need to send in my second request. I can extract & use one value at a time but could not figure out how to get values like I require "1503,1504,1505..."
Currently my Reg Exp extractor is like this
Reference Name: myTestVar Reg Expression: StudentID="(.+?)" Template: $1$ MatchNo: -1
In my second request I am referencing to variable ${myTestVar}
Please guide what else I need to do to get the results in desired way..
"1504,1505,1506"
回答1:
You can use BeanShell Postprocessor:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String response = prev.getResponseDataAsString();
String finalArr = "";
Pattern pattern = Pattern.compile("<input type=\"checkbox\" StudentID=\"(\\d+?)\"");
Matcher matcher = pattern.matcher(response);
while (matcher.find())
{
finalArr = finalArr + matcher.group(1) + ",";
}
Or if you are using Regex Extractor then also in Beanshell Postprocessor:
Integer N = Integer.valueOf(vars.get("myTestVar _matchNr"));
String finalArr = "";
for(Integer i = 1; i <= N; i++)
{
finalArr = finalArr + (vars.get("myTestVar "+String.valueOf(i)+"_g1")) + ",";
}
回答2:
Maybe late, but in 3.2+ you can put -1 in match number
br
回答3:
IN this case it is better to use CSS/JQuery extractor instead of regexp extractor:
CSS/JQuery expression : input
Attribute : StudentID
Reference : myId
Then you values would be in :
- myId_1
- myId_2 ...
- myId_N
And the number of references would be in :
- myId_matchNr
So to concatenate you can use Beanshell PostProcessor that will extract those, concat and store result in a new variables using:
vars.put("concat", )
来源:https://stackoverflow.com/questions/30894524/how-to-extract-all-of-reg-expression-matches-from-html-page-via-jmeter