In a recent post I described a very easy way to programmatically test an ASP. net web service using JavaScript and the xmlhttp com object. windows powershell, the new command shell and scripting language, has generated a lot of interest in the testing community here at Microsoft, and so, several people asked me about testing a web service using powershell. well, because powershell can call COM objects, one easy way of testing a web service with powershell is almost identical to testing with JavaScript. in both cases the idea is to use an XMLHTTP object to send a request to the Web service under test, fetch the XML response, and examine the response for an expected value. suppose you create an ASP. net web service which contains a dummy Web method named trimax (x, y, z) which simply returns the largest of integers x, y, and z. listed below is the basic skeleton of a powershell test automation script which sends 4, 8, 6 to trimax () and looks for a result of 8. the XMLHTTP object is normally used to send asynchronous requests to an Ajax web application, but by setting the second parameter to "false" you can send a synchronous request. you can determine the http post data using Visual Studio by instructing the ASP. net web service to run -- it gives you the post template that you can copy-paste into your powershell script. # testwebservice. PS1 write-host "'nbegin web service test"
$ Req = new-object-com "Microsoft. XMLHTTP"
$ Req. Open ("Post", "http: // localhost/myservice/service. asmx", $ false)
$ Req. setRequestHeader ("Content-Type", "text/XML; charset = UTF-8 ")
$ Req. setRequestHeader ("soapaction", "http://tempuri.org/TriMax") $ requeststring = '<? XML version = "1.0" encoding = "UTF-8"?> '
$ Requeststring + = '<soap: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/">'
$ Requeststring + = '<soap: Body>'
$ Requeststring + = '< trimax xmlns = "http://tempuri.org/">'
$ Requeststring + = '<x> 4 </x>'
$ Requeststring + = '<Y> 8 </Y>'
$ Requeststring + = '<z> 6 </z>'
$ Requeststring + = '</trimax>'
$ Requeststring + = '</soap: Body>'
$ Requeststring + = '</soap: envelope>'
$ Req. Send ($ requeststring)
$ Response = $ Req. responsetext
Write-host "'nresponse is $ response' N" if ($ response. indexof ("<trimaxresult> 8 </trimaxresult>")-GE 0 ){
Write-host "pass"
}
Else {
Write-host "** fail **"
} Write-host "'nend test"