QTP 6-9 QTP基础2 Environment Variables &Utility Objects & Checkpoints& Library Files

来源:互联网 发布:python 读入整个文件 编辑:程序博客网 时间:2024/05/01 08:23

6. (QTP Environment Variables) 环境变量

项目经验:把常用的全局变量保存为单独文件,再引入为环境变量。通常是随便加个环境变量,export出来成为XML文件,再编辑XML文件,最好再导入进来。

1.     Concept:

QTP environment variables arepersist across the test case(Actions, libraries, recovery scenarios)

2.     Types of environment variables: Built-in(systemsdefinition) ; User-defined internal; User-defined external

3.     Environment Object

*ExternalFileName Property: Return the name of the external environment variables file

*LoadFromFile Method: load the environment variables file

* Value Property: Get the value of environment variables

 

4.     How to access an environment variable?

sName = Environment("Name")

               sName = Environment.Value("Name")

5.     How to create an environment variable atrun-time?

Environment("Name") =“runTime”

6.     How to check if an environment variable exists?

Reference: check if dataTable sheet exists:

'E.g. Function to check if dataTable sheet exsits

Function isSheetExists (sheetName)

   On error resume next

               isSheetExists = True

               Err.clear

               Set objSheet = DataTable.GetSheet(sheetName)

 

               If err.number <>0 Then

                              isSheetExists = False

               End If

     On Error Goto 0

                 End Function

7.     How an object is assigned to the environmentvariable

Method 1:

 

Environment("BrowserObj") = Browser("CreationTime:=0")

               Environment("BrowserObj").close

 

Method 2:

Environment("BrowserObj") = "Browser(""CreationTime:=0"")"

Dim objBrowser

Execute "set objBrowser = " &  Environment("BrowserObj")

              objBrowser.close

 

Method 3:

Environment("BrowserObj") = Browser("CreationTime:=0")

Dim objBrowser

set objBrowser = Environment("BrowserObj")

               objBrowser.close

8.     How to load external environment variable XMLfile?

Environment.LoadFromFile "c:\Env_Var.xml"

 

9.     Tips:

9.1  The environment variable names are Case Sensitive.

9.2 The environment variable can saved inthe “Library Files” as well.

9.3 The book also mentioned “an array can assignedto the environment variables”

 

7.(Utility Objects) 实用工具对象

This chapter list below utility objects to enhancethe scripting:

Crypt, DataTable, Description, Environment, Extern, OptionalStep, Parameter, PathFinder, Properties, QCUtil, RandomNumber, Revovery, Reporter, Services, Setting, TextUtil, TSLTest, XMLUtil

 

7.1 The Crypt Object

This always to set the password in thesecurity way, to achieved this:

securePWD = Crypt.Encrypt ("password")

WebEdit(“webEdit”).SetSecure securePWD

You can create an instance of Crypt Object:

Set myCrypt = CreateObject("Mercury.Encrypter")

 

7.2 The OptionalStep Object

               Thisobject is used to make a statement optional (e.g. a security warning may appearand may not)

               Ifsecurity warning not appear, the TC not failed.

OptionalStep.Browser("Browser").Page("Page").WinButton("Yes").Click

 

7.3 The PathFinder Object

               Thisobject can be used to find the absolute path to a file. (QTP also allow settingfolder paths in the ToolsàOptionsàFolders(Tab))

'assign "C:\RelativeFolder\Test.xls" to x

x = PathFinder.Locate("Test.xls")

 

7.4 The RandomNumber Object

               Thisobject provides a method to get a random number.

               e.g. togenerate a number between 1 – 100:

x= RandomNumber.Value(1,100)

x = RandomNumber (1,100)

 

7.5 The Setting Object             

               Theobject provides the following methods or properties:

*Add Method:

Setting.Add "LogErrors", "Yes"

*Exists Method:

Setting.Exists "LogErrors"

*Item Method:

Setting.Item "LogErrors"

*Remove Method:

Setting.Remove "LogErrors"

 

7.6 The RegisterUserFunc Object      

