cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
yoga46
New 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}'")

 

 

Script Assertion.PNG

 

 

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
ZDGN
Contributor

Re: Getting Assertion Renaming Box while adding Script assertion using Groovy Script. How to avoid i

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.

View solution in original post

4 REPLIES 4
ChrisA
Contributor

Re: Getting Assertion Renaming Box while adding Script assertion using Groovy Script. How to avoid i

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
New Contributor

Re: Getting Assertion Renaming Box while adding Script assertion using Groovy Script. How to avoid i

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
Contributor

Re: Getting Assertion Renaming Box while adding Script assertion using Groovy Script. How to avoid i

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.

View solution in original post

yoga46
New Contributor

Re: Getting Assertion Renaming Box while adding Script assertion using Groovy Script. How to avoid i

Thanks a lot, @ZDGN. It is working as I was expecting.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors