JMeter: Regex Extractor not Pulling Token - regex

I need to extract a CSRF token from a webpage, then log it via BeanShell. The latter part is working thanks to the help I received in this thread, but now I need to figure out how to get ${token} to populate with the right data.
Note: I know the Regular Expression Extractor is not the preferred method, but I have to stay within the parameter of the exercise, in this case.
First, I have a HTTP Request set to perform a GET against www.blazedemo.com/register.
Second, I checked the response data shown in the response tree to find the CSRF token:
<!-- CSRF Token -->
<meta name="csrf-token" content="4ZCKKqQgwJH5lT5dQSeAwgeyOr7plAe7IOVRGmQm">
I have a Regex Extractor setup to grab it:
In case it fails to do so, I have default set as "NOT_FOUND".
Finally, I have a post processor logging whatever value is given to ${token}.
I find the following in my log:
2017-10-31 15:12:31,975 INFO o.a.j.u.BeanShellTestElement: The token
is: NOT_FOUND

Remember that it is not recommended to use regular expressions for parsing HTML, I would recommend going for CSS/JQuery Extractor instead.
Add CSS/JQuery Extractor as a child of the request which has this CSRF token
Configure it as follows:
Reference Name: anything meaningful, i.e. token
CSS/JQuery Expression: meta[name=csrf-token]
Attribute: content
Demo:
More information: How to Use the CSS/JQuery Extractor in JMeter
If you still want to go for Regular Expressions - change "Field to check" to Body, however I wouldn't recommend this as when it comes to parsing HTML responses regular expressions are headache to develop and/or support and very sensitive to any markup change, i.e. if order of attributes changes or an attribute goes to a new line it will ruin your test.

You choose in checkbox Response Headers which means it searches expression inside Request's headers.
In your case you search for HTML tag meta, you need to choose Body.

Related

I am not able to capture SRN value in jmeter using regular exp extractor

enter image description here
Even though the value is present in the response, debug sampler is showing not found.
I have already tried with cookie manager, authorization manager, header manager, deflate, bean shell pre processor.
Also the response assertion for that request is failing.enter image description here
You could use the XPath Extractor instead, will be easier, here is the XPath to use
//*[#name='SRN']/#value
Please make sure, you check the options, Use Tidy and Quiet in the XPath Extractor

Replacing part of ${url}'s from a sitemap in Jmeter

I have a jmeter test plan that goes to a site's sitemap.xml page, retrieves each url on that page with an XPath Extractor, then passes ${url} to a HTTP Request sampler within a ForEach Controller to send the results for each page to a file. This works great, except I just realized that the links on this sitemap.xml page are hardcoded. This is a problem when i want to test https://staging-website.com, but all of the links on sitemap.xml are all www.website.com pages. It seems like there must be a way to replace 'www.website.com' in each ${url} with 'staging-website.com' with regex or something, but I haven't been able to figure out how. Any suggestions would be greatly appreciated.
Add a BeanShell pre-processor to manipulate the url.
String sUrl = vars.get("url");
String sNewUrl = sUrl.replace("www.website.com", "https://staging-website.com");
log.info("sNewUrl:" + sNewUrl);
vars.put("url", sNewUrl);
You can also try to correlate the sitemap.xml with the regular expression extractor positioned till www.website.com so that you extract only the URL portion of the data instead of the full host name. Shouldn't you be having it already since the HTTPSampler only allows you to enter the URI segment and not the host name?
You can use __strReplace() function available via JMeter Plugins project like:
${__strReplace(${url},${url},staging-website.com,)}
Demo:
The easiest way to install JMeter Custom Functions (as well as any other plugins) is using JMeter Plugins Manager
I was able to replace the host within the string by putting
${__javaScript('${url}'.replace('www.website'\,'staging.website'))}
in the path input of the second http request sampler. The answers provided by Selva and Dimitri were more elegant, so if I have time in the future to come back to this I will give them another try. I really appreciate the help!

