简体   繁体   中英

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>

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

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

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.

. . .

    
    Use Tidy = true
    Reference Name = punum
    XPath Query = //h3[@class="content-title"]/a[text()="Title 1"]/@href
    Default value = NOT_FOUND

Input variable prefix = punum
Output variable name = pnum
Add "_" before number = true
    
    cnt = ${__counter(FALSE,)}
    
    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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM