Groovy - 编写对文件的响应部分

时间:2014-08-20 09:02:21

标签: groovy response soapui filewriter

我想写一些文件的响应部分。文件看起来像:

Date_and_hour; city_name1; temperatureC
Date_and_hour; city_name2; temperatureC

每次测试都会产生新线。 我有写文件部分响应的问题,不知道该怎么做。

响应:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetWeatherResponse xmlns="http://www.webserviceX.NET">
            <GetWeatherResult><![CDATA[<?xml version="1.0" encoding="utf-16"?>
                <CurrentWeather>
                    <Location>NEWPORT STATE AIRPORT, RI, United States (KUUU) 41-32N 071-17W 51M</Location>
                    <Time>Aug 20, 2014 - 04:10 AM EDT / 2014.08.20 0810 UTC</Time>
                    <Wind> Calm:0</Wind>
                    <Visibility> 3/4 mile(s):0</Visibility>
                    <SkyConditions> obscured</SkyConditions>
                    <Temperature> 57.9 F (14.4 C)</Temperature>
                    <DewPoint> 55.9 F (13.3 C)</DewPoint>
                    <RelativeHumidity> 93%</RelativeHumidity>
                    <Pressure> 29.97 in. Hg (1014 hPa)</Pressure>
                    <Status>Success</Status>
                </CurrentWeather>]]></GetWeatherResult>
        </GetWeatherResponse>
    </soap:Body>
</soap:Envelope>

Groovy代码:

import com.eviware.soapui.support.XmlHolder
import java.text.MessageFormat
import org.apache.commons.lang.ObjectUtils

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def retrieve = groovyUtils.getXmlHolder("GetWeather#Response")
if (!ObjectUtils.equals(retrieve.getNodeValue("//*:Location"), "string")){
    def currentTime = System.currentTimeMillis()
    def fullFilePath = context.expand("d:/Groovy/Scripts") + File.separator + "File.txt"
    def reportFile = new File(fullFilePath)
    if (!reportFile.exists())
    {
        reportFile.createNewFile()
        reportFile.append((Object)retrieve.getPrettyXml(), 'UTF-16')
    }
}

1 个答案:

答案 0 :(得分:0)

您的<GetWeatherResult>值是CDATA,因此首先您需要解析为XMLHolder才能获得<location><time><Temperature>值通过xpath表达式。然后你必须检查文件是否存在,如果需要则创建,否则直接附加内容,你的代码可能是:

import com.eviware.soapui.support.XmlHolder
import java.text.MessageFormat
import org.apache.commons.lang.ObjectUtils

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def retrieve = groovyUtils.getXmlHolder("GetWeather#Response")
// get <wheaterResult> value which is CDATA
def wheaterResultCDATA = retrieve.getNodeValue("//*:GetWeatherResult")
if (wheaterResultCDATA != null){

    def currentTime = System.currentTimeMillis()
    def fullFilePath = context.expand("d:/Groovy/Scripts") + File.separator + "File.txt"
    def reportFile = new File(fullFilePath)

    if (!reportFile.exists()){reportFile.createNewFile()}

    // get CDATA and parse as XML
    def wheaterResult = groovyUtils.getXmlHolder(wheaterResultCDATA);
    // get temperatue, city and time
    def time = wheaterResult.getNodeValue("//*:Time")
    def location = wheaterResult.getNodeValue("//*:Location")
    def temperature = wheaterResult.getNodeValue("//*:Temperature")
    // append the information to the file
    reportFile.with{
            append(time,'UTF-16')
            append(';','UTF-16')
            append(location,'UTF-16')
            append(';','UTF-16')
            append(temperature,'UTF-16')
            append('\n','UTF-16')
        }
}

希望这有帮助,