I am using Webdriver sampler with JavaScript. Is it possible to use external code in a sampler? I have many samplers which use almost same code. I want to write this code in an external file and call the required methods (with appropriate arguments) in multiple samplers. If it is not possible then is there a way to call a sampler from within another sampler?
For example if you have foo.js file in JMeter's "bin" folder with the following function defined
function hello() {
WDS.log.info('Hello there')
}
You can just call load directive in your WebDriver Sampler code in order to import the aforementioned foo.js file lile:
load('foo.js')
Full code:
load('foo.js') // import external file
WDS.sampleResult.sampleStart()
hello() // call external function
WDS.browser.get('http://jmeter-plugins.org')
WDS.sampleResult.sampleEnd()
Demo:
More information: The WebDriver Sampler: Your Top 10 Questions Answered
Related
I plan to add digest header to my "HTTP Header Manager" in JMeter.
I plan to use __digest function referenced in https://jmeter.apache.org/usermanual/functions.html.
An example is:
${__digest(MD5,${message-body},,,)}
How should I reference the messege body according to RFC3230?
You can use __groovy() function instead of the ${message-body} variable and retrieve the request body dynamically in the runtime.
The Groovy syntax to get the current request body:
${__groovy(ctx.getCurrentSampler().getArguments().getArgument(0).getValue(),)}
Put together with the __digest() function:
${__digest(MD5,${__groovy(ctx.getCurrentSampler().getArguments().getArgument(0).getValue(),)},,,)}
Demo:
In the above example:
ctx stands for JMeterContext
ctx.getCurrentSampler() resolves into HTTPSamplerProxy
See the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on various JMeter API shortcuts available for JSR223 Test Elements
I have written a function in file BeanShellFunction.bshrc called XYZ which I can use it in several of my BeanShell (pre and post) samplers throughout threads and .jmx files and all over the map.
Now I like to do the same in JSR223 (pre and post) and clearly I cant call that XYZ function because thats for pre and post Bean files (or Java). How do I do the same and write a fucntion called ABC for my pre/post JSR223 functions which I can use in any thread and any .jmx file?
If you want to use some custom Groovy code in __groovy() function you need to "tell" JMeter your Groovy file location via a property, i.e. add the next line to user.properties file:
groovy.utilities=/path/to/your/file.groovy
JMeter restart will be required to pick the property up
For other JSR223 friends you can add Script.evaluate() function to the beginning of your JSR223 Test Elements like:
evaluate(new File('/path/to/your/file.groovy'))
After that you will be able to use the functions from your file.
Also be aware that if your class is in compiled form and under JMeter Classpath you don't even need to take any extra actions.
More information: Apache Groovy - Why and How You Should Use It
I have an Authenticate method that calls the Service and gets the response, i use those response object(token in response) and insert them into regular exp variables
(BeanShell Post processing of authenticate method code below)
import org.apache.jmeter.protocol.http.control.Header;
sampler.getHeaderManager().add(new Header("Token",vars.get("Token")));
But i see i get an error below. Not sure where exactly is the issue i guess it is not able to identify the sampler class or the getHeaderManager() method
Note: My authentication call succeeds and get a response
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.control.Header; sampler.getHeaderManager . . . '' : Attempt to resolve method: getHeaderManager() on undefined variable or class name: sampler
Anyhelp is highly appreciated, Thanks
Unlike Beanshell PreProcessor, Beanshell PostProcessor doesn't have an access to previous sampler object, so sampler is indeed undefined. Also I think your intention is to add that header to the following samplers, not to the current one (which already finished running). So instead of having post-processor on the sampler which retrieves the token, you need to have a Beanshell PreProcessor on each sampler afterwards with the same code.
You need to use ctx.getCurrentSampler() instead of sampler in case of Beanshell PostProcessor
See:
JMeterContext class JavaDoc
How to Use BeanShell: JMeter's Favorite Built-in Component guide
Also keep in mind that PostProcessor is executed after the sampler so the parent sampler will not be affected by header change.
I can successfully create a summary report in jmeter but in the label column i need the full get request along with parameters passed.I am not getting the parameters passed in the url.
You can get it automatically populated with the help of Beanshell scripting.
Example:
Add Beanshell PostProcessor as a child of HTTP Request
Put the following code into the PostProcessor's "Script" area:
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.testelement.property.JMeterProperty;
HTTPSamplerProxy sampler = (HTTPSamplerProxy) ctx.getCurrentSampler();
StringBuilder builder = new StringBuilder();
builder.append(sampler.getUrl());
Arguments args = sampler.getArguments();
PropertyIterator iter = args.iterator();
while (iter.hasNext()) {
builder.append (iter.next().getStringValue());
}
prev.setSampleLabel(builder.toString());
Run your test.
The code fetches URL and all parameters along with their values and updates parent sampler name with these values:
As you can see HTTP Request became http://example.com/foo=bar
You can place the PostProcessor at the same level as HTTP Request samplers to avoid copy-pasting it multiple times or use i.e. Beanshell Listener or Beanshell Assertion instead.
See How to use BeanShell: JMeter's favorite built-in component guide for comprehensive information on using scripting in JMeter and to learn what all these things like ctx and prev mean.
How can i Save parameter or any word from the response of the page to external file (txt ot csv)ang get this parameter from the file to enter another page ?
In fact I want to be able to change the parameter dynamically via file.
Thanks.
To save the whole response thing into a file:
Add a Beanshell PostProcessor as a child of the sampler, whose data you need to save.
Add the following code to the PostProcessor's "Script" area
import org.apache.commons.io.FileUtils;
File file = new File("/path/to/your/file.txt");
FileUtils.writeByteArrayToFile(file, data);
To read file contents in JMeter:
Just use __FileToString function wherever file's content is required as:
${__FileToString(/path/to/your/file.txt,,)}
For more information on Beanshell bit refer to How to use BeanShell: JMeter's favorite built-in component guide.
For high loads it will be better to replace Beanshell PostProcessor with JSR223 PostProcessor and choose "groovy" as the language (you'll need groovy-all.jar somewhere in JMeter's classpath)