JMeter. Run python script before invoking each HTTP request sampler

I am new to Jmeter. My HTTP test request request is as follows:

Path= /image/**image_id**/list/
Header =  "Key" : "Key_Value"

The key value is generated by calling the python script, which uses image_idto generate a unique key.

Before each sampler, I wanted to generate a key using a python script that will be passed as a header to the next HTTP request sampler.

I know that for this you need to use some kind of preprocessor. Can anyone help me do this using a preprocessor in jmeter.

+4
source share
3 answers

, Beanshell PreProcessor - , .

Beanshell :

import java.io.BufferedReader;
import java.io.InputStreamReader;

Runtime r = Runtime.getRuntime();
Process p = r.exec("/usr/bin/python /path/to/your/script.py");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
StringBuilder response = new StringBuilder();
while ((line = b.readLine()) != null) {
    response.append(line);

}

b.close();
vars.put("ID",response.toString());

Python script ID.

HTTP-   //${ID}//

. BeanShell: JMeter Beanshell Apache JMeter Beanshell.

Transaction Controller, PreProcessor .

+5

, :

JSR223 , jython.jar /lib, "" jython .

, Jython , .

.

+2

BSF.

Jython Library jmeter lib.

HTTP BSF, Jython , , :

import random
randImageString = ""
for i in range(16):
    randImageString = randImageString + chr(random.randint(ord('A'),ord('Z')))

vars.put("randimage", randImageString)

vars.put("randimage",randImageString"), , , jmeter.

Now, when testing, you can use ${randimage}it when you need it:

HTTP Sampler with parameters

Now, each request will be different when the value placed in randimage is changed to Python Script.

+2
source

Source: https://habr.com/ru/post/1546696/


All Articles