使用Groovy修改SoapUI请求:动态生成XML键值对及经销商统计
Hey there! Let's tackle this problem step by step—you need to dynamically generate SOAP request XML tags based on the key-value pairs in your Input.txt file to count dealers in the current country, right? Here's a practical, SoapUI-focused solution:
First, we'll read each line from your Input.txt and extract the key-value pairs. Each line represents a unique set of parameters for your SOAP request. For example, a line like key1=Fruit,value1=Carrot,Key2=Vegetable,value2=Carrot gets split into individual pairs, then each pair is split by = to separate keys and values.
SoapUI natively supports Groovy scripting, which is perfect for dynamic XML generation. Here's a ready-to-use script you can attach to your request:
// Point to your Input.txt file (update the path!) def inputFile = new File("C:/Your/File/Path/Input.txt") def soapRequest = context.request // Loop through each line in the file inputFile.eachLine { line -> def paramBuilder = new StringBuilder("<FilterParameters>") // Split line into key-value pairs def pairs = line.split(",") pairs.each { pair -> def keyValue = pair.split("=") def cleanKey = keyValue[0].trim() def cleanValue = keyValue[1].trim() // Generate XML tags (adjust tag names to match your SOAP service's schema!) paramBuilder.append("<${cleanKey}>${cleanValue}</${cleanKey}>") } paramBuilder.append("</FilterParameters>") // Replace a placeholder in your base SOAP request with the dynamic XML soapRequest = soapRequest.replace("<!--DYNAMIC_PARAMS-->", paramBuilder.toString()) } // Update the request context with the modified SOAP body context.request = soapRequest
- In your base SOAP request, add a placeholder comment where you want the dynamic parameters to inject:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:deal="http://your-dealer-service.com"> <soapenv:Header/> <soapenv:Body> <deal:GetDealerCount> <deal:Country>CurrentCountry</deal:Country> <!--DYNAMIC_PARAMS--> </deal:GetDealerCount> </soapenv:Body> </soapenv:Envelope>
- Attach the Groovy script to your request: Right-click the request > Add Step > Groovy Script, or paste it into the "Script" tab of the request editor.
- Adjust the XML tag names in the script (like
<FilterParameters>or<${cleanKey}>) to match exactly what your SOAP service expects.
If your service expects pairs wrapped in a specific structure (e.g., <Parameter><Key>...</Key><Value>...</Value></Parameter>), tweak the script's pair-processing logic:
pairs.eachWithIndex { pair, idx -> if (idx % 2 == 0) { // Grab key from even indices, value from next odd index def key = pair.split("=")[1].trim() def value = pairs[idx+1].split("=")[1].trim() paramBuilder.append("<Parameter><Key>${key}</Key><Value>${value}</Value></Parameter>") } }
This works if your Input.txt lines follow the consistent keyN=X,valueN=Y pattern from your examples.
If each line in Input.txt needs a separate SOAP call, use SoapUI's built-in tools:
- Add a File Data Source pointing to Input.txt, configured to split lines by newline.
- Link the data source to your request using property expansion (e.g.,
${DataSource#key1}). - Add a Loop test step to iterate through all lines automatically.
内容的提问来源于stack exchange,提问作者lokeshchitturi

