Jmeter Regular expression for extracting - regex

Currently I'm doing a course on JMeter and I am stuck on something with which I would be grateful if you could point me in the right direction.
Using the regular expression extractor I am trying to write a regular expression to extract the values from drop down lists for parameterisation reasons. However, using my expression seems to extract everything on the page which has "option value" in that page i.e i am unable to make the expression to be specific for a particular drop down? many thanks
I have tried:
OPTION VALUE="([A-Za-z]+)"
The following is the relevant HTML:
<SELECT NAME="fromPort">
<OPTION VALUE="Acapulco">Acapulco
<OPTION VALUE="Frankfurt">Frankfurt
<OPTION VALUE="London">London
<OPTION VALUE="New York">New York
<OPTION VALUE="Paris">Paris
<OPTION VALUE="Portland">Portland
<OPTION VALUE="San Francisco">San Francisco
<OPTION VALUE="Seattle">Seattle
<OPTION VALUE="Sydney">Sydney
<OPTION VALUE="Zurich">Zurich
</SELECT>

This is a poor situation for using the Regex extractor because regular expressions are for matching specific strings.
You want to match based on the DOM so try the XPath extractor instead.
A rough example for your situation would be:
/html/body/select[#name="fromPort"]/option/text()
A tutorial here if you'd like some help:
http://blazemeter.com/blog/using-xpath-extractor-jmeter-0

Don't use Regular Expressions to extract HTML data. See RegEx match open tags except XHTML self-contained tags for details.
JMeter offers 2 Post Processors designed for dealing with HTML data.
XPath Extractor
Add XPath Extractor as a child of the request which is returning your port data configured as follows:
Reference name: anything meaningful, i.e. port. It'll be variable or variable prefix for matches results
XPath query: //select[#name='fromPort']/option/text()
Important: if your response is not XML/HTML compliant - check Use Tidy box, it enables XPath Extractor to parse invalid XML/XHTML responses.
The output will be like:
port=Acapulco
port_1=Acapulco
port_10=Zurich
port_2=Frankfurt
port_3=London
port_4=New York
port_5=Paris
port_6=Portland
port_7=San Francisco
port_8=Seattle
port_9=Sydney
port_matchNr=10
CSS/JQuery_Extractor
Another options is using CSS/JQuery extractor which allows using CSS and/or JQuery expressions to fetch interesting parts of the response.
For example, if you're looking for random port following configuration may be helpful:
Reference name: again, something meaningful, for instance port
CSS/JQuery expression: select[name=fromPort] > option
Attribute: value
Hope this helps.

Related

Extract a value from actionlink html response body - JMeter

I have a response body from one http request and I have to extract all the values and as input to another http request
<a class="action-link"
regionName="region name"
jName="a country"
alertID="179"
onclick="showDetail(this)"> Snapshot- v4 (Active)
</a>
i tried with regex like
created a post processors -> regular expression extractors
Apply to -> Main Sample Only
Field to check -> Body
Name of created variable -> regionNameVariable
Regular Expression -> <a class="action-link" regionName=(.*?)
and passed the value as ${regionNameVariable} in the next request.
But It is not working.
Can someone suggest the correct way of doing this.
Be aware that using regular expressions for parsing HTML is not a very good idea, consider using CSS Selector Extractor instead, the relevant configuration would be:
Name of created variable: regionNameVariable
CSS Selector Expression: a[class=action-link]
Attribute: regionName
Demo:
More information:
CSS Selector Reference
How to Use the CSS/JQuery Extractor in JMeter
Add multiple Regular expression extractor to extract each values
For example to extract the 3rd group in regex of Region Name(You can write better Regx if you have better idea)
Like Wise Add Regex for jName and alertID
(jName)(=)\"(.+)\"
(alertID)(=)\"(.+)\"
Then Pass on the Reference name as user variable in your next http request.as ${regionName} and so on.
If you want to do it in the same Regex post Processor. Please refer How to extract multiple values with a regular expression in Jmeter

Need a regexr to fetch the multiple data

I need a regrex to get the value in the below format
Code=ABC&cellNo=314&payType=0&transmission=MANUAL&vendorId=ET&toggleSelctd=0&uniqueId=39
This is a sample code is attached in the below screenshot
but however the regrex which i have created fetches data for one value but i need the same in the above format, for example below are the regrex.
name="cellNo" type="hidden" value="(.+?)" value="(.+?)
name="transmission" type="hidden" value="(.+?)"
Thanks
Don't use regular expressions to parse HTML. JMeter offers CSS/JQuery Extractor for fetching data from the HTML pages so you should be able to extract the required values using the following configurations:
For MANUAL:
Reference Name: anything meaningful, i.e. transmission
CSS/JQuery Expression: input[id=transmission]
Attribute: value
For 314
Reference Name: anything meaningful, i.e. CellNo
CSS/JQuery Expression: input[id=CellNo]
Attribute: value
See How to Use the CSS/JQuery Extractor in JMeter article for more information.
I think response markup is a little bit flaky as HTML input cannot have 2 name and 2 value tags, I would recommend using JMeter's HTML Assertion or online HTML Validator to check response data and raise issues on validation errors.

Specific xPath and Regex - Web Crawling

I'm currently in the process of trying to scrape a website. The problem is the information is placed on google maps in an iframe. Specifically, Latitude and Longitude.
I'm able to get all the other information I currently need expect this. Searching around, and working with import.io tech support, I found I need to use specific xPath and Regex to pull this information but the code I found on the site has me lost. Ideally I'd like to pull Latitude and Longitude separately. This is the code I have to work with.
What are my options? Thank you.
<div class="padding-listItem--sm">
<iframe width="100%" height="310" frameborder="0" allowfullscreen="" src="https://www.google.com/maps/embed/v1/place?q=33.3929503,-111.908652&key=AIzaSyDK08tC4NRubbIiw-xwDR1WEp-YAXX1Mx8" style="border:0"></iframe>
</div>
1) Get the src attribute of the iframe element.
string srcText = driver.findElement(By.tagName("iframe")).getAttribute("src");
2) Parse the url (found in srcText) for the latitude and longitude values.
Regex to find both numbers:
/([-]?\d+\.\d+)/g
when the url is as you specified:
https://www.google.com/maps/embed/v1/place?q=33.3929503,-111.908652&key=AIzaSyDK08tC4NRubbIiw-xwDR1WEp-YAXX1Mx8"
The XPath to obtain the iframe source is:
//div[#class='padding-listItem--sm']/iframe/#src
Then you can apply a regex like this one to obtain latitude and longitude
/q=(-?[\d\.]*),(-?[\d\.]*)/g
Implementation online Here

