ulysses
ulysses

Reputation: 32

JMeter Dynamic Request

I need to test a web service with header-item lines with reading values from csv.

<urn:Requisition_BudgetReqExportHeaderDetails_Item>
        <!--Zero or more repetitions:-->
        <urn:item>
           <urn:CompanyCode>
              <urn:UniqueName>?</urn:UniqueName>
           </urn:CompanyCode>
           <urn:ERPRequisitionID>?</urn:ERPRequisitionID>
           <urn:HoldTillDate>?</urn:HoldTillDate>
           <urn:IsServiceRequisition>?</urn:IsServiceRequisition>
           <urn:Name>?</urn:Name>
        </urn:item>
</urn:Requisition_BudgetReqExportHeaderDetails_Item>

I can read values from CSV file but this web service is complex and items might be 1 or more than 2.

How can I handle this web service request?

Upvotes: 0

Views: 1662

Answers (1)

Dmitri T
Dmitri T

Reputation: 168002

You can use JSR223 PreProcessor like:

  1. Add JSR223 PreProcessor as a child of your request
  2. Put the code to generate the XML payload into "Script" area, an example one would look like:

    def writer = new StringWriter()
    def xml = new groovy.xml.MarkupBuilder(writer) 
    
    xml.records() { 
        car(name:'HSV Maloo', make:'Holden', year:2006) {
            country('Australia')
            record(type:'speed', 'Production Pickup Truck with speed of 271kph')
        }
        car(name:'Royale', make:'Bugatti', year:1931) {
            country('France')
            record(type:'price', 'Most Valuable Car at $15 million')
        }
    }
    
    sampler.addNonEncodedArgument("", writer.toString(), "")
    

    Amend it to match your requirement

  3. When you run your test the JSR223 PreProcessor will generate the request body and set it in the HTTP Request sampler

References:

  • sampler - a shorthand to HTTPSamplerProxy class, see the JavaDoc for all available methods and fields
  • Groovy - Creating XML - learn how to create XML data using Groovy language
  • Groovy is the New Black - an introduction to Groovy scripting in JMeter

Upvotes: 1

Related Questions