It is necessary to pass an object and an operation to a function that performs it

I need to pass an object and its work into a function, so that every time I can only call this function and save me to record the same steps for all objects, such as checking the object before performing the operation. Similar to user registration function in QTP / UFT.

However, Testcomplete does not have this feature (at least, as far as I know, I would be glad to know if there is any)

This is my code that I am trying but cannot:

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 the if condition when I used to pass an object instead of a string

It does not work when executing the execute statement and gives me a VbScript runtime error while this statement is executing. my question is double -

  • How to pass objects and their work into functions and execute it
  • It is also possible to pass the object it self instead of the string for ex:

obtoolbar = "Aliases.Admin.wndMain.toolStrip"

Call OpenPageorTab(obtoolbar, ".Visible")

Appreciate any help or direction on this subject

EDIT 1

I’m somewhere close to the answer, but not for sure. I can pass the object as a 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 what I cannot do.

EDIT 2 I already received an answer to this problem and posted a solution. As the saying goes, is there a way that a function can be used as a method?

+1
source share
2 answers

Here is an example of how to refer to a function and pass parameters 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 
+1
source

I was able to finally formulate a solution, the lower function can work as a temporary 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 
0
source

Source: https://habr.com/ru/post/1312116/


All Articles