You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Yanroy <rm...@wpi.edu> on 2006/07/31 22:45:59 UTC
using regex with beanshell
Hi all,
Upon the advice of the people on this list, I've started using beanshell to
do the complex parts of my jmeter test. So far, this seems like a really
good idea. I'd read that beanshell was a terrible language, but after a
little experience with it, I think it's great (I suspect the person who
didn't like it was objecting to the lax type mode and strange class
declaration syntax). Anyways, I need to make beanshell look at the results
of an HTTP sample and perform conditional operations based on the data
contained in it. To that end, I need to know how to use a regex from inside
beanshell. Is there a way to use jmeter's regular expression extractor
element (or at least functionality) from inside beanshell? A google search
has returned dismally little info on using beanshell with jmeter.
Also, I have another related question... does a beanshell assertion only
apply to its parent sampler? I ask this because I'd like to have 2 samplers
that both share a single beanshell assertion. I'm hoping the assertion
actually responds to the most recent request rather than the parent in the
tree.
Thanks for all your help,
Ryan (aka Yanroy)
--
View this message in context: http://www.nabble.com/using-regex-with-beanshell-tf2029950.html#a5583703
Sent from the JMeter - User forum at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: using regex with beanshell
Posted by sebb <se...@gmail.com>.
I think you can achieve the same result by using the RE Assertion:
Assistment: \((\d+)\)
S
On 01/08/06, Yanroy <rm...@wpi.edu> wrote:
>
> Actually, yes, just moments ago I finally came up with a solution. The code
> isn't very pretty (I suspect beanshell never is), but it does what I need it
No uglier than Java ;-)
> to do. Hopefully it'll help you too. I use the java regex library to parse
> the result of a jmeter sampler. This code is what I typed into a beanshell
> assertion element in jmeter:
>
> import java.util.regex.*;
>
> String pStr = ".*?Assistment: \\((\\d+?)\\).*";
> print("Regex Pattern: " + pStr);
> Pattern idPattern = Pattern.compile(pStr, Pattern.UNIX_LINES |
> Pattern.DOTALL);
> Matcher idMatcher =
> idPattern.matcher(SampleResult.getResponseDataAsString());
> boolean matched = idMatcher.matches();
>
> if (matched) {
> print("Matched ID: " + idMatcher.group(1));
> }
> else {
> print("didn't match id");
> Failure = true;
> FailureMessage = "Couldn't extract ID from question";
> }
>
> [end code]
>
> This matches the string "Assistment: (2100)" and extracts 2100 from it (or
> any other number). That string is embedded in the HTML of a page. I
> specify the UNIX_LINES flag to the regex because I'm using linux... idk if
> it's really necessary. Also, you'll note the plethora of backslashes...
> this is because the java/beanshell compiler escapes the backslash characters
> once, then the regex engine does it again. Of course, this code doesn't do
> anything useful other than fail the assertion if it couldn't find the
> number. Have fun with it.
>
> Ryan
> --
> View this message in context: http://www.nabble.com/using-regex-with-beanshell-tf2029950.html#a5602388
> Sent from the JMeter - User forum at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: using regex with beanshell
Posted by Yanroy <rm...@wpi.edu>.
Actually, yes, just moments ago I finally came up with a solution. The code
isn't very pretty (I suspect beanshell never is), but it does what I need it
to do. Hopefully it'll help you too. I use the java regex library to parse
the result of a jmeter sampler. This code is what I typed into a beanshell
assertion element in jmeter:
import java.util.regex.*;
String pStr = ".*?Assistment: \\((\\d+?)\\).*";
print("Regex Pattern: " + pStr);
Pattern idPattern = Pattern.compile(pStr, Pattern.UNIX_LINES |
Pattern.DOTALL);
Matcher idMatcher =
idPattern.matcher(SampleResult.getResponseDataAsString());
boolean matched = idMatcher.matches();
if (matched) {
print("Matched ID: " + idMatcher.group(1));
}
else {
print("didn't match id");
Failure = true;
FailureMessage = "Couldn't extract ID from question";
}
[end code]
This matches the string "Assistment: (2100)" and extracts 2100 from it (or
any other number). That string is embedded in the HTML of a page. I
specify the UNIX_LINES flag to the regex because I'm using linux... idk if
it's really necessary. Also, you'll note the plethora of backslashes...
this is because the java/beanshell compiler escapes the backslash characters
once, then the regex engine does it again. Of course, this code doesn't do
anything useful other than fail the assertion if it couldn't find the
number. Have fun with it.
Ryan
--
View this message in context: http://www.nabble.com/using-regex-with-beanshell-tf2029950.html#a5602388
Sent from the JMeter - User forum at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: using regex with beanshell
Posted by paraglade <pa...@yahoo.com>.
I have a similar issue your first problem. Have you been able to come up
with any solutions?
--
View this message in context: http://www.nabble.com/using-regex-with-beanshell-tf2029950.html#a5601684
Sent from the JMeter - User forum at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: using regex with beanshell
Posted by sebb <se...@gmail.com>.
On 31/07/06, Yanroy <rm...@wpi.edu> wrote:
>
> Hi all,
> Upon the advice of the people on this list, I've started using beanshell to
> do the complex parts of my jmeter test. So far, this seems like a really
> good idea. I'd read that beanshell was a terrible language, but after a
> little experience with it, I think it's great (I suspect the person who
> didn't like it was objecting to the lax type mode and strange class
> declaration syntax). Anyways, I need to make beanshell look at the results
> of an HTTP sample and perform conditional operations based on the data
> contained in it. To that end, I need to know how to use a regex from inside
> beanshell.
Or use the exisiting RE to extract the information, and BeanShell to process it.
> Is there a way to use jmeter's regular expression extractor
> element (or at least functionality) from inside beanshell? A google search
> has returned dismally little info on using beanshell with jmeter.
The RE is designed to put the results in variables; it is not designed
as a utility module so it would probably not offer any benefit to call
it from BeanShell.
You can use Java RE or Jakarta ORO directly from BeanShell, but I
suspect you would be better off using the JMeter RE Post-Processor in
conjunction with BeanShell.
> Also, I have another related question... does a beanshell assertion only
> apply to its parent sampler? I ask this because I'd like to have 2 samplers
> that both share a single beanshell assertion. I'm hoping the assertion
> actually responds to the most recent request rather than the parent in the
> tree.
Assertions are applied to all samples in their scope. The scope of a
child element is its (sole) parent.
> Thanks for all your help,
> Ryan (aka Yanroy)
> --
> View this message in context: http://www.nabble.com/using-regex-with-beanshell-tf2029950.html#a5583703
> Sent from the JMeter - User forum at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org