JMeter - Error when using __FileToString to set GraphQL request body - graphql

(this is a dummy example with SWAPI)
I'm trying to set my body based on what is in a file and it fails saying the JSON is invalid. But when I set the request body manually the same content the request works.
In the results tree view I can see that the only difference is that the manual request is "encoded", it has /n instead of visual newlines.
Here's the request that fails (body coming from file):
Here's the request that works (body manually set):
does anyone know a way to force this "encoding" when retrieving the query string from a file?

It looks like a limitation or bug of the GraphQL HTTP Request sampler, you might want to raise it via JMeter Bugzilla
In the meantime you can consider manually changing line breaks to \n using __strReplace() function, something like:
${__strReplace(${__FileToString(${fileName},,)},\r\n,\\n,)}
should do the trick for you.
You can install __strReplace() funciton as a part of Custom JMeter Functions bundle using JMeter Plugins Manager
More information: How to Use the Custom JMeter Functions Plugin

I've opened a bug for Apache and they acknowledged this behavior but it doesn't seem like something they'll fix soon.
I came up with a solutions that is close to what #Dmitri T suggested but without using any Plugins
I've created a JSR223 PreProcessor (Language: Groovy) that will replace the special characters that might break the request's JSON. So far the things that I've seen in GraphQL that might break it are new lines and double quotes.
def content = new File(vars.get('fileName')).text;
vars.put('queryContent', content.replaceAll(/(\r\n|\r|\n)/, /\\n/).replaceAll(/"/, /\\"/))

Related

Bad Encoding or Strange Characters in Jmeter HTTP Request POST Option

I have been trying to record session and I got some "bizarre" characters in the POST Option in the HTTP Request component. I need some MAGIC Advice with this(picture).
Stranges Characters Appear in the POST Option in the HTTP Request component
That's it, as you can see, Jmeter in the recorded session, put me Characters that I don't understand. I seen 1000000 videos and in all of those videos the tool always show the information with no problems.
I alreade try to put utf-8 in the "Content Encoding" also change the jmeter.properties file enabling the "sampleresult.default.encoding=utf-8" option, In FACT, I try to edit the proyect modifying the ApacheJMeter_http.jar file (I found in the file that still using encoding=ISO-8859-1). I dig in almost all sites looking for a solution and I found NOTHING that can help me to fix this and I have to buils a report for my jobs next week. Please any help will be appreciated.
my Best
PG
Switching to "Parameters" tab of the HTTP Request sampler will not work given you have something in the "Body Data" tab no matter which "encoding" is being used
I don't think your goal is to test https://ocsp.digicert.com/, my expectation is that you need to exclude this domain from your testing scope
Just in case you don't need to recompile or reverse engineer anything, the "encoding" of JMeter HTTP Request samplers can be controlled either using the relevant field:
or by manipulating the relevant JMeter Properties

How to extract data from AMQP request in JMeter

I have used AMQP Publisher to publish the message in RabbitMQ then I use AMQP Consumer as listener. In the View Results Tree the messages from the queue in shown in the request tab of AMQP Consumer. My question is how to extract data from that request. I tried following the Bean Shell Post Processor but it seems it will only work on Http request. I tried to use JSR223 Post Processor and XPath extractor but it doesn't work as well. Any help?
I wanted to extract the documentId from the request. Here is the Request pattern.
I have already tried following links:
Extracting value from jmeter post request
how to extract value from request in Jmeter
How to extract the values from request input xml in jmeter
The statement that you tried something without sharing the code doesn't make sense
Posting JSON data or code as image is not the best idea
Any reason to extract data from the request? Normally people know everything about the request hence don't require to extract anything from it. Even if they do - they should normally able to store the request data into a JMeter Variable and apply the relevant Post-Processor to it.
Whatever, just in case here is the solution:
Add JSR223 PostProcessor (if you really want to do this using the Post-Processor) as a child of the request
Put the following code into "Script" area:
vars.put('foo', com.jayway.jsonpath.JsonPath.read(sampler.getArguments().getArgument(0).value,'$..documentId')[0])
That's it, you should be able to access the extracted value as ${foo} where required.
References:
JsonPath: Getting Started
Apache Groovy - Why and How You Should Use It

