Thursday, June 02, 2011

Groovy 5 - adding basic assertion using Groovy in soapUI

From the time i started using soapUI(both open source & Pro), I was struggling with one very simple though a painful issue. Adding basic assertion (like SOAP response, Not SOAP Fault) in each and every test step (where number of teststep is around 10+) of your TestCase/TestSuite. There may/are few alternatives available which can be used to acheive this, like create few basic assertion in one teststep and then clonse these assertion into other teststeps.

The above mentioned apporach can be used however that also require many click here and there. So i have written few lines of code to automate that part & these code lines can be customized and extended as per your testing needs.

To proceed with just create a new Groovy Test Step and execute that test step once. Please note: i am assuming you already have all the teststep created under a testcase.

/*   
@Title : Gr-ooooo-vy 5
@Author : Pradeep Bishnoi
@Description : Add 2 basic assertion (soap & not soap fault) in all teststeps using the Groovy script
*/

import com.eviware.soapui.model.testsuite.Assertable
def project = context.testCase.testSuite.project
def testSuite = project.getTestSuiteAt(1)
def testCase = testSuite.getTestCaseAt(0)

wsdlTestSteps = testCase.getTestStepsOfType( com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.class ) //test step

def testStepCount = wsdlTestSteps.size()
( 0..< testStepCount).each
{
        // SOAP Response      Not SOAP Fault
        if (!(wsdlTestSteps[it].getAssertionByName("Not SOAP Fault")))
        {
            wsdlTestSteps[it].addAssertion("Not SOAP Fault")
        }
        if (!(wsdlTestSteps[it].getAssertionByName("SOAP Response")))
        {
            wsdlTestSteps[it].addAssertion("SOAP Response")
        }
       
 log.info("Assertion created into Teststep :: " + testCase.getTestStepByName(wsdlTestSteps[it].getName()).getName().toString())
}

Do share the same with others & provide your comments on the same, anything.
// Slight formatting/editing may be required in the code lines when pasting it into soapUI script editor.

16 comments:

  1. Hi Pradeep,

    This post is of great use for my project.

    Please let me know how the Script Assertion (with expected script being in the body part of the assertion)can be configured in the similar line.

    Thanks
    Rohit Borse

    ReplyDelete
  2. Hi Rohit,

    If i understood your question correctly, you want to copy the script assertion only from one teststep to another.

    By default the name of the Script Assertion would be "Script Assertion". So you can use this as one of the condition check (like SOAP response) and clone it.

    Or in case if you script assertion name is different (like user defined) then use the below code lines (inside the loop) to get the name of assertion and clone the assertion.

    log.info(testStepSrc.getAssertionAt(count).getName())

    I hope this will help. Else provide more details :-)

    ~~ Pradeep Bishnoi ~~

    ReplyDelete
  3. Hi Pradeep, great article. I have designed a test bed such that some of the test case actually expect a SOAP Fault. Is there a way I can kick off an assertion to check whether the response is SOAP Fault from a script?

    ReplyDelete
    Replies
    1. Hi Deepesh,

      Happen to see your comment today so adding my reply (might be useful for others).
      Yes, add a generic script assertion and then a Xpath expression (inside the script) can be used to identify if the response contains tag with some specific value (InvalidClient or Application etc). Hope this will help.

      Regards
      {Pradeep Bishnoi}

      Delete
    2. This comment has been removed by the author.

      Delete
    3. If I understood the question correctly, Pradeep is not merely adding 'SOAP Fault' assertion on the test steps, the solution where we actually expect SOAP fault?

      Regards,
      Ajay

      Delete
  4. Hello Pradeep for helping soapUI community through your posts. You are doing wonderful job. I have to made following points on your above post.

    1. You said the script is write once and it will add same assertions at all the test requests? Have I understood incorrectly? I just added these lines of code and I can see two assertions were successfully created in the test request where I added before my test request step. Is there any way to make this script available for all test requests without exclusively adding the script as a groovy step again and again.

    2. The code worked with minor change at the below line

    def testSuite = project.getTestSuiteAt(1)

    It was throwing arrayIndexOutOfBoundException. I just made index as 0 and it worked. Any reason you see?

    3. One more drawback I see with this approach is that sometimes we need to order assertions in specific order. With this approach assertions are added at the end below existing assertions and they all will be executed in the serial order. Is there any way we can add them at the desired place?

    Anticipating quick reply from you.

    Thanks
    Ajay

    ReplyDelete
    Replies
    1. Hi Ajay,

      Thanks for your feedback.
      1. Yes, it is a Groovy Script Test Step and you need to run it once, rest (adding basic assertion in all WSDL teststep) would be taken care. To achieve the later you need to write a plugin.

      2. I had index 1 because of my SampleTestSuite count under selected project was 2. And i guess you only have a single TestSuite under the selected project hence OutOfBoundException.

      3. Just trying to figure out for which scenario the order of Assertion would play a role. Anyways, that can be achieved by moving the assertion index - moveassertion(int ix, int whereTo)

      refer API docs for more details :
      http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/teststeps/WsdlTestRequestStep.html#moveAssertion(int,%20int)

      I hope this will help.

      Regards,
      {Pradeep Bishnoi}

      Delete
    2. Thanks Pradeep for the quick reply.

      I am happy with answer 2 and 3.

      I will PM you and explain you what I have done for 1. You can suggest what needs to be done to achieve what I want.

      Thanks,
      Ajay

      Delete
  5. hi pradeep

    I am testing a web service through soap ui.I need to compare the data in the response with the expected data and print test success or failure.
    please help..

    Tarun

    ReplyDelete
  6. Hi Pradeep,
    I want to generate the test suite and test cases randomly from the project.xml file, will it be possible to do so. Everything i want to automate with groovy script..

    The input should be the wsdl files, and test-request.xml files and output will be the test case execution report.

    ReplyDelete
  7. Hi Pradeep,
    I am Trupthi here.I have added one Test Request to which i am passing 10 different data through Data Source.

    Now i want to add 10 different assertion to each data.

    How can i do this please help me.

    Regards,
    Trupthi

    ReplyDelete
  8. my mail id is trupthim@gmail.com..Request to plz help me find the solution

    ReplyDelete
  9. Hi i am swapnil. i want add some advance assertion. how to add NotContains or Contains assertion using Script

    ReplyDelete
  10. Hi, Pradeep:

    wsdlTestSteps[it], what is this [it] stand for in the script? Thanks!

    David

    ReplyDelete
  11. Hi,
    I want to add SLA assertion in each and everytc under 1 test suite. Is there any way to do it. I have 100 tc in 1 test suite. I don't want to go 1 by by. Could you plz suggest step by step method if there is a way. It will be appreciated. Thanks..

    ReplyDelete