Search code examples
seleniumautomationweb-api-testingkarate

How to export response into a file or validate the tag value with the help of Karate Framework


I'm using the following feature file and it generates the response. How can we store the response into an XML file instead of showing the console?

Feature File:

Feature: Test soap end point

    Background:
    * url 'sample url'

    Scenario: SmokeTest
       Given request read('getMbrWksMembershipDetails.xml')
       When soap action 'test url' 
       Then status 200
       And print response 

EDITED: The response I'm getting like this.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:getMbrWksMembershipDetailsResponse xmlns:ns1="xxxxxxxxxx">
         <ns4:WksMembershipSummaryResponse xmlns:ns2="xxxxxxxx" xmlns:ns3="xxxxxxxxxx" xmlns:ns4="xxxxxxxxxx">
            <ns2:customerSummary>
               <ns2:address>
                  <ns2:city>SOUTH CHESTERFIELD</ns2:city>
                  <ns2:country>USA</ns2:country>
                  <ns2:isoCountryCode>US</ns2:isoCountryCode>
                  <ns2:line1>9998, N. MICHIGAN ROAD.</ns2:line1>
                  <ns2:postalCode>23834</ns2:postalCode>
                  <ns2:state>VA</ns2:state>
               </ns2:address>
               <ns2:allowPasswordChange>true</ns2:allowPasswordChange>
               <ns2:arpMember>false</ns2:arpMember>
               <ns2:brandCode>ABC</ns2:brandCode>
               <ns2:brandId>1</ns2:brandId>
               <ns2:companyCode>ABC</ns2:companyCode>
               <ns2:eliteMemberRewardStatus>false</ns2:eliteMemberRewardStatus>.....

Question: How can I validate the tag values from this response?

Thanks,


Solution

  • First a question - why do you want to do this ? Karate is a testing framework, and besides being able to run assertions on responses, you can easily re-use responses (or some data from a response) in the next request. There is no need to ever save anything to a file.

    And if you follow the instructions in the Karate documentation on logging you will also see responses logged to the file target/karate.log.

    When you use the JUnit runner, you will see all responses in an HTML report.

    Same case with the parallel runner.

    Anyway, even after all this if you really really want to save responses to a file, use Java interop and write your own utility to save to a file, it will be a just a few lines of code. It is not built-in to Karate because of the above reasons.

    EDIT: well, after all the typing above, turns out you just want to know how to validate (assert / match) the response ? Great, that is the whole selling point of Karate.

    I assume you have read the Karate documentation. Also refer this example: soap.xml and this one for more ideas: xml.feature

    Anyway here are some hints based on your XML sample to get you started. You can paste the below into a Scenario: and run this without needing to make HTTP requests and that way test and get comfortable with match:

    * def response = 
    """
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <soap:Body>
          <ns1:getMbrWksMembershipDetailsResponse xmlns:ns1="xxxxxxxxxx">
             <ns4:WksMembershipSummaryResponse xmlns:ns2="xxxxxxxx" xmlns:ns3="xxxxxxxxxx" xmlns:ns4="xxxxxxxxxx">
                <ns2:customerSummary>
                   <ns2:address>
                      <ns2:city>SOUTH CHESTERFIELD</ns2:city>
                      <ns2:country>USA</ns2:country>
                      <ns2:isoCountryCode>US</ns2:isoCountryCode>
                      <ns2:line1>9998, N. MICHIGAN ROAD.</ns2:line1>
                      <ns2:postalCode>23834</ns2:postalCode>
                      <ns2:state>VA</ns2:state>
                   </ns2:address>
                </ns2:customerSummary>
            </ns4:WksMembershipSummaryResponse>
        </ns1:getMbrWksMembershipDetailsResponse>
      </soap:Body>
    </soap:Envelope>
    """
    * match //address/city == 'SOUTH CHESTERFIELD'
    
    * match //customerSummary/address == 
        """
        <ns2:address>
           <ns2:city>SOUTH CHESTERFIELD</ns2:city>
           <ns2:country>USA</ns2:country>
           <ns2:isoCountryCode>US</ns2:isoCountryCode>
           <ns2:line1>9998, N. MICHIGAN ROAD.</ns2:line1>
           <ns2:postalCode>23834</ns2:postalCode>
           <ns2:state>VA</ns2:state>
        </ns2:address>
        """