博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Testing an ASP.NET Web Service using PowerShell
阅读量:4977 次
发布时间:2019-06-12

本文共 2268 字,大约阅读时间需要 7 分钟。

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", " ", $false)

$req.setRequestHeader("Content-Type", "text/xml; charset=utf-8")

$req.setRequestHeader("SOAPAction", " ")
 
$requestString  = '<?xml version="1.0" encoding="utf-8"?>'

$requestString += '<soap:Envelope xmlns:xsi=" " xmlns:xsd=" " xmlns:soap="

$requestString += '  <soap:Body>'

$requestString += '    <TriMax xmlns="

$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"
 

转载于:https://www.cnblogs.com/javafun/archive/2009/02/13/1390286.html

你可能感兴趣的文章
九涯的第一次
查看>>
处理器管理与进程调度
查看>>
向量非零元素个数_向量范数详解+代码实现
查看>>
java if 用法详解_Java编程中的条件判断之if语句的用法详解
查看>>
matlab sin函数 fft,matlab的fft函数的使用教程
查看>>
mysql adddate()函数
查看>>
mysql sin() 函数
查看>>
单片机复位电路
查看>>
php json_decode失败,返回null
查看>>
3-day3-list-truple-map.py
查看>>
Edit控件显示多行文字
查看>>
JS第二周
查看>>
dataTable.NET的search box每輸入一個字母進行一次檢索的問題
查看>>
Python 文件处理
查看>>
邻接表详解
查看>>
迭代dict的value
查看>>
eclipse package,source folder,folder区别及相互转换
查看>>
Py 可能是最全面的 python 字符串拼接总结(带注释版)
查看>>
《Java程序设计实验》 软件工程18-1,3 OO实验2
查看>>
【Herding HDU - 4709 】【数学(利用叉乘计算三角形面积)】
查看>>