Here are some helpful copy & paste JMeter examples.
Get a value (here I will use Spring Web Flow executionId
) from URL
Regular Expression
Extractor - Main Sample / URL:
- Reference Name:
executionId
(or similar),
- Regular Expression:
[?&]execution=([^&]+)
,
- Template:
$1$
- Match No:
1
,
- Default Value: any (eg.
NOTFOUND
),
Get Spring Web Flow executionId
from a form
Here's our HTML snippet #1:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head><!-- (...) --></head>
<body class="tundra">
<div id="page" class="container">
<div id="header"><!-- (...) --></div>
<div id="content">
<div id="main" class="span-18 last">
<!-- (...) -->
<div id="bookingForm">
<div class="span-12">
<form id="booking" action="/hotels/booking?execution=e1s1" method="post">
<fieldset>
<legend>Book Hotel</legend>
<!-- (...) -->
<p><input type="text" id="checkinDate" name="checkinDate" /></p>
<p>
<button type="submit" id="proceed" name="_eventId_proceed">Proceed</button>
<button type="submit" name="_eventId_cancel">Cancel</button>
</p>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
<!-- (...) -->
</div>
</body>
</html>
- With a Regular
Expression Extractor - Main Sample / Body:
- Reference Name:
formExecutionId
(or similar),
- Regular Expression:
[?&]execution=([^&"]+)
- Template:
$1$
,
- Match No:
1
,
- Default Value: any (eg.
NOT_FOUND
),
- With an XPath and Regular
Expression (this is a 2-step process):
- STEP 1 - Extract form action with XPath:
- Main sample only,
- Use Tidy, Quiet
- Reference Name:
xpathAction
,
- XPath query:
//div[@id='bookingForm']//form[@id='booking']/@action
(see HTML above),
- Default Value:
NOT_FOUND
(anything meaningful),
- We have the
action
value of the form now,
- STEP 2 - Extract the executionId from the action, using Regular Expression Extractor:
- Apply to: JMeter Variable / xpathAction,
- Reference Name:
xpathExecutionId
,
- Regular Expression:
execution=([^&]+)
,
- Template:
$1$
,
- Match No.:
1
,
- Default Value:
NOT_FOUND
,
- With a BeanShell
Post-processor script:
text = prev.responseDataAsString;
patternText = "(.*)execution=([^&\"]+)(.*)";
p = java.util.regex.Pattern.compile(patternText, java.util.regex.Pattern.DOTALL);
m = p.matcher(text);
if (m.find()) {
vars.put("beanShellExecutionId", m.group(2));
} else {
vars.put("beanShellExecutionId", "NOT_FOUND");
}
This will export the execution id to beanShellExecutionId
.
- And here's a BeanShell
assertion example:
text = SampleResult.responseDataAsString;
patternText = "(.*)execution=([^&\"]+)(.*)";
p = java.util.regex.Pattern.compile(patternText, java.util.regex.Pattern.DOTALL);
m = p.matcher(text);
if (m.matches()) {
log.info("found beanShellExecutionId value: " + m.group(2));
} else {
Failure = true;
FailureMessage = "This BeanShell assertion has failed";
}
- BeanShell variables are:
Thanks for great article. It also worth checking How to use BeanShell: JMeter's favorite built-in component for advanced scripting guidelines and examples of making calls to JMeter API from Beanshell test elements and Beanshell cookbook.
ReplyDeleteSkvělé! Děkujeme vám za poskytnutí příkladu xpath jsem hledal! (na zdraví k Pat a Mat :))
ReplyDelete