Forum Discussion

Tareque's avatar
Tareque
Occasional Contributor
17 years ago

Adding assertions to TestRequest dynamically using Groovy

Hi,
I have an urgent requirement in my project which needs to be implemented.
The requirement is that all the Input XML and the XPath assertions will be moved to the database.
The project file will not have the input XML or assertions in the Test Request.
To execute this we have created the following steps.
1. Datasource step to read the request_xml and assertion_text from DB
select request_xml, test_case_name, request_id, assert_text
  from TEST_REQUEST_DATA A, ASSERTIONS_DATA B
  where A.REQUEST_ID = B.ASSERT_REQUEST_ID
 
2. Property Transfer step to feed the request_xml property from the DataSource to the TestRequest Step.

3. Groovy Script to add assertions to the Test Request by getting the assertion_text from the DataSource

def assertText = testRunner.getTestCase().getTestStepByName("DataSource").getPropertyValue("assert_text")
testRunner.getTestCase().getTestStepByName("Test Request").addAssertion(assertText)

Step 3 fails giving the following exception
2008-01-22 14:53:29,125 ERROR [errorlog] java.lang.NullPointerException
java.lang.NullPointerException
at com.eviware.soapui.impl.wsdl.panels.teststeps.AssertionsPanel$AssertionListModel.addAssertion(AssertionsPanel.java:358)
at com.eviware.soapui.impl.wsdl.panels.teststeps.AssertionsPanel$AssertionListModel.assertionAdded(AssertionsPanel.java:350)
at com.eviware.soapui.impl.wsdl.support.assertions.AssertionsSupport.fireAssertionAdded(AssertionsSupport.java:135)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest.addAssertion(WsdlTestRequest.java:187)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.addAssertion(WsdlTestRequestStep.java:539)
at gjdk.com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:119)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at Script1.run(Script1.groovy:14)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:59)
at com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SourceFile:51)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:140)
at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction.actionPerformed(GroovyScriptStepDesktopPanel


My question is:
Is it possible to dynamically add assertions to a TestRequest?
And if it is could you please point out where I am going wrong.
Would appreacite any help with this problem.
Best Regards
tareque

18 Replies

  • AnandKiran's avatar
    AnandKiran
    Frequent Contributor
    Hi Ole,

    How to use the XmlUtils.declareXPathNamespaces( String xmlString ) for declaring the namespaces to the XPath assertion. Can you provide a code snippet ??

    Regards
    Anand
  • bennythedroid's avatar
    bennythedroid
    Occasional Contributor
    Did anybody ever follow up on those last couple questions? Anyone have anything to add to any of it?

    I'm trying to use some of the suggestions here for my own testing.
  • Nullius's avatar
    Nullius
    Occasional Contributor
    mkliba wrote:
    Hi:

    Talking about removing/adding/renaming Assertions, you posted before that to remove assertions we can use:
    def assertionsList = testRunner.getTestCase().getTestStepByName("Test Request").getAssertionList()
    for( e in assertionsList){
      testRunner.getTestCase().getTestStepByName("Test Request").removeAssertion(e)
    }

    Is there any way we can remove the assertion by assertion type and/or assertion name? Ex: Remove all type "XPath Match", or with name "My XPath Match"?

    Thanks.


    You could use the instanceof operator.
    For example, if you want to remove all GroovyScriptAssertions:
    def assertionsList = ...
    for (e in assertionsList)
    {
    if (e instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion)
    testRunner.getTestCase().getTestStepByName("Test Request").removeAssertion(e)
    }


    Alls types of assertions:
    CrossSiteScriptAssertion, GroovyScriptAssertion, HttpDownloadAllResourcesAssertion, InvalidHttpStatusCodesAssertion, JdbcStatusAssertion, JdbcTimeoutAssertion, JMSStatusAssertion, JMSTimeoutAssertion, NotSoapFaultAssertion, ResponseSLAAssertion, SchemaComplianceAssertion, SensitiveInfoExposureAssertion, SimpleContainsAssertion, SimpleNotContainsAssertion, SoapFaultAssertion, SoapRequestAssertion, SoapResponseAssertion, ValidHttpStatusCodesAssertion, WSARequestAssertion, WSAResponseAssertion, WsdlMessageAssertion, WSSStatusAssertion, XPathContainsAssertion, XQueryContainsAssertion

    They all inherit from the TestAssertion interface:
    http://www.soapui.org/apidocs/com/eviwa ... rtion.html
    (links to all assertion types can also be found on that webpage)

    I haven't tried the code I provided, but it should work without any major changes.
  • Hi ,
    I have successfully created the dynamic assertion using the above code.
    But when I run it from the test case, it fails for some of the steps and passed for some of them. However individually when i run it passed for individual values.
    I have the following code...

    for(i in 1..30 )
    {
    log.info ( "this is in IF i " + i)
    def subscriberListValue =holder["//Row[$i]/SUBSCRIBER_ID[1]"]
    def props1 = testRunner.testCase.getTestStepByName( "Count_GetDistinct_MembersforEach_SubscriberID" ) // This is my JDBC Step where I am adding the assertion //
    def assertionsList = testRunner.getTestCase().getTestStepByName("Count_GetDistinct_MembersforEach_SubscriberID").getAssertionList()
    for( e in assertionsList){
    testRunner.getTestCase().getTestStepByName("Count_GetDistinct_MembersforEach_SubscriberID").removeAssertion(e)
    }
    def assertionName = "AssertXPATH" + "$i"
    def assertText = testRunner.getTestCase().getTestStepByName("Properties").getPropertyValue("assert_Text") // This is "count(//MEMBER_ID)"
    def assertion = testRunner.getTestCase().getTestStepByName("Count_GetDistinct_MembersforEach_SubscriberID").addAssertion("XPath Match")
    println " \n\n ########## Assertion Name " + assertionName
    assertion.name = assertionName
    assertion.path = assertText

    assertion.expectedContent = noOfMember // This is the number of members from a XML file and it changes according to my XPATH each time
    testRunner.runTestStepByName("Count_GetDistinct_MembersforEach_SubscriberID")
    }

    The strange thing is i can find some of the steps passed and some failed when run. However it passed when run individually. Please find the attached screen-shot.
    Please let me know if anything i am doing wrong

    Thanks,
    Manoj
  • Hi,
    In other words , the adding assertions works if we add to different steps, but it behaves strange when we add assertions to the same step and run on loop.
    The below code works for me , but I want to avoid to create the different steps in test suite and adding separate assertion.
    Please let me know if this is possible...
    for(i in 1..30 )
    {

    def subscriberListValue =holder["//Row[$i]/SUBSCRIBER_ID[1]"]
    def props1 = testRunner.testCase.getTestStepByName("$i" + "Count_GetDistinct_MembersforEach_SubscriberID" ) // This is my JDBC Step where I am adding the assertion //
    def assertionsList = testRunner.getTestCase().getTestStepByName("$i" +"Count_GetDistinct_MembersforEach_SubscriberID").getAssertionList()
    for( e in assertionsList){
    testRunner.getTestCase().getTestStepByName("$i" +"Count_GetDistinct_MembersforEach_SubscriberID").removeAssertion(e)
    }
    def assertionName = "AssertXPATH" + "$i"
    def assertText = testRunner.getTestCase().getTestStepByName("Properties").getPropertyValue("assert_Text") // This is "count(//MEMBER_ID)"
    def assertion = testRunner.getTestCase().getTestStepByName("Count_GetDistinct_MembersforEach_SubscriberID").addAssertion("XPath Match")
    println " \n\n ########## Assertion Name " + assertionName
    assertion.name = assertionName
    assertion.path = assertText

    assertion.expectedContent = noOfMember // This is the number of members from a XML file and it changes according to my XPATH each time
    testRunner.runTestStepByName("$i" +"Count_GetDistinct_MembersforEach_SubscriberID")
    }
  • mgupta1405's avatar
    mgupta1405
    Occasional Contributor
    Nullius wrote:
    mkliba wrote:
    Hi:

    Talking about removing/adding/renaming Assertions, you posted before that to remove assertions we can use:
    def assertionsList = testRunner.getTestCase().getTestStepByName("Test Request").getAssertionList()
    for( e in assertionsList){
      testRunner.getTestCase().getTestStepByName("Test Request").removeAssertion(e)
    }

    Is there any way we can remove the assertion by assertion type and/or assertion name? Ex: Remove all type "XPath Match", or with name "My XPath Match"?

    Thanks.


    You could use the instanceof operator.
    For example, if you want to remove all GroovyScriptAssertions:
    def assertionsList = ...
    for (e in assertionsList)
    {
    if (e instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion)
    testRunner.getTestCase().getTestStepByName("Test Request").removeAssertion(e)
    }


    Alls types of assertions:
    CrossSiteScriptAssertion, GroovyScriptAssertion, HttpDownloadAllResourcesAssertion, InvalidHttpStatusCodesAssertion, JdbcStatusAssertion, JdbcTimeoutAssertion, JMSStatusAssertion, JMSTimeoutAssertion, NotSoapFaultAssertion, ResponseSLAAssertion, SchemaComplianceAssertion, SensitiveInfoExposureAssertion, SimpleContainsAssertion, SimpleNotContainsAssertion, SoapFaultAssertion, SoapRequestAssertion, SoapResponseAssertion, ValidHttpStatusCodesAssertion, WSARequestAssertion, WSAResponseAssertion, WsdlMessageAssertion, WSSStatusAssertion, XPathContainsAssertion, XQueryContainsAssertion

    They all inherit from the TestAssertion interface:
    http://www.soapui.org/apidocs/com/eviwa ... rtion.html
    (links to all assertion types can also be found on that webpage)

    I haven't tried the code I provided, but it should work without any major changes.



    Thanks very much. I was exactly looking for the same.

    -Mohit
  • latiffsyed's avatar
    latiffsyed
    New Contributor
    Hi,

    Can any one help me plz. I have a assertion with name (xxx) in a test step (yyy) in all testcases under all test suites of a project. I am trying to remove all assertions with name(xxx) from all the test steps (yyy) in all test cases. I have seen above posts the following script can remove assertions in test case level but not project level.

    def assertionsList = testRunner.getTestCase().getTestStepByName("Test Request").getAssertionList()
    for( e in assertionsList){
    testRunner.getTestCase().getTestStepByName("Test Request").removeAssertion(e)
    }

    I have developed till here but not getting success


    def hMap_projectTestSuiteList = testRunner.testCase.testSuite.project.getTestSuiteList();
    def targetList = hMap_projectTestSuiteList;
    def testCaseList;
    def testStepList;

    // iterate testSuites
    for( obj_testSuite in targetList) {
    log.info( "testSuite = " + obj_testSuite.getName() );
    testCaseList = obj_testSuite.getTestCaseList()

    // iterate testCases
    for( obj_testCase in testCaseList) {
    log.info( "testCase = " + obj_testCase.getName() );
    testStepList = obj_testCase.getTestStepList();

    //get testStep list to disable
    for( obj_testStep in testStepList){
    if( obj_testStep.getModelItem().getClass().toString() == "class com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep" )
    if (obj_testStep.getName() == "XXXXXXXXXX"){
    //log.info( " --------- obj = " + obj_testStep.getModelItem().getClass().toString() + " --- ");

    def assertionsList = testRunner.getTestCase().getTestStepByName(obj_testStep.getName()).getAssertionList()
    for( e in assertionsList){
    testRunner.getTestCase().getTestStepByName(obj_testStep.getName()).removeAssertion(e)
    }

    }
    }
    }
    }


    Thanks,

    Latiff.
  • suamy88's avatar
    suamy88
    Occasional Contributor
    Hi everyone! I am just learning how to use SoapUI, and making some investigation about XQuery assertion, I got a doubt, I am trying to make some assertion...This is what I did..

    declare namespace ns2='http://front.vtsuite.smartbt.com/';
    declare namespace S='http://schemas.xmlsoap.org/soap/envelope/';

    //ns2:loginResponse/login/clerkPrivileges

    My Expected Result is this one

    <clerkPrivileges>
    <allowCardPayment>true</allowCardPayment>
    <allowCardPaymentManual>true</allowCardPaymentManual>
    <allowChangeSettings>true</allowChangeSettings>
    <allowDashboard>true</allowDashboard>
    <allowMyTransactions>true</allowMyTransactions>
    <allowRefund>true</allowRefund>
    <allowRefundManual>true</allowRefundManual>
    <allowVoid>true</allowVoid>
    </clerkPrivileges>

    My question is, those privileges can be true or false, how can validate them to false?
    Is that posible...
    Thanks...I really need some help