Jmeter parameter extraction - regex

I am getting a result in my jmeter test that I don't understand:
I am trying to extract the "totalRunning" value from this Json response:
{"notifications":[],"taskNotificationInfo":{"totalRunning":0,"totalCompleted":0,"totalCompletedWithErrors":0,"totalFailed":0,"totalPending":0,"requestTime":1458628767436,"hasRecords":false}}
My regex is configured as following:
Reference Name: TotalRunning
Regular Expression: "totalRunning":"(.+?)"
Template: $1$
Match: 1
Default Value: 1
screen shot:
I keep getting the default value instead of "0" in this case.
Am I extracting it from the wrong place?
Any help would be appreciated.

There is no problem with your regex totalRunning":(.+?),"totalCompleted"
only you need to select radio button Body instead of Body As a Document
refer snapshot:-

Change your regular expression as below:
Regular Expression: "totalRunning":(\d+)
In the question "totalRunning":"(.+?)" is being used as regular expression. Since values of totalRunning is not surrounded by quotes. So none was being matched and default value is being picked.
Below regex can also be used:
"totalRunning":(.+?),

Change your "Field to check" to "Body" as "Body as a document" is for binary files like Word, Excel, PDF, etc. see How to Extract Data From Files With JMeter article for more details. JSON is a usual text so it should be treated as "normal" response.
Also there is a special Post Processor - JSON Path Extractor available via JMeter Plugins project. In case of complex JSON, multiple or conditional matches, etc. it might be better and easier to use it.
The relevant JSONPath expression will be: $..totalRunning[0]

Related

What is the regular expression in Jmeter for a dynamic string?

This is getting generated in a request output in Jmeter and I need to capture the dynamic value.
<update id="javax.faces.ViewState"><![CDATA[-8480553014738537212:-8925834053543623028]]></update>
the - (hyphen) symbol coming in the output is also dynamic.
I have tried handling this using
<update id="javax.faces.ViewState"><![CDATA[(.+?)]]></update>
But this is not helping. Please suggest.
The correct way to grab the data is by using the XPath Extractor with the following XPath:
//update[#id='javax.faces.ViewState']/text()
It gets the update tags that have id attribute with the javax.faces.ViewState value and extracts the text from these nodes.
Your regex is not correct because the [ (and literal dots) must be escaped in the regular expressions, and can be fixed as <update\s+id="javax\.faces\.ViewState"><!\[CDATA\[([^\]<]+)]]></update>. See the regex demo.

How to extract FirstName and LastName from html tags with regex?

