Vbscript 需要在执行对象的函数中传递对象和操作
我需要在函数中传递一个对象及其操作,以便每次只能调用该函数并保存我为所有对象编写相同的步骤,例如在执行操作之前验证对象。类似于QTP/UFT中的寄存器用户函数 然而,Testcomplete没有这个特性(至少据我所知,我很乐意知道是否有) 这是我正在尝试但无法执行的代码:Vbscript 需要在执行对象的函数中传递对象和操作,vbscript,qtp,testcomplete,Vbscript,Qtp,Testcomplete,我需要在函数中传递一个对象及其操作,以便每次只能调用该函数并保存我为所有对象编写相同的步骤,例如在执行操作之前验证对象。类似于QTP/UFT中的寄存器用户函数 然而,Testcomplete没有这个特性(至少据我所知,我很乐意知道是否有) 这是我正在尝试但无法执行的代码: Call OpenPageorTab("Aliases.Admin.wndMain.toolStrip", ".Visible") Function OpenPageorTab(obj, method) 'if obj.E
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运行时错误。
我的问题有两方面:
obtoolbar=“alias.Admin.wndMain.toolStrip”
调用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
但是我仍然需要传递对象,比如
这是我无法做到的
编辑2
我已经得到了这个问题的答案,并发布了解决方案。也就是说,有什么方法可以将函数用作方法吗?这里是一个如何引用函数并向其传递参数(包括对象)的示例
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
我最终制定了解决方案,下面的函数可以作为TestComplete中的临时寄存器函数使用
感谢您的回答,不过我正在尝试将操作/方法与对象一起传递。如果我误解或遗漏了你的答案,你能告诉我这是否可以实现吗。因此,基本上在上面的示例中,我想将“my_obj”作为对象传递,“WriteLine”作为其方法Vbscipt没有Ruby那样的发送方法,但您可以像aCatinvolved建议的那样使用ExecuteGlobal、execute或eval,我修改了我的答案您使用my_obj作为字符串(“my_obj”)指挥-如果你想通过我的目标,你如何实现同样的目标-就像它是一样。谢谢你的帮助,彼得,基于你的回答,我一直在寻找解决方案。回答Posted欢迎您回答您的问题:我不是给myobj一个字符串,而是作为一个对象,只是为了调用函数本身传递的方法,您需要一个字符串由executeSee计算查看VBScript中的
ExecuteGlobal
或ExecuteGlobal
命令。它允许您在运行时将任何语句添加到当前代码中。子函数或函数之外的语句将立即执行。我已经在这里使用execute了。问题是我需要在函数中传递一个对象及其操作。使用字符串,但如何使用对象?使用对象和方法编写函数。executeglobal就是这么做的。你把它转换成编程文本。就好像你在写一样。对不起,我不明白你上面的评论是什么意思。你在问题中检查了我的编辑1吗?-当我以字符串形式传递对象时,它会工作,但如果我传递对象本身,它就会失败。对不起,我会说英语。我已经清楚了。使用全局execute并编写要执行的语句。不要费心回答。
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
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