Показаны сообщения с ярлыком jMeter. Показать все сообщения
Показаны сообщения с ярлыком jMeter. Показать все сообщения

четверг, 18 января 2018 г.

How to clear tokens in jMeter


Flushing all cookies / tokens in jMeter

When launching a chain of experiments or jobs in jMeter project you might require to flush all the tokens from the previous step. To make it exactly when you need it, I suggest using simple Java script withing JSR223 processors.
Use PreProcessor if you want to remove tokens at the beginning of a job,
or PostProcessor for flushing all tokens in the end of some process.

1. Add JSR223 Pre- or PostProcessor:


2. Set language to what you like (in my case - java);
3. Provide your code into Script field. E.g. let's use CookieManager in java:
import org.apache.jmeter.protocol.http.control.CookieManager;

CookieManager cManager = sampler.getCookieManager();
    int count = cManager.getCookieCount();
    for (int index = 0; index < count; index++) {
        cManager.remove(0);
        }
Here it is. That will remove all the tokens including, for example, JSESSIONID token.

jMeter. Gettings variables and their values

This piece of advice came from one of discussions on StackOverflow. Seems to be helpful for some tasks, so putting it here.

Sometimes its extremely valuable to have a chance to pull from the jMeter project all the pre-defined variables and their values. These cases are - but not limited to - exploration existent big jMeter project, debugging issues, intermediate checks of changes made to a project and so on. 

I used to get Set of vars right through the code (variant with Java code in JSR223 PostProcessor):

1. Add "JSR223 PostProcessor" by right click wherever you need to check jMeter variables in your project:


Adding JSR223 PostProcessor

2. Set Language, in my case - to java;

3. Add following code to Script window:
import java.util.Map;
String jMeterVars;
jMeterVars = "Quantity of vars: " + vars.entrySet().size() + ".\n";
jMeterVars += "[VARIABLE NAME] ==>> [VARIABLE VALUE]\n";
for (Map.Entry entry : vars.entrySet()) {
    jMeterVars += entry.getKey() + " ==>> " + entry.getValue().toString() + "\n";
    }

    try {
        FileWriter fw = new FileWriter("D:\\jMeterVarsForStackOverflow.txt",true);
        fw.write(jMeterVars);
        fw.close();
        } catch(IOException ioe) {
            System.err.println("IOException: " + ioe.getMessage());
            }
5. Check that everything in the JSR223 PostProcessor looks like that:

JSR223 PostProcessor ready to go

6. Start your project in jMeter. The code above will create jMeterVarsForStackOverflow.txt file at root of D: and put all variables there:

Sample of file with jMeter variables got by the code above