priya
priya

Reputation: 11

Extracting a value from jmeter response and exporting it to a csv file

I need to know how can I extract a value from response in jmeter and export it to a csv file.

Suppose my response is like this:

<ns3:UpdateConsumerResponse
        xmlns:ns3="http://Pandora.NextGenCRM.Integrations.UpdateConsumerResponse"
        xmlns:ns0="http://tempuri.org/" 
        xmlns:ns1="http://schemas.datacontract.org/2004/07/Pandora.Xrm.DataModel.Request"
        xmlns:ns2="http://schemas.datacontract.org/2004/07/Pandora.Xrm.DataModel.Response">
    <MasterConsumerID>
        CRM-CONID-000000519344
    </MasterConsumerID>
</ns3:UpdateConsumerResponse>

I need to extract the master consumer value and export it to an csv file.

Upvotes: 1

Views: 3075

Answers (2)

Dmitri T
Dmitri T

Reputation: 168082

  1. Add XPath Extractor as a child of the request, which returns above value
  2. Configure it as follows:

    • Check Use Namespaces box
    • Reference Name: anything meaningful, i.e. ID
    • XPath query: /ns3:UpdateConsumerResponse/MasterConsumerID/text()
  3. Add Beanshell PostProcessor after the XPath Extractor
  4. Add the following code into the PostProcessor's "Script" area:

    import org.apache.commons.io.FileUtils;
    
    String file = "path_to_your_file.csv";
    String ID = vars.get("ID");
    String newline = System.getProperty("line.separator");
    
    FileUtils.writeStringToFile(new File(file),ID + newline, true);
    

Upvotes: 2

Kaushlendra Jha
Kaushlendra Jha

Reputation: 396

First of all you have to add an regular expression extractor as a child of this request and then mention below inputs

1.Reference Name: MasterConsumer (or any variable)

2.Regular expression: abc(.*?)d (suppose your value is like abcCRM-CONID-000000519344d then provided reg ex will work, now replace abc with your left boundary and d with right boundary which you can get from your response. if still you need more help then please provide this value along with more text from both side )

3.Template: $1$

4.Match No:1

5.Default Value: null

now you have your value stored in MasterConsumer variable (apply debug sampler to verify). Just you need to write into csv file, so add beanshell post processor as a child of same request and write below code for printing data into csv file

 MasterConsumer =vars.get("MasterConsumer");
 f = new FileOutputStream("Path"-Output.csv",true);
 p=new PrintStream(f);
 this.interpreter.setOut(p);
 p.println(MasterConsumer);
 f.close();

Upvotes: 3

Related Questions