Dear sebb and JMeter Users:
With advice from sebb and some head-scratching, I now have a way to count the number
of regular expression matches in a HTTP response and stop the test plan if the correct
number is not found. Attached find a short sample JMeter script called "Stop test for
incorrect count.jmx". The sample uses the Google home page. In it, a Regular
Expression Extractor counts the number of instances of a string on the Google home
page, and stores the number in the "refName_matchNr" variable. An If controller stops
the test plan if the number is not the correct number.
sebb's suggestion to use the following structure to stop the test plan is perfect for
this application:
If Controller
Java Request Sampler
Result Status Action Handler Post-Processor
In the JavaTest Sampler, set the Status parameter to "FAIL". In the Result Status
Action Handler Post-Processor, set the "Action to be take after a Sampler error"
option to "Stop Test".
This is much better than my former work-around for stopping the test plan. Under the
If Controller, I had an HTTP Request for the Google home page. (Google is handy in
more ways than one.) Under it was a Response Assertion that checked for a string that
does not appear in the Google home page. So the assertion would always fail, thus
stopping the test plan.
sebb, I experimented with using ".*" in the regular expression for counting desired
instances. It certainly would have been more elegant. Unfortunately, the target web
pages contain many lines; using ".*" instead of [\w|\W]+ seemed to work only if the
response was all on one line. Your suggestion of looking into negative look-ahead and
negative look behind is intriguing, but the press of work demands I move on for the
moment.
Sincerely yours,
Kyle
-----Original Message-----
From: sebb [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 13, 2004 6:56 PM
To: Kyle McAbee
Cc: JMeter Users List
Subject: Re: Can number of regular expression matches be tested ?
On Wed, 13 Oct 2004 16:55:09 -0400, Kyle McAbee <[EMAIL PROTECTED]> wrote:
> Dear sebb and JMeter Users:
>
> sebb explained that an If Controller element will not stop the test plan, because
> the If Controller affects only its child elements, not all elements "below" it.
> (Thank you, sebb.)
This is basically true of all Controllers.
> Following sebb's suggestion to explore regular expressions again, I developed the
> workaround below. Is there is a better solution?
>
> To test for an exact number of regular expression matches that may occur anywhere in
> the HTTP response text, add two Response Assertion elements under the HTTP Response
> element.
This is a good approach.
> 1. In one Response Assertion element, use a regular expression that detects an exact
> number of matches in the HTTP response text. Set the Pattern Matching Rules option
> to Contains. For example, to detect exactly two matches of "target":
>
No, it will match two *or more* targets when using Contains.
If using Matches, the same applies, but at least one target must be at the end.
> [\W|\w]+(target)[\W|\w]+(target)
>
> This Response Assertion will stop the test plan if the HTTP response text contains
> fewer than the exact number of matches.
If there aren't at least two targets, it will not match.
Not sure you need the "[\W|\w]" construct. ".*" would be simpler.
And you don't need the leading one, you could just use
(target).*(target)
[] encloses a character class - in this case, both non-Word and Word
characters as well as | which is already in \W.
> 2. In another Response Assertion element, use a regular expression that detects the
> exact number of matches PLUS ONE in the HTTP response text. Set the Pattern Matching
> Rules option to Not. For example, to detect three matches of "target":
>
> [\W|\w]+(target)[\W|\w]+(target)[\W|\w]+(target)
>
> This Response Assertion will stop the test plan if the HTTP response text contains
> more than the exact number of matches.
As above, this could be expressed as
(target).*(target).*(target)
which would match three or more targets.
However, by using negative look-ahead and negative look behind in the
response assertion, I would have thought it should be possible to use
just one assertion - using Matches and Not.
I leave that as an exercise for the reader ... (at least for the time being)
The double Assertion approach is a bit like testing for the value "2"
(say) by seeing if it is not less than 2 and not more than 2 - useful
in this case where testing for equality is more difficult.
> The refName_matchNr variable contains the number of matches extracted by a Regular
> Expression Extractor element. For future reference:
>
> 1. Is there a way to compare the value of refName_matchNr variable to a desired
> number to control test plan execution other than by using an If Controller element?
It's possible, but awkward currently.
The If controller can check variable names, as can the __javaScript()
and __BeanShell() functions.
The BeanShell function would allow you to call the stopThread() or
stopTest() methods. For sample scripts have a look at the bin
directory in the 2.0 branch in CVS, or one of the recent nightlies.
One way to use the If controller would be to add a JavaTest sampler to
it, and set the Status to FAIL (i.e. not OK). Add a Result Status
Action Handler Post-Processor, and you can stop the thread or the test
as part of the If Controller execution. Messy, but it should work.
> 2. Does the __regexFunction create a refName_matchNr variable? (Section 16.5.1 in
> the user manual does not say it does.)
It does now, and the functions reference page has been updated to say
so. This will be in release 2.0.2 (or it's in recent nightly 2.0
builds).
> Sincerely yours,
>
> Kyle
>
>
>
> -----Original Message-----
> From: sebb [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, October 12, 2004 6:20 PM
> To: JMeter Users List
> Subject: Re: Can number of regular expression matches be tested ?
>
> On Tue, 12 Oct 2004 17:16:01 -0400, Kyle McAbee <[EMAIL PROTECTED]> wrote:
> > Dear JMeter Users:
> >
> > Is there a way to test the number of matches extracted by a Regular Expression
> > Extractor element?
>
> Yes, the xxx_matchNr variable
>
> > The following code works. Unfortunately, it ALWAYS works. I used a regular
> > expression tester to make sure that the target string occurs 4 times in the HTTP
> > response text. If I understand the manual, when the condition in the If Controller
> > evaluates to false, the test plan should stop. So specifying the wrong number of
> > matches in the condition should stop the test plan. But it does not.
>
> The If controller only controls whether or not its children are
> invoked. It does not stop a test, nor does it set the status of the
> sampler to failed.
>
> If you can work out the appropriate regex, you could use the Regex
> Assertion to mark the sample as failed, and cause the test to stop
> that way.
>
> > HTTP Request
> > Regular Expression Extractor element
> > Reference Name: Matches
> > Regular Expression: string
> > Template: $1$
> > Match No: 1
> > Default Value: NoMatches
> >
> > If Controller element
> > Condition: Matches_matchNr == 4
>
> This means to invoke any child samples of the If controller
>
> > Sincerely yours,
> >
> > Kyle
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]