how to use __eval in preprocessor of jmeter - testing

I am using a faker.jar plugin to generate random data inside json file
The JSON file is like
{
"name" = "${__BeanShell(new com.github.javafaker.Faker().name().fullName())}"
"id" = "${__BeanShell(new com.github.javafaker.Faker().IdNumber())}"
}
and in jmeter i am using a http sampler
This is working fine, but now i want to evaluate the request with response, so i want to save the request body inside a variable and pass that variable to http request and then use post process to evaluate request against response, but i am unable to use ${__eval()} function inside preprocessor.
How to fix that

Add JSR223 PreProcessor as a child of the HTTP Request sampler
Put the following code into "Script" area:
vars.put('foo', sampler.getArguments().getArgument(0).getValue())
3 That's it, the PreProcessor will fetch the body data from the HTTP Request sampler and store it into ${foo} JMeter Variable.
Be aware that according to JMeter Best Practices you should be using __groovy() function instead of __Beanshell() so consider amending your JSON file on next available opportunity.
Be also informed that JMeter provides some built-in functions allowing generation of random values i.e. __Random(), __RandomString(), __UUID(), etc. so you might even discard your 3rd-party library as JMeter built-in functions provide better performance.

Related

Jmeter 5.4.1 - Groovy script error 'No signature of method'

Newly back to using Jmeter after a 3 year break and pretty confused by this error.
I have a JSR223 Preprocessor which is used for generating AWS signatures and it keeps generating errors of the following type:
021-10-20 13:47:40,101 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, Create AWS Signature
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.protocol.java.sampler.JSR223Sampler.getMethod() is applicable for argument types: () values: []
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-jsr223-3.0.7.jar:3.0.7]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[java.scripting:?]
It's not causing the tests themselves to fail (weirdly) but it would be useful to stop these errors clogging our log files.
Help?!
It looks like you added the JSR223 PreProcessor as a child of the JSR223 Sampler which doesn't really have getMethod() function.
The function is available for HTTPSamplerBase class and derivatives.
In other words you need to put the JSR223 Preprocessor as a child of the HTTP Request sampler and it should start working as expected.
More information: How to Handle Dynamic AWS SigV4 in JMeter for API Testing
Make sure you uncheck checkbox on JSR223 Sampler >> Cache compiled script if available. It looks like script is cached and not recompiled, resulting in old code being run. Also make sure your old code is not hidden in the bottom of script editor window (scrollbar sometimes does not display correctly how much content is hidden).

Jmeter Testing shows null message for api links

How can I handle data from ":/some/api/link" while I am recording using JMeter it works and when I try it again it shows "null" message in 'View results tree'.
In the majority of cases you won't be able to record and replay you test as modern applications and web APIs normally use dynamic parameters for authentication and security purposes. So most probably your Test Plan should look as follows:
1st request
Post Processor (in case of REST API it will be JSON Extractor, in case of SOAP API it will be XPath Extractor) to extract any dynamic parameters and save them into JMeter Variables
2nd request where you should replace hard-coded recorded values with the variables defined in the previous step
The easiest way to detect the dynamic parameters is recording your test 2 times and compare the generated requests, you will need to correlate parameters which differ.
See API Testing With JMeter and the JSON Extractor for more details.

How is XHR a viable alternative to asynchronous module definition?

I'm learning about the case for asynchronous module definition (AMD) from here but am not quite clear about the below:
It is tempting to use XMLHttpRequest (XHR) to load the scripts. If XHR
is used, then we can massage the text above -- we can do a regexp to
find require() calls, make sure we load those scripts, then use eval()
or script elements that have their body text set to the text of the
script loaded via XHR.
XHR is using ajax or something to make a call to grab a resource from the database, correct? What does the eval() or script elements have to do with this? An example would be very helpful
That part of RequireJS' documentation is explaining why using XHR rather than doing what RequireJS does is problematic.
XHR is using ajax or something to make a call to grab a resource from the database, correct?
XHR is what allows you to make an Ajax call. jQuery's $.ajax for instance creates an XHR instance for you and uses it to perform the query. How the server responds depends on how the server is designed. Most of the servers I've developed won't use a database to answer a request made to a URL that corresponds to a JavaScript file. The file is just read from the file system and sent back to the client.
What does the eval() or script elements have to do with this?
Once the request is over, what you have is a string that contains JavaScript. You've fetched the code of your module but presumably you also want to execute it. eval is one way to do it but it has the disadvantages mentioned in the documentation. Another way to do it would be to create a script element whose body is the code you've fetched, and then insert this script in the DOM but this also has issues, as explained in the documentation you refer to.

Is it possible to determine the http request method (POST/GET) using a variable?

I am using a csv file as the basis for my requests. The thing is, I have some GET requests and some POST requests. Is there a way to use the same http request element for both request types where the method will be determined by the variable from the csv file?
This is really simple using Beanshell Preprocessor.
Add a Beanshell preprocessor for your existing HTTP request. Lets assume the default HTTP method is GET.
Now lets change it to POST whenever the csv variable 'method' is 'POST'
if(vars.get("method").equalsIgnoreCase("POST")){
sampler.setMethod("POST"); //this will change current sampler's http method from GET to POST.
}
The most direct solution for this would be to have two requests in the test plan, one a GET and one a POST. This does not quite satisfy your requirement to have it use the SAME request element, but it is probably the best solution.
Nest each of those inside their own IF controllers that reads a value from the CSV.
For example lets say the csv is the following:
http_method,host,path,params...
The first IF could be:
"${http_method}" == "GET"
Then the next:
"${http_method}" == "POST"
Each line from the CSV would only evaluate true to one of the statements, and then make the correct POST or GET call.
There are 2 options:
Use HTTP Raw Request available via JMeter Plugins
Write your custom logic in Java. See "How to Write a Custom AJAX Request Sampler" chapter of How to Load Test AJAX/XHR Enabled Sites With JMeter for idea on how this could be done.

JMeter - injecting variables into a HTTP Request

I'm trying to work with JMeter to test some web services. So far so good, but I was wondering if you could do the following -
I make a http POST request to create a resource, and if successful the response comes back with the location of the resource in the headers. What I would like to do is take the value of this header, and use it in a http GET request to retrieve the resource. Is this possible with JMeter?
Any help is much appreciated
Use the regular expression extractor to extract the header value to a variable by using a regex. Then use the variable like any other variable in your GET request.