Forum Discussion

jshu's avatar
jshu
Occasional Contributor
9 years ago

How to assert on condition of another assertion please?

I'm testing a simple medical rule: children of 2 years old should have MMR vaccine done

    1) if a child is not 2 years old, he is not eligible
    2) if a child is 2 years old, he is eligible for vaccine, and if he has done, then he is compliant, otherwise he's not compliant


I'm using data driven testing, the spreadsheet has patients' age and vaccine date (if he's done) like below


Header:      Age      VaccineDate      isEligible       isComplaint

                       2        01/01/2015       true               true

                       1        null                        false              null

                       2        null                        true               false


If a patient is eligible, the xml response returns patientID

If a patient is NOT eligible, the xml response returns nothing

If a patient is eligible, the xml response also returns <compliance>true</compliance>, or <compliance>false</compliance>


For Eligibility I do xPath Assertion, in "xPath Expression" I have

declare namespace NDS='http://....;
exists( //NDS:QualityGuideline/NDS:patientID)


for "Expected Result" I have ${DataSource#isEligible}


So if a patient is eligible, the node of patientID exists, "exists( //NDS:QualityGuideline/NDS:patientID)" is true and it fits "isEligible" (true) in the datasource, if a patient is NOT eligible, the node of patientID doesn't exist, "exists( //NDS:QualityGuideline/NDS:patientID)" is false, it fits "isEligible" (false) in the datasource


For Compliance I do xPath Assertion, in "xPath Expression" I have

declare namespace NDS='http://....;
//NDS:QualityGuideline/NDS:compliance

 

for "Expected Result" I have ${DataSource#isCompliant}


So if a patient is compliant, I have the response <compliance>true</compliance>, it fits "isCompliant" (true) in the datasource, if a patient is NOT compliant, the response <compliance>false</compliance> fits "isCompliant" (false) in the datasource


The assertion on eligibility works perfectly, but the assertion on compliance only works when a patient is eligible, when a patient is not eligible, there's no response for compliance and it generates an error of "<compliance> doesn't exist"


In order to fix this, I'd like to add a condition for assertion on compliance so it only works when a patient is eligible (which means the node of <patientID> exists in the response), something like:


import com.eviware.soapui.support.XmlHolder


def holder = new XmlHolder( messageExchange.responseContentAsXml )
holder.namespaces["NDS"] = "http://..."
def node = holder.getDomNode( "//NDS:QualityGuideline/patientID")


if node != null        // if the node for <patientID> exists in the response, the patient is eligible, then assert compliance

{//NDS:QualityGuideline/NDS:compliance = ${DataSource#isCompliant} }


Of course the last line doesn't work, because it's xPath assertion not script assertion, so my question is, how do I write assertion on compliance with the condition that it only asserts when patient is eligible (if not eligible it won't assert)


I don't mind using script or xPath assertion or combination, as long as it works


Thanks a lot for help


Jerry

4 Replies

  • Instead of using ${DataSource#isCompliant}, have you tried using:

     

    testRunner.testCase.getTestStepByName("Name of your datasource").getPropertyValue("isCompliant")

     

    I use this in my Groovy scripts and it works great

    • jshu's avatar
      jshu
      Occasional Contributor

      Hi Justin,

       

      Thanks a lot for the advice, I gave it a try and I have 2 questions please

       

      Here's my code

       

      ----------------------------

      import com.eviware.soapui.support.XmlHolder

       

      def holder = new XmlHolder( messageExchange.responseContentAsXml )
      holder.namespaces["NDW"] = "http://..."
      holder.namespaces["NDS"] = "http://..."

      def node = holder.getDomNode( "//NDW:mu14Response[1]/NDW:QualityGuidelineList[1]/NDS:QualityGuideline[1]/NDS:sourceAssertedID[1]" )  // check if eligible


      def resp_compliance = holder.getNodeValue( "//NDW:mu14Response[1]/NDW:QualityGuidelineList[1]/NDS:QualityGuideline[1]/NDS:isCompliant[1]" )  // get isCompliant true or false in the response


      def datasource_compliance = testRunner.testCase.getTestStepByName("DataSource").getPropertyValue("isCompliant") // get isCompliant true or false from the data source

       

      if (node != null)     // if eligible, the node exists
      { if (comp == compliance)     // if isCompliant in response is the same as in the data source
           { log.info "pass"}

         else (log.info "fail")
      }

      1) It didn't recognize Porperty "isCompliant" in data source, so for

      def datasource_compliance = testRunner.testCase.getTestStepByName("DataSource").getPropertyValue("isCompliant")

      do I need to give the project name, test case name or test step name as well please?

       

      2) "if" works, but "if ... else..." doesn't, does it suport if else or is my syntax wrong please?

       

      Thanks again for help and have a wonderful Happy New Year!

       

      Jerry

       

       

      • JustinM89's avatar
        JustinM89
        Contributor

        Is "isCompliant" the name of the column in your Excel sheet?