4.3.5.Handling CDATA - using XPath and Groovy

1. CDATA

  • 嵌在XML裡面的區塊, 可以是XML, 也可以是其他資訊

  • 搭配Groovy來parsing

  • 無法用一般的方式parsing

2.Parsing CDATA

  • 1.Add Assertion-> Property Content -> XPath Match

  • 2.點選Declare

  • 3.填上路徑

    • e.g.,

          <getAirportInformationByAirportCodeResponse xmlns="http://www.webserviceX.NET">
               <getAirportInformationByAirportCodeResult><![CDATA[<?xml version="1.0" encoding="UTF-8"?><NewDataSet>
                <Table>
                  <AirportCode>LHR</AirportCode>
                  <CityOrAirportName>LONDON HEATHROW</CityOrAirportName>
                  <Country>Great Britain (UK)</Country>
                  <CountryAbbrviation>GB</CountryAbbrviation>
                  <CountryCode>493</CountryCode>
                  <GMTOffset>0</GMTOffset>
                  <RunwayLengthFeet>12802</RunwayLengthFeet>
                  <RunwayElevationFeet>80</RunwayElevationFeet>
                  <LatitudeDegree>51</LatitudeDegree>
                  <LatitudeMinute>28</LatitudeMinute>
                  <LatitudeSecond>0</LatitudeSecond>
                  <LatitudeNpeerS>N</LatitudeNpeerS>
                  <LongitudeDegree>0</LongitudeDegree>
                  <LongitudeMinute>27</Longitude
                  <LongitudeSeconds>0</LongitudeSeconds>
                  <LongitudeEperW>W</LongitudeEperW>
                </Table>
    • 應填入

3.Parsing CDATA by using Groovy

  • 1.Add Assertion-> Property Content -> XPath Match

  • 2.重新命名Test step: 改為getAirportInformationByAirportCode

  • 3.編輯script

      1. context.expand放的字串順序為: Test step名稱#Response#declare...;//ns1

      1. 用groovy.util.XmlSlurper來parse出想要得到的key

      import groovy.util.XmlSlurper
      //1. Extract CDATA from the response
      
      //declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/';
      //declare namespace ns1='http://www.webserviceX.NET';
      //ns1:getAirportInformationByAirportCodeResponse/ns1:getAirportInformationByAirportCodeResult/text()
      def responseText = context.expand('${getAirportInformationByAirportCode#Response#declare namespace       ns1=\'http://www.webserviceX.NET\';//ns1:getAirportInformationByAirportCodeResponse/ns1:getAirportInformationByAirportCodeResult/text()}')
      log.info responseText
      
      XmlSlurper xmlSlurper = new XmlSlurper()
      
      def xmlData = xmlSlurper.parseText(responseText)
      
      log.info xmlData
      
      //2. I want to Assert airportCode == 'LHR'
      log.info xmlData.Table[0].AirportCode
      
      String airportCode= xmlData.Table[0].AirportCode
      
      assert airportCode == 'LHR'
      
      log.info 'Assering done'

Last updated

Was this helpful?