虽然现今数据大部分都是使用数据库存储,但是停电断网等不可抗拒因素经常中断着automation的进行,这时若这些非业务的配置数据用xml来存储就显得方便很多了,下面是用VBScript脚本读取XML文件的实现方法,记录下以作备忘。
1. XML文件的节点信息:
<?xmlversion="1.0"?> <TC_Testlogon> <TNSlogon> <DataID>1</DataID> <I_Type>TNSlogon</I_Type> <I_TNSName>ELOG</I_TNSName> <I_Username>ASD</I_Username> <I_Password>asd</I_Password> <I_SavePassword>1</I_SavePassword> </TNSlogon> <TNSlogon> <DataID>3</DataID> <I_Type>TNSlogon</I_Type> <I_TNSName>NAV12C2</I_TNSName> <I_Username>navdev</I_Username> <I_Password>navdev</I_Password> <I_SavePassword>0</I_SavePassword> </TNSlogon> <TNSlogon> <DataID>5</DataID> <I_Type>Directlogon</I_Type> <I_TNSName>RTTD</I_TNSName> <I_Username>devman</I_Username> <I_Password>devman</I_Password> <I_SavePassword>0</I_SavePassword> </TNSlogon> </TC_Testlogon>
2. 判断当前系统的MSXML.dll组件的版本(由于系统中一般会有多个版本,这里就只取最大的版本)
FunctioniGetMSxmlVersion() DimSystemPath,fso,f,fa,fname,iVersion,iMaxVersion SystemPath="C:\Windows\System32"'systempath Setfso=CreateObject("Scripting.FileSystemObject") Iffso.FolderExists(SystemPath)Then Setf=fso.GetFolder(SystemPath) Setfa=f.Files iMaxVersion=0 'calctheamountoffiles ForEachfnameinfa IfInstr(fname,"msxml")>0Then iVersion=Mid(fname.name,6,1) IfIsNumeric(iVersion)Then IfiMaxVersion<iVersionThen iMaxVersion=iVersion EndIf EndIf EndIf Next IfiMaxVersion>0Then iGetMSxmlVersion=iMaxVersion Log.Message("ThemaxMSxmlversionis:"+iMaxVersion) Else iGetMSxmlVersion=0 Log.Error("ThecurrentsystemnotexistMSxmlmodule.") EndIf EndIf EndFunction
FunctionsGetDataFromXML(sXmlPath,sRoot,sNode,iDataID,sColumn) DimxmlVersion xmlVersion=iGetMSxmlVersion IfsXmlPath<>""andsRoot<>""andsNode<>""andiDataID<>""andsColumn<>""Then IfxmlVersion<>0Then 'CreateCOMobject SetDoc=Sys.OleObject("Msxml2.DOMDocument."+xmlVersion+".0") 'youcanalsousethefollowingmethodtocreateXMLDOMobject 'SetDoc=CreateObject("Microsoft.XMLDOM") Doc.async=False 'Loaddatafromafile 'Weusethefilecreatedearlier CallDoc.load(sXmlPath) 'Reportanerror,if,forinstance,themarkuporfilestructureisinvalid IfDoc.parseError.errorCode<>0Then s="Reason:"+Chr(9)+Doc.parseError.reason+_ "Line:"+Chr(9)+CStr(Doc.parseError.line)+Chr(13)+Chr(10)+_ "Pos:"+Chr(9)+CStr(Doc.parseError.linePos)+Chr(13)+Chr(10)+_ "Source:"+Chr(9)+Doc.parseError.srcText 'Postanerrortothelogandexit CallLog.Error("Cannotparsethedocument.",s) ExitFunction EndIf 'UseanXPathexpressiontoobtainalistof"control"nodes SetNodes=Doc.selectNodes("/"+sRoot+"/"+sNode+"[DataID="+CStr(iDataID)+"]/"+sColumn) IfNot(Nodes.item(0)isNothing)Then CallLog.Message("DatafromXML:"+Nodes.item(0).text) sGetTestDataFromXML=Nodes.item(0).text Else Log.Error("ReadXMLnodesoccurerrors.") ExitFunction EndIf SetNodes=Nothing SetDoc=Nothing Else ExitFunction EndIf Else Log.Error("FunctionsGetTestDataFromXML()occurincorrectparameters.") ExitFunction EndIf EndFunction
4. TestComplete中测试脚本及log输出:
SubTest DimstrXmlPath,strTNSName,strUserame,strPassword strXmlPath=Project.Path+"Data\TC_TNSInfo.xml" log.Message(strXmlPath) IfaqFile.Exists(strXmlPath)Then strTNSName=sGetTestDataFromXML(strXmlPath,"TC_Testlogon","TNSlogon",3,"I_TNSName") strUserame=sGetTestDataFromXML(strXmlPath,"I_Username") strPassword=sGetTestDataFromXML(strXmlPath,"I_Password") log.Message("TNSNameis:"+strTNSName) log.Message("Usernameis:"+strUserame) log.Message("Passwordis:"+strPassword) EndIf EndSub原文链接:https://www.f2er.com/xml/297327.html