QTP环境变量的动态生成与加载(深入篇)

当然了,除了上一篇所讲的操作,对这三种文件的操作还包括了增删改查。幸运的是,txt格式跟ini格式因为保存的文件的样式是一样的,所以可以用相同的函数去操作,具体如下:

 

'读取txt格式环境变量文件里的所有值,存储到Dictionary对象里

Public Function ReadTxtIniEnvVars(fileName)

    Dim txtStream,txtStr,txtArray,txtDic,i

         Set txtStream = syFSO.OpenTextFile(fileName)

         Set txtDic = CreateObject("Scripting.Dictionary")

         txtStr = txtStream.ReadAll

         txtArray = Split(txtStr,vbcrlf)

         For i=1 to UBound(txtArray) -1

                    If Trim(Split(txtArray(i),"=")(0) )<>Empty And Trim(Split(txtArray(i),"=")(0) )<>"" Then

                              txtDic.Add Trim(Split(txtArray(i),"=")(0) ),Trim(Split(txtArray(i),"=")(1) )

                    End If

         Next

         Set ReadTxtIniEnvVars = txtDic

         Set txtDic = nothing

End Function

 

'更新txt文件里指定的环境变量值

'isRemove = true执行删除操作。否则执行更新

Public Sub UpdateTxtIniEnvVars(fileName,varName,varValue,isRemove)

    Dim txtStream,txtStr,temp

         Set txtStream = syFSO.OpenTextFile(fileName,1)

         Do Until txtStream.AtEndOfLine

                   txtStr = txtStream.ReadLine

                   If Trim(Split(txtStr,"=")(0))=varName Then

                            If isRemove Then

                                     txtStr = ""

                            Else

                                     txtStr = varName & " = " & varValue

                            End If

                   End If

                   If txtStr<>"" Then

                            temp = temp + txtStr + vbcrlf

                   End If

         Loop

         txtStream.Close

         Set txtStream = syFSO.OpenTextFile(fileName,2)

         txtStream.Write temp

         txtStream.Close

         Set txtStr = nothing

End Sub

 

'增加环境变量到已经存在的txt配置文件里,加在原文件的末尾

Public Sub AddTxtIniEnvVars(fileName,varName,varValue)

         Dim txtStream

         Set txtStream = syFSO.OpenTextFile(fileName,8)

         txtStream.WriteLine varName + " = " +varValue

         txtStream.Close

End Sub

 

'在txt文件里删除一个已经存在的环境变量

Public Sub DelTxtIniEnvVars(fileName,varName)

   UpdateTxtIniEnvVars fileName,varName,"",True

End Sub

 

华丽丽的分割线:————————————————————

下面的是关于xml文件里的环境变量的增删改查操作:

 

'读取xml文件里所有环境变量,返回一个DIctionary对象

Public Function ReadXmlEnvVars(fileName)

   Dim xmlObj,xmlDic

   Dim xmlRoot,xmlNode

   Dim varName,varValue

   Set xmlObj = CreateObject("MSXML.DOMDocument")

   Set xmlDic = CreateObject("Scripting.Dictionary")

   xmlObj.Load fileName

   Set xmlRoot = xmlObj.LastChild

   For i = 0 to xmlRoot.ChildNodes.Length - 1

                   Set xmlNode = xmlRoot.ChildNodes.Item(i)

                   varName = xmlNode.ChildNodes.Item(0).nodeTypedValue

                   varValue = xmlNode.ChildNodes.Item(1).nodeTypedValue

                   xmlDic.Add varName,varValue

   Next

   Set ReadXmlEnvVars = xmlDic

   Set xmlObj = nothing

   Set xmlDic = nothing

   Set xmlRoot = nothing

   Set xmlNode = nothing

End Function

 

'更新xml格式环境变量文件

Public Sub UpdateXmlEnvVars(fileName,varName,varValue)

   Dim xmlObj

   Dim xmlRoot,xmlNode

   Dim nodeName,nodeValue

   Set xmlObj = CreateObject("MSXML.DOMDocument")

   xmlObj.Load fileName

   Set xmlRoot = xmlObj.LastChild

   For i = 0 to xmlRoot.ChildNodes.Length - 1

                   Set xmlNode = xmlRoot.ChildNodes.Item(i)

                   nodeName = xmlNode.ChildNodes.Item(0).nodeTypedValue

                   'nodeValue = xmlNode.ChildNodes.Item(1).nodeTypedValue

                   If UCase(Trim(nodeName)) = UCase(Trim(varName)) Then

                            xmlNode.ChildNodes.Item(1).nodeTypedValue = varValue

                   End If

   Next

   xmlObj.Save fileName

   Set xmlObj = nothing

   Set xmlRoot = nothing

   Set xmlNode = nothing

End Sub

 

'删除xml格式环境变量文件里的一个节点

Public Sub DelXmlEnvVars(fileName,varName)

   Dim xmlObj

   Dim xmlRoot,xmlNode

   Dim nodeName,nodeValue

   Set xmlObj = CreateObject("MSXML.DOMDocument")

   xmlObj.Load fileName

   Set xmlRoot = xmlObj.LastChild

   If xmlRoot.ChildNodes.Length = 1 Then

            Exit Sub

   End If

   For i = 0 to xmlRoot.ChildNodes.Length - 1

                   Set xmlNode = xmlRoot.ChildNodes.Item(i)

                   nodeName = xmlNode.ChildNodes.Item(0).nodeTypedValue

                   'nodeValue = xmlNode.ChildNodes.Item(1).nodeTypedValue

                   If UCase(Trim(nodeName)) = UCase(Trim(varName)) Then

                            xmlRoot.RemoveChild(xmlNode)

                            Exit For

                   End If

   Next

   xmlObj.Save fileName

   Set xmlObj = nothing

   Set xmlRoot = nothing

   Set xmlNode = nothing

End Sub

 

'增加一个新的节点到xml格式的环境变量

'可以增加判断,如果varName重复,则调用UpdateXmlEnvVars()

Public Sub AddXmlEnvVars(fileName,varName,varValue)

   Dim xmlObj

   Dim xmlRoot,xmlNode

   Dim nodeName,nodeValue

   Dim index

   Set xmlObj = CreateObject("MSXML.DOMDocument")

   xmlObj.Load fileName

   Set xmlRoot = xmlObj.LastChild

   Set xmlNode = xmlRoot.ChildNodes.Item(0).CloneNode(1)

   xmlNode.ChildNodes.Item(0).nodeTypedValue = varName

   xmlNode.ChildNodes.Item(1).nodeTypedValue = varValue

   xmlRoot.AppendChild(xmlNode)

   xmlObj.Save fileName

   Set xmlObj = nothing

   Set xmlRoot = nothing

   Set xmlNode = nothing

End Sub

以上这些就是我最近总结的一套关于QTP三种格式的环境变量的动态存储,当然有兴趣的童鞋可以将它封装起来。欢迎关注我的博客:http://www.cnblogs.com/ryansunyu/

posted @ 2012-08-20 22:55  平静缓和用胸音说爱  阅读(667)  评论(0编辑  收藏  举报