1

My goal is to use Regex to extract from an HTML document the value of a tag with a specific name. The relevant part of the code is:

<!-- hidden datas -->
        <p class="hidden">
            <input type="hidden" name="token" value="6ac2c9b7d56b483ad6b9db051a285637" />
            <input type="hidden" name="id_product" value="541" id="product_page_product_id" />
            <input type="hidden" name="add" value="1" />
            <input type="hidden" name="id_product_attribute" id="idCombination" value="" />
        </p>

I'll need to extract the alphanumerical characters 6ac2c9b7d56b483ad6b9db051a285637 Unfortunately, there is no other way than via Regex.

The same token is also present in another part of the document. Maybe it's easier to extract in that block of code?

        <script type="text/javascript" src="/webshop/js/tools.js"></script>
    <script type="text/javascript">
        var baseDir = '/webshop/';
        var static_token = '6ac2c9b7d56b483ad6b9db051a285637';
        var token = '1799f145490151b92137df1493a520cc';
        var priceDisplayPrecision = 2;
    </script>
Robin
  • 15
  • 1
  • 3

4 Answers4

1

I would recommend to get it using XPath Extractor as follows:

  1. Add XPath Extractor as a child of the request which returns that response
  2. Add something meaningful as a Reference Name - it'll be a JMeter Variable name holding the result, i.e. token
  3. If the response is not XHTML compliant check Use Tidy box
  4. Use the following XPath expression in `XPath Query" input:

    //input[@name='token']/@value
    
  5. Refer extracted value as ${token} where required.

For more information on XPath language see the following resources:

Using Regex can also work, however in case of complex multiline HTML it is better to use XPath or CSS/JQuery extractors. See the famous answer on StackOverflow for explanation.

Community
  • 1
  • 1
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

The following regex will capture the token in your second example:

/var static_token = '([0-9a-f]{32})';/

Tryth
  • 411
  • 2
  • 11
0

Use Regular expression extractor component of JMeter for extracting string, Expression will be,

static_token \= '(\w+)';

Template : $1$
Match no : 1 

This should extract value in specified variable.

Nachiket Kate
  • 8,473
  • 2
  • 27
  • 45
0

JMeter documentation has a example that closely matches what you want to achieve: http://jmeter.apache.org/usermanual/regular_expressions.html

In your case:

<input type="hidden" name="token" value="([^"]+)" />
ketan
  • 19,129
  • 42
  • 60
  • 98