2013-10-30 45 views
2

我正在使用SOAP UI-GetCityWeatherByZip的演示Web服务。该脚本将从csv文件中读取不同的邮政编码,在请求xml中传递它们,获取响应并将结果存储在csv文件中。 ulresp变量用于存储响应,并且不返回null,但getnodevalue返回null。我不确定问题是什么。邮编正确被读取。该服务也运行良好。代码如下。getNodeValue()返回null,尽管响应xml持有者不是

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
def f = new File(groovyUtils.projectPath + "\\Trial\\TestData1.csv"); 
int row; 
log.info(f.absolutePath); 


def o = new File("C:\\Users\\Ananth\\Desktop\\SOAP UI\\Trial\\Output.csv"); 
log.info(o.absolutePath); 
o << "Zipcode,Responsetext \n"; 

context.fileReader = new BufferedReader(new FileReader(f)); 
line=context.fileReader.readLine(); 
row=1; 
while(line!=null) 
{ 
data=line.split(",") 
zipcode=data[0].replace("\"", ""); 
log.info("row:${row}:zipcode:${zipcode}"); 

def req=groovyUtils.getXmlHolder("GetCityWeatherByZIP#Request"); 
req["//weat:ZIP"] = "$zipcode"; 
req.updateProperty(); 
log.info("Node Zip:${req.getNodeValue('//weat:ZIP')}"); 

def ulresult = testRunner.runTestStepByName("GetCityWeatherByZIP"); 
log.info("ulresult: ${ulresult}"); 
    if(ulresult != null && ulresult.getStatus().toString() == "OK") 
    { 
     log.info("Run successful"); 

     ulresp=groovyUtils.getXmlHolder("GetCityWeatherByZIP#Response"); 

     if(ulresp != null) 
     {  
     log.info("Response:${ulresp.getNodeValue('//ResponseText')}"); 
     } 
     else 
     { 
     log.info("Response returning null"); 
     }  
    } 
      line=context.fileReader.readLine(); 
     row=row+1; 
} 

输入XML:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:weat="http://ws.cdyne.com/WeatherWS/"> 
    <soap:Header/> 
    <soap:Body> 
     <weat:GetCityWeatherByZIP> 
     <!--Optional:--> 
     <weat:ZIP>07009</weat:ZIP> 
     </weat:GetCityWeatherByZIP> 
    </soap:Body> 
</soap:Envelope> 

输出XML:

<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> 
     <GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/"> 
     <GetCityWeatherByZIPResult> 
      <Success>true</Success> 
      <ResponseText>City Found</ResponseText> 
      <State>NJ</State> 
      <City>Cedar Grove</City> 
      <WeatherStationCity>Caldwell</WeatherStationCity> 
      <WeatherID>14</WeatherID> 
      <Description>Cloudy</Description> 
      <Temperature>60</Temperature> 
      <RelativeHumidity>72</RelativeHumidity> 
      <Wind>NE7</Wind> 
      <Pressure>30.12S</Pressure> 
      <Visibility/> 
      <WindChill/> 
      <Remarks/> 
     </GetCityWeatherByZIPResult> 
     </GetCityWeatherByZIPResponse> 
    </soap:Body> 
</soap:Envelope> 

登录信息

Wed Oct 30 07:30:16 IST 2013:INFO:C:\Users\Ananth\Desktop\SOAP UI\Trial\TestData1.csv 
Wed Oct 30 07:30:16 IST 2013:INFO:FIle created 
Wed Oct 30 07:30:16 IST 2013:INFO:C:\Users\Ananth\Desktop\SOAP UI\Trial\Output.csv 
Wed Oct 30 07:30:16 IST 2013:INFO:row:1:zipcode:07008 
Wed Oct 30 07:30:18 IST 2013:INFO:Node Zip:07008 
Wed Oct 30 07:30:21 IST 2013:INFO:ulresult: c[email protected]15929b1 
Wed Oct 30 07:30:21 IST 2013:INFO:Run successful 
Wed Oct 30 07:30:21 IST 2013:INFO:Response:null 
Wed Oct 30 07:30:21 IST 2013:INFO:row:2:zipcode:07009 
Wed Oct 30 07:30:21 IST 2013:INFO:Node Zip:07009 
Wed Oct 30 07:30:22 IST 2013:INFO:ulresult: c[email protected]10b9a14 
Wed Oct 30 07:30:22 IST 2013:INFO:Run successful 
Wed Oct 30 07:30:22 IST 2013:INFO:Response:null 

回答

3

很可能是因为响应具有名称空间,因此您必须在响应中访问项目之前定义名称空间。使用如:

ulresp.namespaces["ns"] = "http://ws.cdyne.com/WeatherWS/" 

然后

ulresp.getNodeValue('//ns:ResponseText') 

应该看起来像

..... 
ulresp=groovyUtils.getXmlHolder("GetCityWeatherByZIP#Response") 
ulresp.namespaces["ns"] = "http://ws.cdyne.com/WeatherWS/" 

if(ulresp != null) {  
    log.info("Response:${ulresp.getNodeValue('//ns:ResponseText')}") 
} else { 
    log.info("Response returning null") 
} 
...... 
+0

太感谢你了。它的工作 –

+0

感谢分享。在我的情况下,我使用命名空间[“ns”]来请求而不是响应,并且工作很棒! –