I have response body which contains
"<h3 class="panel-title">Welcome
First Last </h3>"
I want to fetch 'First Last' as a output
The regular expression I have tried are
"Welcome(\s*([A-Za-z]+))(\s*([A-Za-z]+))"
"Welcome \s*([A-Za-z]+)\s*([A-Za-z]+)"
But not able to get the result. If I remove the newline and take it as
"<h3 class="panel-title">Welcome First Last </h3>" it is detecting in online regex maker.
I suspect your problem is the carriage return between "Welcome" and the user name. If you use the "single-line mode" flag (?s) in your regex, it will ignore newlines. Try these:
(?s)Welcome(\s*([A-Za-z]+))(\s*([A-Za-z]+))
(?s)Welcome \s*([A-Za-z]+)\s*([A-Za-z]+)
(this works in jMeter and any other java or php based regex, but not in javascript. In the comments on the question you say you're using javascript and also jMeter - if it is a jMeter question, then this will help. if javaScript, try one of the other answers)
Well, usually I don't recommend regex for this kind of work. DOM manipulation plays at its best.
but you can use following regex to yank text:
/(?:<h3.*?>)([^<]+)(?:<\/h3>)/i
See demo at https://regex101.com/r/wA2sZ9/1
This will extract First and Last names including extra spacing. I'm sure you can easily deal with spaces.
In jmeter reg exp extractor you can use:
<h3 class="panel-title">Welcome(.*?)</h3>
Then take value using $1$.
In the data you shown welcome is followed by enter.If actually its part of response then you have to use \n.
<h3 class="panel-title">Welcome\n(.*?)</h3>
Otherwise above one is enough.
First verify this in jmeter using regular expression tester of response body.
Welcome([\s\S]+?)<
Try this, it will definitely work.
Regular expressions are greedy by default, try this
Welcome\s*([A-Za-z]+)\s*([A-Za-z]+)
Groups 1 and 2 contain your data
Check it here

Regex HTTP Response Body Message

I use a jmeter for REST testing.
I have made a HTTP Request, and this is the response data:
{"id":11,"name":"value","password":null,"status":"ACTIVE","lastIp":"0.0.0.0","lastLogin":null,"addedDate":1429090984000}
I need just the ID (which is 11) in
{"id":11,....
I use the REGEX below :
([0-9].+?)
It works perfectly but it will be a problem if my ID more than 2 digits. I need to change the REGEX to :
([0-9][0-9].+?)
Is there any dynamic REGEX for my problem. Thank you for your attention.
Regards,
Stefio
If you want any integer between {"id": and , use the following Regular Expression:
{"id":(\d+),
However the smarter way of dealing with JSON data could be JSON Path Extractor (available via JMeter Plugins), going forward this option can be much easier to use against complex JSON.
See Using the XPath Extractor in JMeter guide (scroll down to "Parsing JSON") to learn more on syntax and use cases.
I suggest using the following regular expression:
"id":([^,]*),
This will first find "id": and then look for anything that is not a comma until it finds a comma. Note the character grouping is only around the value of the ID.
This will work for ANY length ID.
Edit:
The same concept works for almost any JSON data, for example where the value is quoted:
"key":"([^"]*)"
That regular expression will extract the value from given key, as long as value is quoted and does not contain quotes. It first finds "key": and then matches anything that is not a quote until the next quote.
You can use the quantifier like this:
([0-9]{2,}.+?)
It will catch 2 or more digits, and then any symbol, 1 or more times. If you want to allow no other characters after the digits, use * instead of +:
([0-9]{2,}.*?)
Regex demo

Searching a number in a specific string with regexp in jmeter

I want to find a specific number from a HTML response.
For example, I want to extract 3 from publicationID3publicationID.
Does someone know a solution with regexp?
Add Regular Expression Extractor Post Processor as a child of the request, which returns to you this string.
Configure it as follows:
Reference Name: publicationID (you can use any variable name here)
Regular Expression: publicationID(\d+)publicationID
Template: $1$
other fields can be left blank.
You can later refer publication ID as ${publicationID} or ${__V(publicationID)}
You can see what matches does your Regular Expression return using View Results Tree Listener (select RegExp Tester from dropdown). Another option is Debug Sampler again with combination with View Results Tree.
you can use \d to match a number using regex.

Regular Expression failing in Response Assertion and Regular Expression Extractor Jmeter

The response data in my test has this line:
<head><title>
My Title
</title><meta charset
I checked this regex in the inbuilt regex tester in Jmeter and it found the title.
(?m)(?is)<title>\n\tMy Title\n</title>
However, when I use it as a response assertion, the assertion always fails.
I have tried all settings in "Apply to" section. "Text Response" is selected for "Response Field to Test". I selected "Contains" for "Pattern Matching Rules".
I have a very similar issue with a regular expression extractor as well - the selected expression passes in the tester, but fails with regular expression extractor.
I believe it may have something to do with the multi-line nature of the response.
Any pointers?
try use:
(?<=<title>\s*)(\S.+\S)(?=\s*</title>) for find any title
(?<=<title>\s*)(My Title)(?=\s*</title>) for find 'My title'
Try the following:
Regular Expression: <title>(.+?)</title>
Template: $1$
Match: 1
Try to use xpath instead.
Use expression like //title/text() along with XPath Extractor - to extract title value, - and expression like //title[text()='My Title'] along with XPath Assertion.
In both the cases you have to ensure that that Use Tidy (tolerant parser) option is CHECKED - since you are parsing HTML (not XML!..) response.