0

Using Jmeter I was trying to extract the value of a token from the following, using the regular expression extractor:

<input name="__RequestVerificationToken" type="hidden" 
    value="BeRYiSIRjZoQHq4VW8qbkgXlnnzdUINpFNoYF_ugx-FRk0tkImbQPhwyYjyz_0Q-w6F2A0gDOfMZrdklD6rVn6-QnYggfImb55f90V7nrD_kbSkT3-y3gPqoTFg0ynTBLyX5Lw2" />

When I used the following expression:

name="__RequestVerificationToken" type="hidden" value="(.+?)"

the value was not extracted.

After a few searches I used the following expression:

name="__RequestVerificationToken" type="hidden" value="([A-Za-z0-9-_]+?)"  

which worked, but I don't know why :d.

My question: why the first expression didn't worked since basically tells to extract any character that matches one or more times.

DB5
  • 13,553
  • 7
  • 66
  • 71
SebastianG
  • 1
  • 1
  • 1
  • Use the regex tester in View Results Tree listener, and see what you get. I think the two statements are equivalent based on the input given. The ? is redundant unless there is following data. – CharlieS Oct 21 '14 at 23:50

2 Answers2

0

use this

name="__RequestVerificationToken" type="hidden"\s*value="(.+?)"

or the best is

name="__RequestVerificationToken" type="hidden"\s*value="([^"]*)"

Both of yours will not work as between type and value there is a \n which you have not taken care of.Now it works.See demo.

http://regex101.com/r/dK1xR4/14

vks
  • 67,027
  • 10
  • 91
  • 124
0

First of all, don't use Regular Expressions to extract data from HTML. It is complicated and very fragile in case of even slight DOM changes.

JMeter provides the following components to extract data from HTML responses:

  1. XPath Extractor
  2. CSS/JQuery Extractor

XPath Extractor Guide

  • Add Xpath Extractor as a child of the request which produces that response
  • Configure it as follows:
    • Reference name: anything meaningful, i.e. token
    • XPath query: //input[@name='__RequestVerificationToken']/@value
    • If your response is not valid XHTML check Use Tidy box
  • Refer to extracted value as ${token} or ${__V(token)} where required. Remember that JMeter Variables scope is limited to current thread group only.

For more information see Using the XPath Extractor in JMeter

CSS/JQuery Extractor Guide

  • Add CSS/JQuery Extractor as a child of the request which produces that authentication token response
  • Configure it as follows:
    • Reference name: anything meaningful, i.e. token
    • CSS/JQuery expression: input[name=__RequestVerificationToken]
    • Attribute: value
  • Refer to extracted value as ${token} or ${__V(token)} where required. Same restriction on JMeter Variables scope apply.

See JSoup selector syntax guide for a reference on how to build CSS selectors.

Hope this helps.

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