Here we have first Guest Blog from one of our blog reader & now a proud author Ajay Pawar.

—————————————————————————————————————————————————————

There are some situations when you need to add assertions in each test step manually. The chances are that you may forget to add them. Moreover it is a tiresome process to add them manually especially when you have hundreds of test cases.

What is solution? Can we automate this by any means? Yes we can write simple groovy script for this purpose.
First thing first! You must read Pradeep’s one more fantastic blog on similar subject before reading this blog:

https://learnsoapui.wordpress.com/2011/06/02/groovy-5-adding-basic-assertion-using-groovy-in-soapui/

Then why one more blog on same subject? Well we are going to meet same goal but this time for a different style of test suite where above blog would need enhancement.

Here we go. Have you noticed soapUI facilitates you to design test suite in two different styles?
1. One TestCase for each Operation

2. Single TestCase with One Request for each Operation

Let’s try both the styles to generate test suits and see how they look. I am using Amazon WSDL at following site
http://webservices.amazon.com/AWSECommerceService/UK/AWSECommerceService.wsdl

In the soapUI create new project with the name Amazon 1 by importing above WSDL. Once imported the WSDL successfully, following screen is appeared to generate TestSuite

Let’s select first option (One TestCase for each Operation) in the Style and click OK.
Your project should seem as below

To try with another style let’s create one more soapUI project with the same WSDL. Give different name to the project Amazon 2. One more time a screen to generate testSuite will appear. This time selects second option (Single TestCase with One Request for each Operation) in the Style and your project should appear like this:

We are done with the two different styles of test suite generation and how they look in the projects window.

As mentioned at the start, Pradeep has explained how to add assertions automatically using Groovy script. The approach really does a magic when you generate test suite as we created second time.
However for the first style, we need to think about some other solution. soapUI is such a fantastic tool there is not dearth of options.

We have a couple of options in our hand to meet the task.
1. Write Groovy script in the Project Save/Load Script
2. Write Groovy script in the Project Event listener

You might ask me where is Event Listener? I understand if you have not yet tried soapUI Pro, you may not be familiar about it. No worries. As you know Project load script is available in soapUI open source version. I would also recommend using first option (even though you have soap UI Pro!) as it fits for the purpose.

Right click on Project and select Show Project View option. Click on Overview tab and select Save/Load Script tab at the beneath of the screen.
Paste following script in the Load Script section

/*
@Author : Ajay Pawar / Pradeep Bishnoi
@Description : Adding basic assertions (SOAP Response, Not SOAP Fault, Contains IsSuccess=true ) in all teststeps using the Groovy script
*/

log.info "Loading/Saving project : $project.name ..."
log.info "Adding 3 Basic assertions into all testSuite/testCases/teststeps... if not already exists."
testSuiteList = project.getTestSuites()
text = "~"

testSuiteList.each
{
testSuite = project.getTestSuiteByName(it.key)
testCaseList = testSuite.getTestCases()
log.info " ${text*5} Adding assertion in testSuite :: $testSuite.name... if not already exists."
testCaseList.each
{
testCase = testSuite.getTestCaseByName(it.key)
log.info " ${text*5} Adding assertion in testcase :: $testCase.name... if not already exists."
wsdlTestSteps = testCase.getTestStepsOfType( com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.class ) //only WsdlTestRequest steps
wsdlTestSteps.each
{
// Adding 'SOAP Response' Assertion if not exists already
if (!(it.getAssertionByName("SOAP Response")))
{
log.info "${text*10}   SOAP response assertion not found in -- " + it.name
it.addAssertion("SOAP Response")
log.info "${text*10}   Added the SOAP Response assertion in -- " + it.name
}
// Adding 'Not SOAP Fault' Assertion if not exists already
if (!(it.getAssertionByName("Not SOAP Fault")))
{
log.info "${text*10}   Not SOAP Fault assertion not found in -- " + it.name
it.addAssertion("Not SOAP Fault")
log.info "${text*10}   Added the Not SOAP Fault assertion in -- " + it.name
}
}
}
}

Above code runs each time you load the project. It adds 2 basic assertions in each test step, if they are not already present.

Do you still want to go with Event Handler? No problem use above script with minor tweak.
Again Right click on your Project and select Show Project View option. Click on Event tab. Click on Add button and choose ‘TestRunListener.beforeStep’ from the list. Paste above code.

Since event handler script is not invoked with project scope, you need retrieve it at the beginning of the script (add this single code line at beginning of above code).

def project = context.testCase.testSuite.project

The script would be called automatically when a new test would be created. Please make a note that you should not limit yourself by adding only couple of assertions. Go ahead and extend the script. Add all the assertions you would normally add in your test cases at your current project.

We are done. I hope it helps you all.

About Author :

Ajay Pawar is a IBM middleware consultant. Over the years, he has worked on number of projects in SOA environment using IBM family tools like WebSphere Process Server, WebSphere Integration Developer, WebSphere MQ and Websphere Service Registry and Repository, to name a few. He has been extensively using soapUI tool from last 3 years.

Advertisements
Comments
  1. Aaron Lau says:

    how about removing assertions?
    it.removeAssertion()

  2. Anirv says:

    How to add simple contains assertion via groovy script?

  3. Greg says:

    In my humble opinion it would be better to use:
    project.getTestSuiteList() insted of project.getTestSuites()
    and
    testSuite.getTestCaseList() insted of testSuite.getTestCases()
    two lines less.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s