作者:? aslandhu '公司:? 中科大讯飞科技股份有限公司 '部门:? 技术质量部 '脚本功能:???? webservice自动化测试,自动读取测试用例,用例配置文件,最终将测试结果写入用例中。 '开发开始时间: 2009-4-20 '******************************************************************webservice 测试开始************************************************************* '请在使用前调试脚本确保脚本能正常工作 '请严格使用提供的模板编写用例,否则脚本无法正常工作 '***************************************************************** 收集相关信息************************************************************************ Dim SoapText,XmlHead,XmlLast,SoapQuest,MethodInput,XmlMethodString1,XmlMethodString2,WebserviceAddress,testResult,ResultPath MethodInput=InputBox("方法名称为:") Dim n,m,i,j,a,b,k,c 'n为参数个数,也即用例需要取值的列数量,m为用例数量即Excel行数,K为方法数量(保留) n=CInt(InputBox("参数个数为:")) k=CInt(inputbox("需要比较的返回值个数")) m=CInt(InputBox("Excel行数,即用例数:")) WebserviceAddress=Inputbox("接口URL为:") TestPath=Inputbox("完整用例路径:") Dim ArrayInfo() ReDim ArrayInfo(m,n) '**************************************************************** 拼装SOAP头尾消息***************************************************************** XmlHead="<?xml version=""1.0"" encoding=""utf-16""?>"&"<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">"&"<soap:Body>" XmlLast="</soap:Body>"&"</soap:Envelope>" XmlMethodString1="<"&MethodInput&" "&"xmlns=""http://tempuri.org/"">"'&"<model>" 'XmlMethodString2="</model>"&"</"&MethodInput&">" XmlMethodString2="</"&MethodInput&">" '***************************************************************** 创建Excel对象,准备读取用例************************************************ Set Excel = CreateObject("Excel.Application") oExcel.DisplayAlerts=FALSE oExcel.Visible = false oExcel.WorkBooks.Open(TestPath) oExcel.WorkSheets(MethodInput).Activate '***************************************************************** 将用例存入数组******************************************************************** For i=1 To m? '行数3,b ? For j=1 To n? '列数2,a ? ArrayInfo(i,j)= oExcel.Cells(i,j).Value ?Next Next '**************************************************************** 开始组装SOAP主体***************************************************************** SoapText="" For b=2 To m For a=1 To n SoapText=SoapText&"<"&ArrayInfo(1,a)&">"&ArrayInfo(b,a)&"</"&ArrayInfo(1,a)&">" Next SoapQuest=XmlHead&XmlMethodString1&SoapText&XmlMethodString2&XmlLast 'MsgBox(SoapQuest) WriteToFile? "c:test.txt",SoapQuest '**************************************************************** 开始循环发送消息********************************************************************* '定义一XML的文档对象 '初始化http对象 Set h = createobject( "Microsoft.XMLHTTP") h.open "POST",False h.setRequestHeader "Content-Type","text/xml; charset=utf-8" h.setRequestHeader "SOAPAction","http://tempuri.org/"&MethodInput h.send (SoapQuest) While h.readyState <> 4 Wend '***************************************************************循环处理返回结果并将结果写入用例*********************************************** For c=n+1 to n+k testResult=oExcel.Cells(1,c).Value If? oExcel.Cells(b,c).Value <>NodeData(h.responseText,testResult) then ?? oExcel.Cells(b,c+k).Value ="false" ????????? oExcel.Cells(b,c+k).Font.Color = RGB(255,225) ?? oExcel.Cells(b,c+k).Interior.Color = RGB(0,255,0) 'Background color ???????????????? oExcel.Cells(b,c+k).Font.Bold = True ?else ?? oExcel.Cells(b,c+k).Value ="true" ?? oExcel.Cells(b,c+k).Font.Color = RGB(0,0) ?? oExcel.Cells(b,c+k).Interior.Color = RGB(255,255) ' end if Next SoapText="" Next '****************************************************************释放Excel对象,并将结果写到指定路径*************************************** CheckFolder "C:TestResult" ResultPath="C:TestResult"&MethodInput&".xls" oExcel.WorkSheets(MethodInput).SaveAs ResultPath oExcel.WorkBooks.Close oExcel.Quit Set Excel=nothing '*************************************************************** 以下是被调用的函数**************************************************************** '获取结点值并输出到txt文本 function NodeData(XmlString,Node) Dim ResultArray,NodeString,NodeDateLen,No,i ResultArray=Split(XmlString,Node,-1,1) No=UBound(ResultArray) '获取数组上界 '循环查找符合要求的字节 For i=1 To No Step 2 NodeString=ResultArray(i) NodeDateLen=Len(NodeString)-3 NodeData=Mid(NodeString,2,NodeDateLen) Next End function ''将string写入文本 Sub WriteToFile (filepath,text) ? Dim fso,MyFile,txtFile if? IsFileExist(filepath)=true then ? Set fso = CreateObject("Scripting.FileSystemObject") ? Set txtFile = fso.GetFile(filepath) ? Set MyFile = txtFile.OpenAsTextStream(8,0) ? MyFile.Write text&vbcrlf ? MyFile.Close ? else ? CreatedFile(filepath) ?? Set fso = CreateObject("Scripting.FileSystemObject") ? Set txtFile = fso.GetFile(filepath) ? Set MyFile = txtFile.OpenAsTextStream(8,0) ? MyFile.Write text&vbcrlf ? MyFile.Close ? end if End Sub '创建文本文件 Sub CreatedFile(filepath) ?Set fso = CreateObject("Scripting.FileSystemObject") ??? Set MyFile = fso.CreateTextFile(filepath,true) ??? MyFile.WriteLine("<!-- Result -->") ??? MyFile.Close End Sub '判断txt文件是否存在 Function IsFileExist(filepath) ? Dim fso ? Set fso = CreateObject("Scripting.FileSystemObject") ? IsFileExist = fso.FileExists(filepath) End Function Sub CheckFolder(folderpath) Dim fso,CreateFolderSuccess,f Set fso=CreateObject("Scripting.FileSystemObject") If? fso.FolderExists(folderpath)=false Then ?Set f = fso.CreateFolder(folderpath) ? CreateFolderSuccess = f.Path ? Else ? Set fso=nothing End If End Sub |