如何使用Groovy Script克隆SOAP UI测试步骤?
Hey there! I totally get where you’re coming from—starting out with Groovy in SoapUI can feel a bit overwhelming at first, especially when you’re trying to do something specific like cloning test steps between test cases. Let me walk you through a straightforward script that should do the trick, plus tips to customize it for your needs.
The Groovy Script to Clone Test Steps
You can run this script from a Groovy Test Step within your project, or use it as a project-level script if you need to automate this across multiple test cases.
// Grab the current SoapUI project instance def project = context.testCase.testSuite.project // Replace these with your actual test suite, source, and target test case names def testSuiteName = "Your Test Suite Name" def sourceTestCaseName = "Source Test Case" def targetTestCaseName = "Target Test Case" // Fetch the source and target test cases from the project def sourceTestCase = project.testSuites[testSuiteName].testCases[sourceTestCaseName] def targetTestCase = project.testSuites[testSuiteName].testCases[targetTestCaseName] // Choose which steps to clone - adjust this line to filter as needed def stepsToClone = sourceTestCase.testSteps.values() // Clones ALL test steps by default // Loop through each selected step and clone it to the target case stepsToClone.each { step -> // Create a deep clone of the test step def clonedStep = step.clone() // Add the cloned step to the target test case targetTestCase.addTestStep(clonedStep) // Print confirmation to the SoapUI log log.info "Successfully cloned test step '${step.name}' to '${targetTestCaseName}'" } log.info "All selected test steps have been cloned!"
Customization Tips
- Filter Specific Steps: If you don’t want to clone every step, modify the
stepsToCloneline to narrow it down:- Clone only REST Request steps:
def stepsToClone = sourceTestCase.testSteps.values().findAll { it.type == "RESTRequest" } - Clone a single step by name:
def stepsToClone = [sourceTestCase.testSteps["Your Step Name"]]
- Clone only REST Request steps:
- Handle Cross-Test-Suite Cloning: If your source and target test cases are in different test suites, just update the
sourceTestCaseandtargetTestCaselines to reference their respective suites:def sourceTestCase = project.testSuites["Source Test Suite"].testCases[sourceTestCaseName] def targetTestCase = project.testSuites["Target Test Suite"].testCases[targetTestCaseName] - Update Dependencies: Keep in mind that this script clones the steps themselves, but not external dependencies like project/test case properties or data sources. If your steps rely on these, make sure they exist in the target test case first, or add extra code to copy them over.
Troubleshooting Common Issues
- NullPointerException: Double-check that your test suite and test case names are spelled correctly—they’re case-sensitive!
- Step Cloning Failures: Some step types (like Groovy scripts with hardcoded references) might need adjustments after cloning. You can add logic to update properties or references right after the
clone()line if needed.
Since you’re still building up your Groovy skills in SoapUI, start small—test cloning one step first, then expand to more complex scenarios. Don’t hesitate to tweak the script to match your exact workflow!
内容的提问来源于stack exchange,提问作者Kristiyan Damyanov

