You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ReadyAPI中遍历Excel数据源并传值至Groovy脚本的方法

Automate XML Generation from Excel Data in ReadyAPI with Groovy

Hey there! Let's work through this step by step since you're new to using Groovy in ReadyAPI for this kind of automation. You already have the basics down (generating XML from hardcoded values), so we just need to adjust things to loop through all your Excel rows and pull dynamic values.

1. First, Confirm ReadyAPI's Datasource Loop Settings

ReadyAPI's Excel Datasource step is built to loop through rows automatically—you just need to make sure it's configured right:

  • Open your Excel Datasource step's configuration, find the Loop section
  • Set it to "For each row in the datasource" or "All rows" (depending on your ReadyAPI version)
  • This tells ReadyAPI to run the subsequent Groovy script once for every row in your Excel file, passing that row's values each time

2. Pull Dynamic Values from the Datasource in Groovy

Replace your hardcoded values with references to the Excel data. It's super straightforward using context.expand():

  • Use the syntax context.expand('${YourDatasourceName#ParameterName}') where:
    • YourDatasourceName is the exact name of your Excel Datasource step
    • ParameterName is the column header from your Excel file (matches what you set in the datasource configuration)

Example code snippet:

// Grab values from the current Excel row
def firstName = context.expand('${Excel数据源#First Name}')
def lastName = context.expand('${Excel数据源#Last Name}')
def userId = context.expand('${Excel数据源#User ID}')

3. Generate XML with Dynamic Values (Using MarkupBuilder)

Keep using your existing Schema-based XML logic, but swap in the dynamic variables. Here's a clean example using Groovy's MarkupBuilder to build valid XML:

import groovy.xml.MarkupBuilder

// Create a writer to hold the XML output
def xmlWriter = new StringWriter()
def xmlBuilder = new MarkupBuilder(xmlWriter)

// Build XML matching your Schema, using the dynamic values from Excel
xmlBuilder.userRecord {
    userID(userId)
    fullName("${firstName} ${lastName}")
    status("active") // This could also be a datasource value if needed
}

// Log the generated XML to ReadyAPI's test log for debugging
log.info("Generated XML for row: ${xmlWriter.toString()}")

// Optional: Save each XML to a unique file (adjust the path as needed)
def outputFile = new File("C:/ReadyAPI_Output/user_${userId}.xml")
outputFile.write(xmlWriter.toString(), "UTF-8")

4. Make Sure Your Test Suite Loops Correctly

Your current test suite structure (Datasource → Groovy Script → Datasource...) needs one final check:

  • Select your test suite, go to the Test Suite Properties panel
  • Set Loop Count to 0 (this means "loop until the datasource has no more rows")
  • Alternatively, confirm the datasource's own loop setting is set to process all rows (this usually handles it, but the suite loop count is a helpful safety net)

5. Quick Troubleshooting Tips

  • If only the first row is processed: Double-check the datasource's loop setting—don't set a fixed number of loops
  • If values are missing: Verify the datasource step name and parameter names match exactly (case-sensitive!)—use log.info(context.expand('${Excel数据源#First Name}')) to test if the value pulls correctly
  • If XML doesn't match your Schema: Add a XML Schema Validator step right after your Groovy script to automatically check each generated XML against your Schema

That should get you up and running with automating XML generation for every row in your Excel file!

内容的提问来源于stack exchange,提问作者sanny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:50:07