yoga46
4 years agoNew Contributor
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}'")
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.