you never doubt youself,I belive!

02~ Environment Variables 学习笔记 -《QuickTest Professional Unggled》

上一篇 / 下一篇  2009-12-13 17:16:04 / 个人分类:QTP

1. How to access an enironment variable?
'Get the value to Environment variable TestName
sTestName = Environment.Value ("TestName")

'Since .Value is the default property of the Environment
'object, we can also use shorthand as follows:

sTestName = Environment ("TestName")

2. How can we check if an environment variable exists?
'Function to check if an environment variable exist
Public Function IsEnvExist(ByVal varName)
    IsEnvExist = True

   'In case of error resume execution of next statement
    On Error Resume Next

    Dim envVal
    envVal = Environment(varName)

   'Check if any error occured
    If err.number<>0 Then
       'If error occured assume the variable does not exist
        IsEnvExist = False
    End If

   'Enable the error popups
    On Error Goto 0
End Function
Usage:
'Check if "Invalid" environment variable exist
MsgBox IsEnvExist("Invalid")

 

3. How is an environment variable defined at run time?
'Check if "Invalid" variable exists in QTP environment
MsgBox IsEnvExist("Invalid") 'this returns False

'Create and set the environment variable ar run-time
Environment("Invalid") = "Not invalid any more"

'Check if "Invalid" variable exists in QTP environment
MsgBox IsEnvExist("Invalid") 'this now returns True

4. How can environment variables be exported to an XML file?
<Environment>
       <Variable>
              <Name>FirstName</Name>
              <Value>Tarun</Value>
       </Variable>
       <Variable>
              <Name>LastName</Name>
              <Value>Lalwani</Value>
       </Variable>
</Environment>
And here are the QTP statements to generate that XML code in an external file:
'Function to get XML tags for a variable with name and value
Public Function GetVarXML(varName,varValue)
 GetVarXML ="<Variable>" & vbCrLf & _
     "<Name>" & varName & "</Name>" & vbCrLf & _
     "<Value>" & varValue & "</Value>" & vbCrLf & _
     "</Variable>" & vbCrLf
End Function

'Function to write the file to a string
Public Sub WriteStringToFile(fileName, varXML)
 Dim fso, file
 Set fso = CreateObject ("Scripting.FileSystemObject")

 'Create the file
 Set file = fso.CreateTextFile (fileName,True)

 'write the text
 file.WriteLine(varXML)
 file.close

 Set file = nothing
 Set fso = nothing
End Sub


Dim strXML

'Create the envrionment XML start
strXML = "<Environment>"

'nodes for variables
strXML = strXML & GetVarXML("FirstName","Tarun")
strXML = strXML & GetVarXML("LastName","Lalwani")

'Create the envrionment XML end
strXML = strXML & "</Environment>" & vbCrLf

'Create the XML file from the string
WriteStringToFile "C:\Testing.xml",strXML

'Load the XML to test it
Environment.LoadFromFile "C:\Testing.xml"

'Read the variables
Msgbox Environment("LastName") & ", " & Environment("FirstName")

 

5. How an array is passed using Environment variables?
'Create a fixed array
Dim fixedArr(3)

For i = LBound(fixedArr,1) to UBound(fixedArr,1)
 fixedArr(i) = Cstr(i)
Next

Dim dynArr

'Assign it to another variable to make it dynamic
dynArr = fixedArr

'Assign the dynamic array to the environment variable
Environment.Value("PassArray") = dynArr

MsgBox Environment.Value("PassArray")(2)

'Get the array from the environment variable
retArr = Environment.Value("PassArray")
Msgbox retArr(1)

6. How an object is passed using an environment variable?

Method1
'String form. of the object we need
Environment("BrowserObj") = "Browser(""creationtime:=0")"

Dim objBrowser

'Execute the code Set bjBrowser=Browser("Browser")
Execute "set bjBrowser = " &Environment("BrowserObj")

objBrowser.close

Method2

'String form. of the object we need
Environment("BrowserObj") = Browser("creationtime:=0")

Dim objBrowser

Set bjBrowser = Environment("BrowserObj")

objBrowser.close

7. How are environment variables loaded from an external XML file?

'Variables in Env_Dev.xml are now available
Environment.LoadFromFile "C:\Env_Dev.xml"


Remark function

1.Cstr(expression):Returns an expression that has been converted to a Variant of subtype String.

2.LBound(arrayname [,dimension]): Returns the smallest available subscript. for the indicated dimension of an array.
 UBound(arrayname [,dimension]):Using the same
* Remark:① The LBound function is used with the UBound function to determine the size of an array
           ② Use the UBound function to find the upper limit of an array dimension.
              eg. Dim A(100,5,8)
                  UBound(A,2) 'return 5
3.Environment.LoadFromFile "filepath"  'external export


TAG:

 

评分:0

我来说两句

Open Toolbar