Testing an ASP. NET web service using powershell

Source: Internet
Author: User


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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.