14

I use JMeter to do performance test of web server. My test case is as following:

step1: send file update request to server.
step2: server will return some files URL as html response
step3: client need to create new request with the URL returned in step2,thus need to parse 
the response of step2.

I am new to JMeter, and do not know how to implement it. I basically learned JMeter about the pre-processor and post-processor, but still no clue about how to do.

Simon Wang
  • 2,235
  • 7
  • 36
  • 48

2 Answers2

27

Ok let's start before the first step :

Right click -> Add -> Threads (Users) -> Thread Group

  • Now the actual first step (If you use REST) :

Add -> Sampler -> Http Request

You have at the bottom part Send Files With the Request. You can add file attachment if that is what you asked.

  • Extracting response from the server :

Let's assume your response is this :

<Response>
  <name>StackOverflow.com</name>
  <url>http://stackoverflow.com/questions/11186423/how-to-parse-response-of-sample1-to-create-new-sample-in-jmeter</url>
</Response>

Here is what you do :

Right click on The http request you previously added (in step 1) -> Post Processors -> Xpath Extractor

Reference Name is the name of the variable in which you want to store the value. Let's name it url. And Xpath query is Response/url or //Response/url if you get more response tags. If you want the first one //Response[1]/url and so on..

  • Repeat step 1 (copy/paste sampler and remove the Xpath Extractor you don't need it anymore), and change the Server Name or IP to ${url} which is the value previously returned.

And Voila there you go. Let me know if you got more specific questions. Jmeter is fun.

Per Grace comment :

Wants to extract https://192.168.100.46/updserver/download?action=signature_download&amp;token=&#xd;

Out of response data :

<responseData class="java.lang.String">&lt;html&gt;&#xd;
&lt;body&gt;&#xd;
ERROR=0&#xd;
MSG=N/A&#xd;
FILELIST=1555;1340778737370;1526545487;&#xd;
VERSION=1.002&#xd;
URL=https://192.168.100.46/updserver/download?action=signature_download&amp;token=&#xd;
INTERVAL=0&#xd;
&lt;/body&gt;&#xd;
&lt;/html&gt;&#xd;
</responseData>

This should be pretty simple. Add a post processor -> Regular Expression Extractor and put the following :

Reference Name : url 
Regular Expression : (http[\S]+)
Template : $1$
Match No. (0 for Random): 1

So now you have url variable that you can use further in your test as ${url}. Let me know if that works for you. I tested with dummy sampler and it works for me.

ant
  • 22,634
  • 36
  • 132
  • 182
  • @Grace can you paste that in the pastebin.com ? – ant Jun 27 '12 at 08:07
  • What would be the right url value if you were extracting it by yourself. That is the value we want from the extractor, and I just can't see it right now – ant Jun 27 '12 at 08:57
  • @ ant, here is the sample html response: [link]http://pastebin.com/Hbf6btx0. There is one part as "URL=https://192.168.100.46/updserver/download?action=signature_download&token= ", i just need to extract the URL part and send a second request. Thanks~ – Simon Wang Jun 28 '12 at 02:03
  • @ ant, thanks, it worked now. Still one question need your help:i also need to extract "FILELIST" part, and combine it to the URL as "token" parameter, is it achievable with JMeter? Also the second request need to download the file from Java servlet, is it possible for JMeter to do that? – Simon Wang Jun 29 '12 at 08:44
  • @Grace please open another question for that. And put the sample input(things you have) and sample output(things you want) in the pastebin.com – ant Jun 29 '12 at 10:33
  • How can we do if the response its in json ? – Guillermo Nahuel Varelli Jun 02 '16 at 14:32
4

This is how I extract some value from url and pass it further as variable so next requests will contain it.

Here are some nice screenshot and wider description about making test in JMeter http://jmeter.apache.org/usermanual/build-web-test-plan.html

Add the Thread Group and HTTP Requests

When this HTTP Requests response with some data (in this example in URL) you want to extract it and us it after

So lets go:

  1. Go to your first HTTP Request after which one you receive response with variable:

    Add -> Post Processor -> Regular Expression Extractor

    In this window set:

    Response Field To Check: URL

    Reference Name: MY-CUSTOM-VARIABLE-NAME

    define name of variable whatever you like

    Regular Expression: permanent.part.of.url.com/([a-zA-Z0-9]*)

    so expression ([a-zA-Z0-9]*) is responsible for getting all occurrences of alphabetic and numeric chars after meeting permanent url at start

    Template: $1$

    only one expression is extracted in our case and it need to be read

    Match No. (0 for Random): 1

    in this case there is only one match, but if more occur you can choose which one use

  2. Now put extracted value into next HTTP Request Path: some.other.url.com/${MY-CUSTOM-VARIABLE-NAME}

    remember that you read JMeter variables with this pattern ${}, so use ${MY-CUSTOM-VARIABLE-NAME} whenever you need this value

Run your test and check what did you get in url of your request with MY-CUSTOM-VARIABLE-NAME Experiment with regexp to get desired output.

Here is blogpost about this stuff: http://kenning.co.nz/development/extracting-variables-using-regular-expressions-in-jmeter/

And always useful JMeter documentation: http://jmeter.apache.org/usermanual/component_reference.html#Regular_Expression_Extractor

pbaranski
  • 22,778
  • 19
  • 100
  • 117