Getting js objects from browser console and Jmeter

I'm working on a new project, the client is a bank and I need to perform performance tests, I wanted to use jmeter as a main tool, but I found a problem, most of the pages ask for a token, so I need to catch token and set this to the next POST request in order to login and continue through the different page forms, the problem is that this token is generated by a javascript, so is not returned in any GET response body; I can just get it by console's browser, because this token is generated in a javascript object.
I've tried with postProcessor elements as regular expression extractor, JSR223 PostProcessor (I'm not a pro with) and finally with Webdriver Sampler, this last one is working, but I had to import some Java classes, did some modifications, etc; but I this is not an option, because if I perform the test with several virtual users the machine is going to run out of resources.
I was wondering if you have faced with something similar, if so, could you please make me a suggestion?, maybe perform tests with a different tool, framework...
Thanks in advance.
First of all I would recommend looking into the response more attentively as the token might be coming as a Cookie or a Header, if this is the case you should be able to extract it using HTTP Cookie Manager or Regular Expression Extractor correspondingly.
If the token is generated via JavaScript code you can re-implement the logic using Groovy language and use vars.put() function in order to store the generated value into JMeter Variables. If you cannot come up with the relevant Groovy code - update your question with the JavaScript code snippet and most probably we'll be able to help you with converting it into Groovy and exposing to next Sampler(s)

Manipulate post body of httpsampler before request

I'm testing an Eclipse-RAP application with JMeter.
In RAP the client (javascript-framework in the browser) communicates with the server over a json-based protocol.
A message looks like this:
{"head":{"requestCounter":3,"uiSessionId":"832834"},"operations":[["set","w1",{"bounds": [0,0,1680,893],"cursorLocation":[1262,-1]}]]}
As you see there is a session-ID stored in uiSessionId. I extracted this id using a "Regular Expression Extractor" and stored it in a variable namedUI_SESSION_ID`.
Then i edited the raw "Post Body" of the recorded HTTP-Request:
{"head":{"requestCounter":3,"uiSessionId":"${UI_SESSION_ID}"},"operations":[["set","w1",{"bounds": [0,0,1680,893],"cursorLocation":[1262,-1]}]]}
and it works.
But i have to alter each Post-Body of each HTTP-Request which can be a lot.
After some research i thought using a "BeanShell PreProcessor" would be the right thing, but:
var sampler = ctx.getCurrentSampler();
would give me a HTTPSampler and i did not find any method which allows me to retrieve the post-body, replace the session-id with the variable and set the altered post-body.
JavaDoc: http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/sampler/HTTPSampler.html
Do you know any way i can replace the uiSessionId with the variable without changing every request manually?
You don't have to. The uiSessionId header has been introduced in a milestone version in preparation for multiple browser-tab support in RAP, but it has been replaced by another mechanism that does not use this header parameter anymore.
As of RAP 2.1 RC1, the client now attaches a “connection id” to every POST request in a URL parameter cid. This parameter does not affect load tests, and you don't have to remove it from your recording. Since every jmeter thread gets its own HttpSession, it's not a problem that all threads have the same cid.
One reason for this change was the ability to load test RAP applications without the kind of problems that you've been struggling with.

Jmeter for pages having dynamic token

I am recording a set of pages using JMeter. Some of the pages have dynamically generated token stored in hidden field.
I retrieve this token using xpath extractor, query is
//input[#name='__RequestVerificationToken']/#value, store it in variable and use this variable for next request.
I don't know why this request is getting failed. I have checked the request value in View Results Tree. In raw tab the value is exactly the same as that of hidden field and on http tab "==" is missing at the end.
As mentioned by Andrey Bolatov, there is a visualization issue in Request HTTP Tab which has now been fixed (you can test using nightly build.
This does not explain you issue.
To debug, add a Debug Sampler to see what has been extracted.
You issue may come from the fact that you didn't encode the parameter, read:
Send Parameters With the Request
Looks like a bug in JMeter. I reported it here - https://issues.apache.org/bugzilla/show_bug.cgi?id=54055

Resources