需要在执行它的函数中传递对象和操作

时间:2018-02-05 08:17:16

标签: vbscript qtp testcomplete

我需要在函数中传递一个对象及其操作,这样每次我只能调用该函数并保存我为所有对象编写相同的步骤,比如在执行操作之前验证对象。与QTP / UFT中的寄存器用户功能类似。

然而,Testcomplete没有此功能(至少据我所知,会很高兴知道是否有)

这是我正在尝试但无法执行的代码:

Call OpenPageorTab("Aliases.Admin.wndMain.toolStrip", ".Visible")

Function OpenPageorTab(obj, method)

'if obj.Exists then
  execute a = obj&method
  delay(1000)
  OpenPageorTab = True
'Else
  log.Error "Unable to find the object"
  OpenPageorTab = False
'End if

使用if条件,因为我之前传递的是对象而不是字符串

它在“execute”语句失败,并在执行此语句时给出了VbScript运行时错误。 我的问题是双重的 -

  1. 如何在函数中传递对象及其操作并执行它
  2. 也可以传递一个对象而不是字符串,例如:
  3. obtoolbar = "Aliases.Admin.wndMain.toolStrip"

    Call OpenPageorTab(obtoolbar, ".Visible")

    感谢此问题的任何帮助或指示

    编辑1

    我接近答案,但不准确。我能够将对象作为字符串传递 - 检查下面的代码

    Call OpenPageorTab("Aliases.Admin.wndMain.toolStrip", ".Click")
    
    
    Function OpenPageorTab(obj, method)
    
    ' if obj.Exists then
        eobj = "" & obj & method
        execute (eobj)
        delay(1000)
        OpenPageorTab = True
    ' Else
        log.Error "Unable to find the object"
        OpenPageorTab = False
    ' End if
    
    End Function
    

    但是我仍然需要传递像

    这样的对象
    Set oToolStrip = Aliases.Admin.wndMain.toolStrip
    Call OpenPageorTab(oToolStrip, ".Click")
    

    这是我无法做到的事情。

    编辑2 我已经得到了这个问题的答案,并已发布解决方案。话虽如此,有没有什么方法可以将函数用作方法?

2 个答案:

答案 0 :(得分:1)

这是一个如何引用函数并将参数传递给它的示例,包括对象。

Const forReading = 1, forWriting = 2, forAppending = 8, CreateFile = True
Set my_obj = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\temp\test.txt", forWriting, CreateFile)

Function my_function(my_obj, method, text)
  command = "my_obj." & method & " """ & text & """"
  ExecuteGlobal command
End Function

'make a reference to our function
Set proc = GetRef("my_function") 
'and call it with parameters, the first being the method invoked
Call proc(my_obj, "WriteLine", "testing")

'cleanup'
my_obj.Close
Set my_obj = Nothing

答案 1 :(得分:0)

我能够最终制定解决方案,以下功能可以作为TestComplete中的临时寄存器功能

Sub test

'Set the Object
Set pToolStrip = Aliases.Admin.wndMain.toolStrip.Button("User Admin")
Call GenericOperationFunc(pToolStrip, ".Click", "N")

'if you want to perform an operation which return a value
b = GenericOperationFunc(Aliases.Admin.wndPopup.Child(2), ".Caption", "Y")

End Sub

Public Function GenericOperationFunc(obj, method, Return)
   GenericOperationFunc = False
   on error resume next
   if obj.Exists then
      if Ret = "Y" then
          eobj = "NewText="&"obj" & method
          execute (eobj)
          GenericOperationFunc = NewText 
          Delay(500)
      Else
          eobj = "obj" & method
          execute (eobj)
          delay(1000)       
          GenericOperationFunc = True
      End if
  Else
      log.Error "Unable to find the object"
      GenericOperationFunc = False
  End if

End Function

'log.error, delay, aliases, ptoolstrip(object) are testcomplete specific