代码之家  ›  专栏  ›  技术社区  ›  Pranav

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

  •  1
  • Pranav  · 技术社区  · 7 年前

    我需要在函数中传递一个对象及其操作,以便每次只能调用该函数并保存我为所有对象编写相同的步骤,例如在执行操作之前验证对象。类似于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. 也可以将对象传递给它自己,而不是传递给ex的字符串:

    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 回复  |  直到 7 年前
        1
  •  1
  •   peter    7 年前

    这里是一个如何引用函数并向其传递参数(包括对象)的示例。

    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
    
        2
  •  0
  •   Pranav    7 年前

    我最终制定了解决方案,下面的函数可以作为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