需要在执行它的函数中传递对象和操作 [英] Need to pass object and operation in a function that executes it
问题描述
我需要在函数中传递一个对象及其操作,以便每次我只能调用该函数,并节省我为所有对象编写相同的步骤,例如在执行操作之前验证对象.类似于 QTP/UFT 中的注册用户功能.
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.
然而,Testcomplete 没有这个功能(至少据我所知,如果有,我很乐意知道)
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
使用 if 条件,因为我之前传递的是对象而不是字符串
它在执行"语句中失败,并在执行此语句时给我 VbScript 运行时错误.我的问题有两个 -
It fails at "execute" statement and gives me VbScript runtime error when executing this statement. my question is two fold -
- 如何在函数中传递对象及其操作并执行它
- 还可以将对象本身而不是字符串传递给 ex:
obtoolbar = "Aliases.Admin.wndMain.toolStrip"
调用 OpenPageorTab(obtoolbar, ".Visible")
感谢有关此问题的任何帮助或指导
Appreciate any help or direction on this issue
编辑 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
但是我仍然需要传递诸如
Set oToolStrip = Aliases.Admin.wndMain.toolStrip
Call OpenPageorTab(oToolStrip, ".Click")
这是我无法做到的.
编辑 2我已经得到了这个问题的答案并发布了解决方案.话虽如此,有什么方法可以将 Function 用作方法吗?
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 ?
推荐答案
这是一个如何引用函数并向其传递参数(包括对象)的示例.
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
这篇关于需要在执行它的函数中传递对象和操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!