Jmeter Regular Expression Extraction

Could someone help me in getting the value "1237857346" from the following using regex or any other way I could get the value "1237857346" in from HTML in JMeter.
<select class="card_account" name="existing__account">
<option value="" disabled="disabled">Card Number</option>
<option value="1237857346" selected="selected">************4567</option>
</select>
Little bit of background. I am using JMeter and trying to extra the value "1237857346" to pass it in the next request.
It is not very good idea to parse HTML using Regular Expressions as it evidenced by the famous Stack Overflow answer
I would suggest switching to XPath Extractor instead. Add the XPath Extractor as a child of HTTP Request sampler which returns that select and configure it as follows:
XML Parsing Options: tick Use Tidy box. It may not be necessary but if your server response is not XML/XHTML compliant you'll get nothing
Reference Name: anything meaningful, i.e. value - it will be the name of the variable holding extracted data
XPath Expression: //select[#class='card_account']/option[#selected='selected']/#value - it will take
select having class = card_account
option with selected = "selected"
value attribute of the above option
and store it to "value" variable. You will be able to refer to it as ${value} where required.
See following material for further reference:
XPath Tutorial
XPath Language Specification
Using the XPath Extractor in JMeter
You can use the following regex:
<option[^<]*>Card Number</option>\s*<option[^<]*?value="(\d+)"
The value will be in group 1 ($1$), which is exactly what you need.
See demo
In case the are always 12 asterisks (that can be matched with \*{12}) in the <option> node value, you'd can use:
<option[^<]*value="(\d+)"[^<]*>\*{12}\d+</option>
See another demo.

How to properly use xpath & regexp extractor in jmeter?

I have the following text in the HTML response:
<input type="hidden" name="test" value="testValue">
I need to extract the value from the above input tag.
I've tried both regexp and xpath extractor, but neither is working for me:
regexp pattern
input\s*type="hidden"\s*name="test"\s*value="(.+)"\s*>
xpath query
//input[#name="test"]/#value
The above xpath gives an error at the Xpath Assertion Listener .. "No node matched".
I tried a lot and concluded that the xpath works only if I use it as //input[#name].
At the moment I'm trying to add an actual name it gives the error .. "No node matched".
Could anyone please suggest me how to resolve the above issue?
Please take a look at my previous answer :
https://stackoverflow.com/a/11452267/169277
The relevant part for you would be step 3:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Element;
String html = prev.getResponseDataAsString(); // get response from your sampler
Document doc = Jsoup.parse(html);
Element inputElement = doc.select("input[name=test]").first();
String inputValue = inputElement.attr("value");
vars.put("inputTextValue", inputValue);
Update
So you don't get tangled with the code I've created jMeter post processor called Html Extractor here is the github url :
https://github.com/c0mrade/Html-Extractor
Since you are using XPath Extractor to parse HTML (not XML) response ensure that Use Tidy (tolerant parser) option is CHECKED (in XPath Extractor's control panel).
Your xpath query looks fine, check the option mentioned above and try again.