# 4.3.5.Handling CDATA - using XPath and Groovy

## 1. CDATA

* 嵌在XML裡面的區塊, 可以是XML, 也可以是其他資訊&#x20;
* 搭配Groovy來parsing
* 無法用一般的方式parsing

## 2.Parsing CDATA

* 1.Add Assertion-> Property Content -> XPath Match ![](https://163116165-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G7tIs4o9F9vSrao%2F-M4M0IVB0ax0vs2ocz_r%2F-M4M0ZROtc8WMmgxfofV%2F6375056d-de1e-42e9-900b-9ad70bc49a6f.jpeg?generation=1586302970860617\&alt=media)
* 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 ![](https://163116165-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G7tIs4o9F9vSrao%2F-M4M0IVB0ax0vs2ocz_r%2F-M4M0ZRQM3UnIOm2pDNU%2F2018062601.jpg?generation=1586302970927618\&alt=media)
* 2.重新命名Test step: 改為getAirportInformationByAirportCode ![](https://163116165-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G7tIs4o9F9vSrao%2F-M4M0IVB0ax0vs2ocz_r%2F-M4M0ZRS-o-SW-ewQF1y%2F2018062602.jpg?generation=1586302970761436\&alt=media)
* 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'
  ```
  ````
