vbscriptqtptestcomplete

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


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 ?


Solution

  • 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