转载地址:http://www.joecolantonio.com/2010/09/02/eight-performance-tips-to-help-optimize-your-qtp-code/

 

Why is QTP so slow?

Are your QTP scripts running slower than you’d like? Is your function library slowing you down? Try these ten performance tips to help optimize your QuickTest Professional code:

1. Dim your variables. Declared variables are faster than variables that are not declared. Once a variable is declared it is assigned an ordinal at runtime, but an undeclared variable is referenced by name every time it is used, which slows it down considerably.

2. Use Option Explicit at the beginning of all your scripts. Setting Option Explicit forces variable declaration. This is a good thing, as we saw in Tip #1. Also --Option Explicit has the added benefit of helping to debug, since it can point out any misspelled variable references.

3. For screen/object synchronization, use the Exist statement or Sync rather than the Wait statement. A Wait statement tells QTP to pause for the full amount of time you’ve entered. Wait(10) will pause for exactly ten seconds -- even if the object you are waiting for appears sooner. But an Exist(10) will only wait up to ten seconds. That means that if the object appears in two seconds, the Exist will continue after only two seconds rather then wait the full ten.

4. If you are using functions with keywords, place them in order of common usage rather than alphabetically.

5. Use local variables rather than global variables for functions. Local variables are faster than global variables, so use local whenever possible.

6. Set QuickTest's 'Run Mode' to Fast. To set in QTP, go to Options\Run. This setting changes the script’s run behavior so that it will run without the execution arrow to the left of the Expert view (or Keyword view). The test will also not expand the item tree, nor display the script of each action as it runs. Also, this setting requires fewer system resources. Also if you don't care about capturing images in the result --on an error-- you could un-check 'Save still image captures to results' under Run\Screen Capture.  Same thing for the 'Save movie to results ' setting.

7. Use the Object Repository (OR). Scripts run faster if all the objects are in the QTP OR as opposed to using straight descriptive programming. However, I’ve also seen extremely large object repositories create performance issues; especially in Business Process Tests. What I prefer to do is use a mix of both. In my applications, all objects live under one of the following two main paths:

Browser("CF").Page("CF").Frame("WorkSpace").SwfObject("IEController")
or:
Browser("CF").Page("CF").Frame("WorkSpace").SwfObject("IEController").ActiveX("ICW")

So -- I only have the bare essential objects in my OR. For everything else, I use a combination of the path that is in the OR and a descriptive for my objects. For example, I have a function for all of the different object types -- textbox, Combobox, Buttons, etc. -- my application contains. In each object function, I use a Set Statement to assign an object reference to the OR path. I then pass it a descriptive definition to enable it to recognize the specific object. A simple example would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Public Function TextBox (swfName, action)
 
Dim mainPath
 
' This is the path to the OR
Set mainPath =   Browser("CF").Page("CF").Frame("WorkSpace").SwfObject("IEController")
  
Select Case UCASE(action)
   Case "CLICK"
   ' This is the OR path + descriptive programming
    mainPath.SwfEdit("swfname:="&swfName,"index:=0").Click
End Select  
  
End Function

8. If your test has a dynamic object that sometimes appears use the ChildObjects method to check if it exists rather than use the Exist statement.

So, rather than this:

Set strDlgPath = Browser("CF").SwfWindow("swfname path:=ModalForm")
If strDlgPath.Exist(5) Then
strDialogExistFlag = True
strBrowserDialogExistFlag = True
Else
DialogAW = "False"
Exit Function
End If

I would do something like this:

Set strDlgPath = Browser("CF")
Set dlgObjDesc = Description.Create()
dlgObjDesc("swfname path").Value = "ModalForm"
set nowDialog = strDlgPath.ChildObjects(dlgObjDesc)
objFoundCount = nowDialog.count
if objFoundCount = 0 then ' Object Exist
DialogAW = "False"
Exit Function
else
strDialogExistFlag = True
strBrowserDialogExistFlag = True
end If

9. If you are creating objects make sure you destroy them after using them.  In long scripts you don't want to have unnecessary object still in memory.  This can cause QTP to slow down. To eliminate a variable reference to its object set it to Nothing:

Set myObject = Nothing

10. Turn off QTP's 'Active Screen' option.  If you are recording against an application and it appears that either QTP or the APP (or both) are slow or appear to hang disable Active screen. This behavior can appear if you are recording against a screen that has a large number of objects or have deep object hierarchies.

To disable go to Tools\Options\Active Screen and set the Capture Level to "None".

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3