简体   繁体   中英

How to make object available globally in QTP

In the following code:

systemutil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"

WinEdit
WinButton


Public Function WinEdit
  Set objDialog=Description.Create
  objDialog("micclass").value="Dialog"

  Set objWinEdit=Description.Create
  objWinEdit("micclass").value="WinEdit"

  set WinEditCount=Dialog(objDialog).ChildObjects(objWinEdit)
  msgbox WinEditCount.count
End Function

Public Function WinButton
  Set objWinButton=Description.Create
  objWinButton("micclass").value="WinEdit"

  set WinButtonCount=Dialog(objDialog).ChildObjects(objWinButton)
  msgbox WinButtonCount.count   
End Function

The objects which are made in WinEdit function, are limited to that function only. I want to use the objects created in WinEdit function to be reused in WinButton function.

The whole point of functions is to encapsulate logic. If you declare a variable in a function it defaults to being private to this function, you can declare it globally but this is usually discouraged since it creates dependencies between the functions.

You can do something like this (which will initialize desc only once).

Foo
Bar

Dim desc ' global variable

Private Function InitDesc
    If IsEmpty(desc)  Then
        Set desc= Description.Create()
    End If
End Function

Public Function Foo
    InitDesc

End Function

Public Function Bar
    InitDesc
End Function

给对象变量指定一个唯一的名称,将其放在记事本文件中,然后将该文件与脚本关联

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM