Forum Discussion

MartinH's avatar
14 years ago

Trying to clone assertions through Groovy

I know that SOAPUI Pro allows one to clone Assertions from one test case to another but I don't have Pro and am trying to do it through Groovy. I'm also trying to do it not a run time but at design time by using a groovy test step that's disabled and that I run manually.

My plan is this:

1) Define 2 properties: the source and destination test steps
2) loop on the list of Assertions in the Source
3) Add the Assertions to the Destination (after checking they don't already exist there)

The crux of my logic is this loop:


sourceStep = testRunner.testCase.getTestStepByName(sourceTestStepName)
destinationStep = testRunner.testCase.getTestStepByName(destinationTestStepName)

if (sourceStep != null && destinationStep != null && sourceStep instanceof Assertable && destinationStep instanceof Assertable) {

for (assert in sourceStep.getAssertionList()) {

//add to destination
destinationStep.addAssertion(assert.get????)
}
}


That destinationStep.addAssertion(assert.get????) is proving to ge difficult. According to other posts that "String" needs to be the type of Assertion (i.e. one of the values in the drop down) but that type values does not seem to be carried on the assertion object I'm pulling from the source step.

Anyone manage to do something like this or know how to assertain the type of the assertion and use that to define a new assersion?

thank you,
Martin.

1 Reply

  • Hi Martin,

    Here are the code snippets, will do the work you wanted to achieve. Put this code into a grovy teststep and execute it once after updating the values of the property(source & target). Name passed is case sensitive, so please copy the teststep name and paste into the property value.

    import com.eviware.soapui.model.testsuite.Assertable
    def project = context.testCase.testSuite.project
    def testSuite = project.getTestSuiteAt(1)
    def testCase = testSuite.getTestCaseAt(0)
    def sourceTestStep = context.expand( '${#TestCase#SourceTestStep}' ) // testCase level property "SourceTestStep"
    def targetTestStep = context.expand( '${#TestCase#TargetTestStep}' )

    def testStepsrc=testCase.getTestStepByName(sourceTestStep)
    def testStepTrgt = testCase.getTestStepByName(targetTestStep)
    def counter = testStepSrc.getAssertionList().size()
    for (count in 0..<counter)
    {
    testStepTrgt.cloneAssertion(testStepSrc.getAssertionAt(count), testStepSrc.getAssertionAt(count).getName())
    }



    Will be writing a blog on this topic/issue soon :-) Update the thread if it works for you also.

    Best Regards,
    ~~ Pradeep Bishnoi ~~
    http://learnsoapui.wordpress.com
    http://pradeepbishnoi.blogspot.com