雖然現今數據大部分都是使用數據庫存儲,但是停電斷網等不可抗拒因素經常中斷著automation的進行,這時若這些非業務的配置數據用xml來存儲就顯得方便很多了,下面是用VBScript腳本讀取XML文件的實現方法,記錄下以作備忘。
1. XML文件的節點信息:
<?xml version="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組件的版本(由于系統中一般會有多個版本,這里就只取最大的版本)
Function iGetMSxmlVersion()
Dim SystemPath, fso, f, fa, fname, iVersion, iMaxVersion
SystemPath = "C:\Windows\System32" 'system path
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(SystemPath) Then
Set f = fso.GetFolder(SystemPath)
Set fa = f.Files
iMaxVersion = 0
' calc the amount of files
For Each fname in fa
If Instr(fname,"msxml")>0 Then
iVersion = Mid(fname.name,6,1)
If IsNumeric(iVersion) Then
If iMaxVersion < iVersion Then
iMaxVersion = iVersion
End If
End If
End If
Next
If iMaxVersion >0 Then
iGetMSxmlVersion = iMaxVersion
Log.Message("The max MSxml version is: " + iMaxVersion)
Else
iGetMSxmlVersion = 0
Log.Error("The current system not exist MSxml module.")
End If
End If
End Function3. 讀取xml文件函數:
Function sGetDataFromXML(sXmlPath, sRoot, sNode, iDataID, sColumn)
Dim xmlVersion
xmlVersion = iGetMSxmlVersion
If sXmlPath <> "" and sRoot <> "" and sNode <> "" and iDataID <>"" and sColumn <> "" Then
If xmlVersion <> 0 Then
' Create COM object
Set Doc = Sys.OleObject("Msxml2.DOMDocument."+ xmlVersion +".0")
' you can also use the following method to create XMLDOM object
'Set Doc = CreateObject("Microsoft.XMLDOM")
Doc.async = False
' Load data from a file
' We use the file created earlier
Call Doc.load(sXmlPath)
' Report an error, if, for instance, the markup or file structure is invalid
If Doc.parseError.errorCode <> 0 Then
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
' Post an error to the log and exit
Call Log.Error("Cannot parse the document.", s)
Exit Function
End If
' Use an XPath expression to obtain a list of "control" nodes
Set Nodes = Doc.selectNodes("/" + sRoot + "/" + sNode + "[DataID=" + CStr(iDataID) + "]/" + sColumn)
If Not(Nodes.item(0) is Nothing) Then
Call Log.Message("Data from XML: " + Nodes.item(0).text)
sGetTestDataFromXML = Nodes.item(0).text
Else
Log.Error("Read XML nodes occur errors.")
Exit Function
End If
Set Nodes = Nothing
Set Doc = Nothing
Else
Exit Function
End If
Else
Log.Error("Function sGetTestDataFromXML() occur incorrect parameters.")
Exit Function
End If
End Function4. TestComplete中測試腳本及log輸出:
Sub Test
Dim strXmlPath, strTNSName, strUserame, strPassword
strXmlPath=Project.Path + "Data\TC_TNSInfo.xml"
log.Message(strXmlPath)
If aqFile.Exists(strXmlPath) Then
strTNSName = sGetTestDataFromXML(strXmlPath, "TC_TestLogon", "TNSLogon", 3, "I_TNSName")
strUserame = sGetTestDataFromXML(strXmlPath, "TC_TestLogon", "TNSLogon", 3, "I_Username")
strPassword = sGetTestDataFromXML(strXmlPath, "TC_TestLogon", "TNSLogon", 3, "I_Password")
log.Message("TNSName is :"+strTNSName)
log.Message("Username is :"+strUserame)
log.Message("Password is :"+strPassword)
End If
End Sub
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。