This object is used to override existing functions.

e.g you want to override “Set” Function by your “newSet “Function:

Function NewSet(object, newValue)

   object.Set Ucase(newValue)

End Function

RegisterUserFunc "WinEdit", "Set", "NewSet"

Browser("Browser").Page("Page").WinEdit("WinEdit").Set "value"

‘QTP will enter upper case “VALUE” in the edit.

 

8.(Checkpoints) 检查点

Concept: to verify a runtime value against an expectedvalue.

Note: 检查点是很多东西的集合,很不灵活,除非被测系统的检查点简单,否则不推荐使用。可以使用其他的代替方案:Browser("").CheckProperty&  Browser("").GetROProperty= “Expected value”; 像DateBaseCheckpoint 可以用ADODB来模拟。             

1.     7 types of checkpoints:

*Standard checkpoints: verify the properties of standard objects

*Database checkpoints: validate the contents of a DB

*Bitmap checkpoints: compare bitmap image from pixel by pixel

*XML checkpoints: validate XML document contents

*Text checkpoints: validate a text string is presented at expected place

*Textarea checkpoints: validate a text string is presented at expected area

*Accessibility checkpoints: check application if comply W3C accessibility standards

 

2.     How to use the checkpoints:

Browser("").Page("").Link("test").Check CheckPoint("TestLink")

Or

              cpStatus = Browser("").Page("").Link("test").Check CheckPoint("TestLink")

 

Note: How to avoid the steps not reportedin the test result file?

Reporter.Filter  = rfDisableAll

 

'code

 

               Reporter.Filter = rfEnableAll

 

3.     Checkpoints Properties:

*Standard:

      -micclass

      -step_type

      -step_timeout

*Text

       -micclass

      -step_type

      -step_timeout  

      -Text

      -Text Before

      -Text After

      -text not found

 

9.(Library Files) 库文件

Concept: Library files contain VBS code, with extension VBSor TXT.

1.     Two ways to load the library file:

*Associate as resource

FileàSetting àResources(TAB)àlibraries

*Load libraries using code at run-time

ExecuteFile "C:\library.vbs"

Note:如果Action循环很多遍,就要注意reload很多遍的问题。要写个Function(LoadFlag)来判断library是否已经加载。

 

2.     可以把全局变量放到Library files, 这样所有的Action都可以用,这个变量可以传递。

C:\test.vbs           Dim x

Action1                   x=2

Action2                   x=x+2

                               Print x          ‘x=4

 

3.     Dynamically loading libraries locally & globally:

Locally: the library only visible withinthe Action in which the ExecuteFile is used.(参考1*Load libraries using code atrun-time)

Globally: 把功能写进在global范围的libraries中,或者在global范围的libraries写一个含有ExecuteFile的function,这样调用这个function而加载的其他libraries是适用于这个testcase 的。

4.     Library files 的优先级:

4.1  这个TC优先级:先加载 associated Recovery scenario’sLibrary files,再加载globally 的Libraryfiles(FileàSetting àResources(TAB)àlibraries),最后加载运行到的eachaction 的Libraryfiles.

4.2  Globally 的Library files(FileàSetting àResources (TAB)àlibraries)的优先级:从下往上—Thesefiles are loaded from bottom to the top.

Note1: 所以两个libraries含同样命名的function,调用靠上library文件(FileàSetting àResources (TAB)àlibraries)的function。

Note2:  Option Explicit: OptionExplicit cannot force individual libraries in a global scope. So put thelibrary file with “Option Explicit” in Recovery VBS orbottom most in the (FileàSettingàResources (TAB)àlibraries)

5.     利用libraries 提高自动化项目的维护性:

If no of scripts need modify at the startor end of an action. You can call two special functions (GetActionStart();GetActionEnd()) in the library, then only edit the functions.

 

Function GetActionStart()

               GetActionStart = ""

               sAction = Lcase(Environment("ActionName"))

               If Instr(sAction, "Main") Then

                              GetActionStart = "ExecuteFile PathFinder.Locate(""library.vbs"")"

               End If

              End Function