aaa*_*ine 3 vbscript function-pointers hp-uft
我正在尝试按照程序员的方式将HP Unified Functional Testing中的测试放在一起.对于那些不知道的人,该工具使用VBScript作为其驱动程序.
因为我想在多个UFT操作中使用来自相同DataTable的数据 - 并且因为Global表上已经有一组不同的数据 - 我想从外部文件中检索数据.UFT很乐意支持这个功能.
我目前的计划是,根据我正在运行的测试,我将遍历该表中的一系列行.
这是我提出的脚本:
' targets the local sheet, but
' not the same value as dtLocalSheet
Const sheetNum = 2
dim sheetRowCount
DataTable.ImportSheet "PersonFile.xlsx", 1, sheetNum
sheetRowCount = DataTable.GetSheet(sheetNum).GetRowCount
dim firstRow, lastRow
firstRow = Parameter("FirstPersonIndex")
lastRow = Parameter("LastPersonIndex")
If sheetRowCount < lastRow Then
lastRow = sheetRowCount
End If
If sheetRowCount >= firstRow Then
Dim i
For i = firstRow To lastRow
DataTable.SetCurrentRow i
' begin payload
MsgBox(DataTable.Value("LastName", dtLocalSheet))
' end payload
Next
End if
Run Code Online (Sandbox Code Playgroud)
每次我想使用这种模式时,我都不想重复所有这些样板.我真的很喜欢这样的东西:
在函数库中:
sub LoopThroughSheetAnd(sheetFile, doThis)
' targets the local sheet, but
' not the same value as dtLocalSheet
Const sheetNum = 2
dim sheetRowCount
DataTable.ImportSheet sheetFile, 1, sheetNum
sheetRowCount = DataTable.GetSheet(sheetNum).GetRowCount
dim firstRow, lastRow
firstRow = Parameter("FirstRow")
lastRow = Parameter("LastRow")
If sheetRowCount < lastRow Then
lastRow = sheetRowCount
End If
If sheetRowCount >= firstRow Then
Dim i
For i = firstRow To lastRow
DataTable.SetCurrentRow i
call doThis()
Next
End if
end sub
Run Code Online (Sandbox Code Playgroud)
在原来的行动......
sub Payload1()
MsgBox(DataTable.Value("LastName", dtLocalSheet))
end sub
LoopThroughSheetAnd "PersonFile.xlsx", Payload1
Run Code Online (Sandbox Code Playgroud)
在一个单独的行动中,3或4步后......
sub Payload2()
' compare the data against another data source
end sub
LoopThroughSheetAnd "PersonFile.xlsx", Payload2
Run Code Online (Sandbox Code Playgroud)
上面的代码在VBScript中不起作用.一旦我们尝试Payload1作为参数传递,就会抛出类型不匹配错误.
怎么可以合理地在VBScript中解决这个问题?如果答案也适用于UFT,则奖励积分.
您可以使用该GetRef()函数将函数作为参数传递.这是一个实用程序map函数,就像你在JavaScript中发现的那样接受一个数组并为数组的每个元素调用一个函数:
Sub Map(a, f)
Dim i
For i = 0 To UBound(a)
' Call a function on each element and replace its value with the function return value
a(i) = f(a(i))
Next
End Sub
Map MyArray, GetRef("SomeFunc")
Run Code Online (Sandbox Code Playgroud)
现在您可以编写SomeFunc它以便对值进行操作并返回更新的值:
Function SomeFunc(i)
SomeFunc = i + 1
End Function
Run Code Online (Sandbox Code Playgroud)
这很好用.使用我们传递给它的函数"pointer" map调用SomeFunc.
你可以用你的LoopThroughStreetAnd功能做类似的事情:
LoopThroughStreetAnd "PersonFile.xlsx", GetRef("Payload2")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2293 次 |
| 最近记录: |