简体   繁体   中英

In JMeter, how can I create multiple blocks of payload while selecting different values for each elements from CSV file?

In the Sample below I need to send 1 to 10 blocks of <ACCOUNT> payload randomly (the sample shows 4 blocks of <ACCOUNT> payload) , while the elements <ACCOUNT_TYPE_CODE> , <OPEN_DATE> , and <MEMBER_ID> need to select values either sequentially or randomly from CSV file. Even the same thread needs to pick different values for each block of <ACCOUNT> payload. How can I do that in JMeter? Thanks.

<IN xmlns:ns3="http://schema.example.com/queryparam" xmlns:ns2="http://schema.example.com/header" xmlns="http://schema.example.com/Account/CreateAC">
      <ns2:HEADER>
            <ns2:TRANID>hgjhkjhjkhjg</ns2:TRANID>
            <ns2:TIMESTAMP>2019-10-06T15:32:470Z</ns2:TIMESTAMP>
      </ns2:HEADER>
      <ns3:QUERY>
            <ns3:PARAM>
                  <ns3:ITEM>OPERATOR</ns3:ITEM>
                  <ns3:VALUE xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">CREATEAC</ns3:VALUE>
            </ns3:PARAM>
      </ns3:QUERY>

    <ACCOUNT_SPECIFIC>
            <ACCOUNT>
                <ACCOUNT_TYPE_CODE>CD</ACCOUNT_TYPE_CODE>
                <OPEN_DATE>2019-10-06</OPEN_DATE>
                <MEMBER_ID>68768789799<MEMBER_ID>
            </ACCOUNT>
            <ACCOUNT>
                <ACCOUNT_TYPE_CODE>Checking</ACCOUNT_TYPE_CODE>
                <OPEN_DATE>2019-10-05</OPEN_DATE>
                <MEMBER_ID>45667568797<MEMBER_ID>
            </ACCOUNT>            
            <ACCOUNT>
                <ACCOUNT_TYPE_CODE>Saving</ACCOUNT_TYPE_CODE>
                <OPEN_DATE>2019-10-04</OPEN_DATE>
                <MEMBER_ID>24535456677<MEMBER_ID>
            </ACCOUNT>
            <ACCOUNT>
                <ACCOUNT_TYPE_CODE>Money Market</ACCOUNT_TYPE_CODE>
                <OPEN_DATE>2019-10-03</OPEN_DATE>
                <MEMBER_ID>898977867554<MEMBER_ID>
            </ACCOUNT>          
    </ACCOUNT_SPECIFIC>
</IN>

If you have XML file which looks like:

CD,2019-10-06,68768789799
Checking,2019-10-05,45667568797
Saving,2019-10-04,24535456677
Money Market,2019-10-03,898977867554
  1. Add JSR223 PreProcessor as a child of theHTTP Request which request body you want to parameterize
  2. Put the following code into "Script" area:

     import groovy.xml.MarkupBuilder import org.apache.commons.lang3.RandomUtils def writer = new StringWriter() def xml = new MarkupBuilder(writer) def csvFileLines = new File('test.csv').readLines() def lineCounter = 0 xml.IN("xmlns:ns3": "http://schema.example.com/queryparam", "xmlns:ns2": "http://schema.example.com/header", "xmlns": "http://schema.example.com/Account/CreateAC") { "ns2:HEADER"() { ns2: TRANID("hgjhkjhjkhjg") ns2: TIMESTAMP("2019-10-06T15:32:470Z") } "ns3:QUERY"() { "ns3:PARAM"() { "ns3:ITEM"("OPERATOR") "ns3:VALUE "("CREATEAC") } } xml.ACCOUNT_SPECIFIC() { 1.upto(RandomUtils.nextInt(1, 11), { def line = csvFileLines.get(lineCounter) def entries = line.split(",") ACCOUNT() { ACCOUNT_TYPE_CODE(entries[0].trim()) OPEN_DATE(entries[1].trim()) MEMBER_ID(entries[2].trim()) } lineCounter++ }) } } sampler.addNonEncodedArgument('', writer.toString(), '') sampler.setPostBodyRaw(true)
  3. That's it, the above code will generate from 1 to 10 ACCOUNT blocks and set the generated data as the HTTP Request sampler body data. Overall test plan should look like:

    在此处输入图像描述

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM