bkone
bkone

Reputation: 251

Extracting string from jmeter response using regular expression extractor

I'm trying to extract the string (201 & 202) from the html response code below. So far I have tried the following regex

punumber=(.+)

but the problem is that there are many instances of the punumber on the page and gets me stuff that I dont need.
The string i need are inside the <h3 class="content-title">.

So can someone please help me write a regex to extract the punumber within the h3 class only?

<h3 class="content-title">
<!--  change when this is completed -->
    <a href="/container/recentIssue.jsp?punumber=201">
    Title 1
    </a>
</h3>

<h3 class="content-title">
<!--  change when this is completed -->
    <a href="/container/mostRecentIssue.jsp?punumber=202">
    Title 1
    </a>                                    
</h3>

Upvotes: 1

Views: 12953

Answers (3)

Aliaksandr Belik
Aliaksandr Belik

Reputation: 12873

You can possibly combine in this case XPath Extractor with structured query (to get all href values with punumber from ONLY instances inside <h3> tags) together with extracting then punumber value from href in ForEach Controller loop.

. . .
YOUR HTTP REQUEST
    XPath Extractor
    Use Tidy = true
    Reference Name = punum
    XPath Query = //h3[@class="content-title"]/a[text()="Title 1"]/@href
    Default value = NOT_FOUND
ForEach Controller
Input variable prefix = punum
Output variable name = pnum
Add "_" before number = true
    User Parameters
    cnt = ${__counter(FALSE,)}
    Regular Expression Extractor
    Apply to = Jmeter Variable = pnum
    Reference Name = punumber_${cnt}
    Regular Expression = punumber=(\d+)
    Template = $1$
    Match No. = 1
    Default value = NOT_FOUND
    ...
. . .
  1. XPath Extractor will give you hrefs values of all the <a> items under <h3> tag as punum_1,punum_2,...,punum_N vars.
  2. Foreach Controller takes one after another punum_X var, refers it as pnum, applies to it RegEx Extractor to get punumber value and stores extracted value as punumber_1, punumber_2,...,punumber_N (using counter defined in User Parameters and incremented each step).

NOTE: Since here XPath Extractor is used to parse HTML (not XML) response ensure that Use Tidy (tolerant parser) option is CHECKED (in XPath Extractor's control panel).

Same test-plan available here: http://db.tt/dnACZtGL (I've used @ant's one from his answer, thank him).

Upvotes: 0

ant
ant

Reputation: 22948

Here is the regex that works for me :

punumber=(\d+)

If you're parsing html you should consider using something else other than regex to extract info like jsoup.

Anyways here is the jmeter test file attached with dummy sampler(with regex post processor) simulating your case and debug sampler that gets the result you want.

http://pastebin.com/Uti8Pv9E

Upvotes: 2

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34566

This works for me:

Reference Name : test
Regexp : punumber=([^"]+?)"

Template : $1$

Match No : -1

(this will get all values) NV_punumber

With -1, JMeter will create:

  • ${test_1} => 201

  • ${test_2} => 202

Upvotes: 5

Related Questions