If someone might be interested, here is the complete code of the script.
It deletes every JDBC testStep in the current testCase that has not 'eemplate' in its name.
Then it creates JDBC steps based on a specified template with the correct queries.
The new steps receive the same name as their corresponding SQL filenames (with .sql removed)
// Define the path where all SQL queries are stored
String ResultSetsPath = "PATH_TO_DIRECTORY_CONTAINING_SQL_FILES"
String ResultSetTemplateStepName = "ResultStepTemplate"
// Try to get the template JDBC TestStep
def ResultSetTemplateStep = testRunner.testCase.getTestStepByName(ResultSetTemplateStepName)
// Make sure it's a valid JDBC TestStep
assert ResultSetTemplateStep != null
assert ResultSetTemplateStep.getClass().name.contains("JdbcRequestTestStep")
// Delete every JDBC TestStep in the current TestCase except the template steps
int TestStepCount = testRunner.testCase.getTestStepCount()
def TestStepsToDelete = []
for (int i = 0; i < TestStepCount; i++)
{
// Get the current TestStep
def curTestStep = testRunner.testCase.getTestStepAt(i)
// Only delete JDBC steps
if (!curTestStep.getClass().name.contains("JdbcRequestTestStep"))
continue
// Don't delete template JDBC steps
if (curTestStep.name == ResultSetTemplateStepName || curTestStep.name.toLowerCase().contains("template"))
continue
TestStepsToDelete.add(i)
}
int TestStepsToDeleteCount = TestStepsToDelete.size
for (int i = TestStepsToDeleteCount - 1; i >= 0; i--)
testRunner.testCase.removeTestStep(testRunner.testCase.getTestStepAt(TestStepsToDelete[i]))
//
// For every SQL file in the provided ResultStepsPath, clone the template TestStep with the filename as TestStep name
//
// Because of a bug in the Clone methods, we have to:
// - Create a temporary TestCase
// - Clone the original TestStep(s) to the new TestCase
// - Change the name of the the cloned TestStep(s)
// - Clone the renamed TestStep(s) to the original TestCase
// - Remove the temporary TestCase
def tmpCloneTestCase = testRunner.testCase.testSuite.addNewTestCase("TemporaryCloneTestCase")
new File(ResultSetsPath).eachFile
{
// Ignore files that do not end in .sql
if (it.name.endsWith(".sql"))
{
// Make sure we can read the file
assert it.canRead()
// Get the new name for the testStep
String newStepName = it.name[0..-5]
// Clone the template TestStep
def newStep = ResultSetTemplateStep.clone(tmpCloneTestCase, newStepName)
newStep.setName(newStepName)
// Update the query of the TestStep with the contents of the file
newStep.setQuery(it.text)
// Now clone the temporary testStep to the original testCase
newStep.clone(testRunner.testCase, newStepName)
// Remove the temporary testStep
tmpCloneTestCase.removeTestStep(newStep)
}
}
testRunner.testCase.testSuite.removeTestCase(tmpCloneTestCase)
// Save the project
assert testRunner.testCase.testSuite.project.save() == true