Forum Discussion

yoga46's avatar
yoga46
New Contributor
4 years ago

Getting Assertion Renaming Box while adding Script assertion using Groovy Script. How to avoid it?

I need to add a Script Assertion using the Groovy Script. When I tried the below piece of code I am getting the assertion renaming box as displayed in the screenshot. How to avoid it?

// Get the SOAP test step.
def testStep = context.getTestCase().getTestStepByName('TestStepforScript')

// 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}'")

 

 

 

 

  • ZDGN's avatar
    ZDGN
    4 years ago

    Hi yoga46,

     

    ChrisA was really near to find the explanation.

    When you create a new assertion, you have to use one of the choice provided in the "Add Assertion" dialog box.

    That is to say must use "Script Assertion" otherwise the addAssertion method won't work .

     

    A workaround could be to rename the previous assertion:

     

    // Get the SOAP test step.
    def testStep = context.getTestCase().getTestStepByName("REST Request Search")
    
    // 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)
    
    // Rename the existing assertion
    testStep.getAssertionByName("Script Assertion").name = "My First Script Assertion"
    
    // 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}'")

     

     

    And here is another script that could give you more options by adding a loop on the existing assertions:

    // Get the SOAP test step.
    def testStep = context.getTestCase().getTestStepByName("REST Request Search")
    
    // 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)
    
    // Rename the existing assertion
    for(assertion in testStep.getAssertions()) {
    	if(assertion.getKey() == "Script Assertion") {
    		testStep.getAssertionByName("Script Assertion").name = "My First Script Assertion"
    	}
    }
    
    // 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}'")

    So you can add some treatment on specific type of assertion or looping for global renamming, or whatever you want.

     

    Hope this helps.

     

    David.

  • Hi,

     

    I have not actually tried adding an assertion step from Groovy Script, but it looks like you should create it with the new name, instead of creating then setting it.  I suspect you have another script in your test step called 'Script Assertion'.

     

    Instead of...

    // Add the new script assertion.
    def testAssertion = testStep.addAssertion('Script Assertion')
    testAssertion.setName(newAssertionName)

    try

    // Add the new script assertion.
    def testAssertion = testStep.addAssertion(newAssertionName)
    //testAssertion.setName(newAssertionName)

     

    • yoga46's avatar
      yoga46
      New Contributor

      Hi Chris,

      I tried the way you suggested, but it is not creating the assertion in the test step as expected, and in the next step getting he below error

      "Cannot invoke method setScriptText() on null object"

      • ZDGN's avatar
        ZDGN
        Contributor

        Hi yoga46,

         

        ChrisA was really near to find the explanation.

        When you create a new assertion, you have to use one of the choice provided in the "Add Assertion" dialog box.

        That is to say must use "Script Assertion" otherwise the addAssertion method won't work .

         

        A workaround could be to rename the previous assertion:

         

        // Get the SOAP test step.
        def testStep = context.getTestCase().getTestStepByName("REST Request Search")
        
        // 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)
        
        // Rename the existing assertion
        testStep.getAssertionByName("Script Assertion").name = "My First Script Assertion"
        
        // 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}'")

         

         

        And here is another script that could give you more options by adding a loop on the existing assertions:

        // Get the SOAP test step.
        def testStep = context.getTestCase().getTestStepByName("REST Request Search")
        
        // 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)
        
        // Rename the existing assertion
        for(assertion in testStep.getAssertions()) {
        	if(assertion.getKey() == "Script Assertion") {
        		testStep.getAssertionByName("Script Assertion").name = "My First Script Assertion"
        	}
        }
        
        // 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}'")

        So you can add some treatment on specific type of assertion or looping for global renamming, or whatever you want.

         

        Hope this helps.

         

        David.