Need to pass object and operation in a function that executes it

爷,独闯天下 提交于 2019-12-10 11:38:22

问题


I need to pass an object and its operation in a function so that each time I can call the function only and save me to write same steps for all the objects like validating the object before performing an operation. Similar way to a Register User Function in QTP/UFT.

However, Testcomplete doesn't have this feature (atleast under my knowledge, would be happy to know if there is)

This is my code that I am trying but unable to:

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

using if condition as i was passing object earlier instead of string

It fails at "execute" statement and gives me VbScript runtime error when executing this statement. my question is two fold -

  1. How do I pass objects and its operation in a function and execute it
  2. Also is it possible to pass an object it self instead of string for ex:

obtoolbar = "Aliases.Admin.wndMain.toolStrip"

Call OpenPageorTab(obtoolbar, ".Visible")

Appreciate any help or direction on this issue

EDIT 1

I am somewhere close to an answer however not accurately. I am able to pass the object as string - Check the code below

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

However I still need to pass the object something like

Set oToolStrip = Aliases.Admin.wndMain.toolStrip
Call OpenPageorTab(oToolStrip, ".Click")

This is something that I'm unable to do.

EDIT 2 I have already got the answer to this problem and have posted the solution. That being said, is there any way that Function can be utilized as a method ?


回答1:


Here an example of how to reference a function and pass parameteers to it, including objects.

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:


I was able to finally formulate the solution the below function can work as makeshift register function in 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


来源:https://stackoverflow.com/questions/48618251/need-to-pass-object-and-operation-in-a-function-that-executes-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!