Forum Discussion

zen_paul's avatar
zen_paul
Occasional Contributor
8 years ago

trying to set the name of a script assertion using groovy

I have some groovy code which sets the assertions on a soap test case step to run tests in our automation framework, but for some reason when I try and create a 'Script Assertion' it does not seem to be taking the name correctly. I am hoping I've made a simple mistake and someone can point me in the right direction.

 

Using SoapUI 5.2.1, - Build Date: 20151002-1138

 

I have this code running in Groovy for the Test Case.

 

This block of code runs correctly adding the assertion to the testcase (teststep) and running. 

assertion = tStep.addAssertion("Contains")
assertion.setName("hasBillingType")
assertion.setToken("billingType")

 

This block of code does not work in the same way, as though the setName is not assigning the name to the Script Assertion, instead it opens the interaction window to add the assertion name. This is not helpful as I want to run this using Maven and Jenkins, once its developed.

 

assertion = tStep.addAssertion("Script Assertion")
assertion.setName("testScriptAssertion")
assertion.setScriptText("log.info 'we are running the script assertion'\n log.inf

 

 

Any help greatly appreciated.

 

 

 

1 Reply

  • Radford's avatar
    Radford
    Super Contributor

    I think there may be some other factors at play here, I've just tried the following. A test case with two test steps, first a Groovy Script, followed be a SOAP request.

     

    With the full Groovy script test step contents as follows (based on your code):

     

    // Get the SOAP test step.
    def testStep = testRunner.getTestCase().getTestStepByName('SOAP Request')
    
    // Just create a unique name each time, allows the script to be run 
    // multiple times without clashing assertion names.
    def currentDateTime = new Date()
    def newAssertionName = currentDateTime.format("HHmmss")
    log.info('newAssertionName = ' + newAssertionName)
    
    // Add the new script assertion.
    def testAssertion = testStep.addAssertion('Script Assertion')
    testAssertion.setName(newAssertionName)
    testAssertion.setScriptText("log.info 'we are running the script assertion ${newAssertionName}'")

     

    This works exaclty as you would expect, adding a new assertion and renaming as expected. With the log output:

     

    Thu Nov 24 17:44:05 GMT 2016:INFO:newAssertionName = 174405
    Thu Nov 24 17:44:05 GMT 2016:INFO:we are running the script assertion 174405
    Thu Nov 24 17:44:05 GMT 2016:INFO:we are running the script assertion 174405

     

    (Not sure why the assertion is run twice?)

     

    I'm using the same build as you so on the face of it what you are doing seems right. Sorry it's not a solution, just wanted to say that I got your code working correctly.