getNodeValue() returns null although response xml holder does not
10,138
Most likely because response has a namespace, you have to define the namespace before accessing an item in the response. Use as:
ulresp.namespaces["ns"] = "http://ws.cdyne.com/WeatherWS/"
and then
ulresp.getNodeValue('//ns:ResponseText')
Should look like
.....
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")
}
......
Author by
Admin
Updated on June 05, 2022Comments
-
Admin almost 2 years
I am using the demo web service of SOAP UI-GetCityWeatherByZip. The script is to read different zip codes from a csv file, pass them in the request xml, get the response and store the results in a csv file. ulresp variable is used to store the response and is not returning null but getnodevalue is returning null. I am not sure what the problem is. The zipcodes are correctly being read. The service also runs fine.Code is as follows.
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; }
Input 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>
Output 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>
Log info
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: com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStepResult@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: com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStepResult@10b9a14 Wed Oct 30 07:30:22 IST 2013:INFO:Run successful Wed Oct 30 07:30:22 IST 2013:INFO:Response:null
-
Gueorgui Obregon almost 8 yearsThanks for sharing. In my case I use namespaces["ns"] for request instead response and works great!!