1

I have created a User Defined Variable with name as "Status" and a default value as "Started".

I got a HTML Response, with following content:

<SPAN id="ApplicationStatus">&nbsp;Interrupted</SPAN>

I want to get the Span Id value and use in beanshell samplers to process further either in If Controller or Switch Controller.

I used Regular Expression extractor to extract the value needed and its working too.

But when i say vars.get("Status") will always return me the default value "Started".

Is there a way where i can extract the required value "Interrupted" and substitute that to the user defined variable "Status"?

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
user1876040
  • 431
  • 1
  • 7
  • 18

2 Answers2

1

Yes you can get that value of #ApplicationStatus into your User Defined Variable (UDV).

You can use regex but really you shouldn't for this type of parsing I'm not going to get into many reasons why.

Here is how you can do it using alternative (better solution IMHO) :

String html = "<SPAN id=\"ApplicationStatus\">&nbsp;Interrupted</SPAN>";

Document doc = Jsoup.parse(html);

String value = doc.select("#ApplicationStatus").first().text();
//Put value in UDV Status
vars.put("Status", value);

You can add this to your sampler that does this kind of parsing i.e Beanshell sampler, here are the imports (which go above this code) :

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

Please note that this code is Jsoup dependent so you will need to download jsoup jar and put it in your $JMETER_HOME/lib directory.

Hope this sheds some light on your issue.

Update

If you want to avoid Java, I've written small jmeter post processor component that extracts text value from HTML element. Take a look at :

https://github.com/c0mrade/Html-Extractor

If you go over the steps how to install the post processor from the page above, you would use it as follows :

Right click on your sampler. Add a Post Processors -> Html Extractor , in the jquery selector field write #ApplicationStatus and store result in variable of your choice (Status). Following this add Debug Sampler, if in your Debug sampler there is variable Status with the value Html Extractor is working! you're done!

ant
  • 22,634
  • 36
  • 132
  • 182
  • Why do you propose to use Jsoup instead regexes? Usually when writing performance test, you know precise structure of HTML. Using Jsoup will likely to [make your test more resource intensive for master machine](http://stackoverflow.com/a/13766154/841064). It will make you using more machines for generating higher load – Andrei Botalov Dec 07 '12 at 16:21
  • @AndreyBotalov nowhere in his question does he state that he is doing performance testing. Nowhere in the link you provided do you or anybody else offers a shred of evidence that "Jsoup will likely make your test resource intensive for master machine". Just because PMD says it, doesn't make it true. Show me some metrics and I'll congradulate to both of you. In the meantime I don't even see how your comment relies to my answer. I answered this question in correct matter, this approach works. It's ok if you disagree with my answer as I happen to disagree with yours but doesn't make it incorrect. – ant Dec 07 '12 at 20:03
  • As to why do I propose Jsoup instead of regex. Well the reason is simple, main purpose of Jsoup is to parse HTML from Java. For XML parsing you should use xml parser or in the jemeter case xpath extractor. Regex will work as well in some cases for HTML, the purpose of regex is not to be used for HTML, it's just too hard to use it. You obviously want to make your life easier by using tools for what they were designed for. Also embedding Java code is not very good as much test personnel don't want get tangled with Java code so you have to choose which tools to use for what purpose. See my update – ant Dec 07 '12 at 20:56
-1

I can't reproduce your issue.

Here is my plan:

- User defined variables with variable Status
- Thread Group
  - HTTP Request
    - Regular expression extractor with reference name = Status
  - Beanshell Sampler that logs Status variable

Beanshell sampler logs value that was received in Regular Expression Extractor

Andrei Botalov
  • 20,686
  • 11
  • 89
  • 123