Cannot extract variable with RegEx from a Mail Reader in jMeter

I have the following setup:
Thread
Mail Reader Sampler
Regular Expression Extractor
Viewer
HTTP Sampler
Viewer
What I'm trying to do is retrieve an email, extract a value from it and then pass it as a variable in the next request.
The problem is that the retrieved email comes in the following format:
Mail Reader Sampler
Message 1
Part 0
Part 1
The info that I'm aiming for is located in "Part 1". And this is causing my problem...jMeter can not extract it because it is a sub-sub-sample.
If the desired variable was located in the "Message 1" part, then jMeter is extracting the value without any problems and is then passing it to the next request.
My RegEx setting is "Apply to main sample and sub-samples", so theoretically, this shouldn't be a problem..but apparently is.
How can I get jMeter to extract the wanted value when the retrieved email comes in multiple sub-parts?
Unfortunately Regular Expression Extractor processes only direct sub-samples so in case of multipart email messages you will be able to extract only This is a multipart message in MIME format. line which is not very helpful.
I would recommend adding a Beanshell PostProcessor as a child of the Mail Reader Sampler but before the Regular Expression Extractor and use the following code:
import org.apache.jmeter.samplers.SampleResult;
StringBuilder response = new StringBuilder();
for (SampleResult message : prev.getSubResults()) {
response.append(message.getResponseDataAsString());
for (SampleResult part : message.getSubResults()) {
response.append(part.getResponseDataAsString());
}
}
prev.setResponseData(response.toString(), "UTF-8");
The above code will traverse down all the messages and their parts, extract response data and substitute Mail Reader Sampler's default response data X messages found with the actual contents of these message(s) so you will be able to use the Regular Expression Extractor for correlation.
See How to Use BeanShell: JMeter's Favorite Built-in Component article for comprehensive information on using scripting in JMeter tests.

easiest way to parse html from soapui http request

I created a http request step in soapui which is an html page from which I need to extract one single value from
<span class="result">12345<span>
I'm thinking about using groovy,is it the best way ? If yes I'm beginner in both soapui and groovy, any snippet code to get started (how to get html Content from http request step, how to parse in groovy) thanks.
If you feel more comfortable with Groovy, then go for it!
SoapUI internally represents almost everything as XML. Therefore the easiest way to manipulate things in SoapUI is using XPath. In your case, you could probably use a Property Transfer step to extract //span[#class="result"].
Siking answer worked setting the source 'property' to ResponseAsXml along with XPath expresions, as for common HTML XPath expressions there's a resource online at
https://devhints.io/xpath

Extract Path from Href link using Jmeter

I am trying to develop a simple test script using Jmeter that can do a bing search and randomly select a link from search results and navigate into the selected link. I am able to capture and randomize the link selection using Regex Extractor & Random variable functions but what I am not sure is, how to go about extracting the path from randomly captured href link.i.e., if the captured link is "http://en.wikipedia.org/wiki/.NET_Framework", I want to extract "/wiki/.NET_Framework" from the link and substitute it in the "Path" textspace of the subsequent HTTP requests. If I am correct, I think using Regex Extractor is not possible here as there are no unique boundaries inorder to extract the path directly from response of the page .
Let's break down http://en.wikipedia.org/wiki/.NET_Framework into components:
http - protocol
en.wikipedia.org - host
/wiki/.NET_Framework - path
Add Beanshell Pre Processor as a child of the request, where you need to substitute path and add the following code to Script area:
import java.net.URL;
URL url = new URL(vars.get("LINK"));
sampler.setProtocol(url.getProtocol());
sampler.setDomain(url.getHost());
sampler.setPath(url.getPath());
The code above assumes that Reference name for your URL is "LINK". Change it to the reference name which is specified in Regular Expression Extractor and it should work fine.
Beanshell Pre Processor is executed before request so all necessary fields will be populated.
See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting.