QTP使用之一个灵活的数据驱动的IF函数

发表于:2017-2-15 09:12

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:译者:杨 俊    来源:51Testing软件测试网原创

  在上一篇文章里,我们对于能够覆盖选择案例决策结构的一个数据驱动方法进行了研究,这种灵活的方法让此类结构案例变得更加有效、可管理,并且让代码也变得可读。
  而这篇短文描述了以一种类似方式的方法去覆盖"If-Else If-Else-End"的If结构案例。结果是,又一次地,让代码变得更加简洁、高效、灵活、并且方便维护。
  函数签名是:
  Function [If](ByVal strExpression, ByVal strDoIfTrue, ByVal dicElseIf, ByVal strDoIfFalse)
  这里:
  1.strExpression是一个表达式,返回的是"真"或者"假"
  2.strDoIfTrue是一个有效的存储过程名或者一段代码,当strExpression返回"真"的时候,就被执行
  3.dicElseIf是一个带有键-值对的字典对象,键定义每个条件,值定义当条件为真时的执行命令。相比于多次使用IF和Else语句,我们可以用先前提到的[Select Case]函数,传入这个字典对象。
  4.strDoIfFalse是一有效的存储过程名称或者一段代码,当strExpression返回"假"时被执行。
  备注:方括号是VBScript一不太常用的特性,里面可以使用特殊字符或者保留关键字来标识。
  覆盖IF语句的[IF]方法如下:
-----------------------------------------------------------------------------------------
Function [If](ByVal strExpression, ByVal strDoIfTrue, ByVal dicElseIf, ByVal strDoIfFalse)
[If] = Eval(strExpression)
If([If])Then
PrintLog("If", "Executing " & strDoIfTrue)
Execute(strDoIfTrue)
ElseIf ([Select Case](dicElseIf)) Then
PrintLog("If", "Executed ElseIf")
Else
PrintLog("If", "Executing " & strDoIfFalse)
Execute(strDoIfFalse)
End If
End Function
-----------------------------------------------------------------------------------------
  在上面我们可以看到,[IF]函数里面用了[Select Case]的函数(该函数里面加了日志事件,如下所示)
-----------------------------------------------------------------------------------------
Function [Select Case](ByVal dicCases)
Dim [Case], [Cases]
[Select Case] = False
If (Not TypeName(dicCases) = "Dictionary") Then
PrintLog("Select Case", "Nothing to do")
Exit Function
End If
If (dicCases.Count = 0) Then
PrintLog("Select Case", "Nothing to do")
Exit Function
End If
[Cases] = dicCases.keys
For each [Case] in [Cases]
[Select Case] = Eval([Case])
If([Select Case])Then
PrintLog("Select Case", "Executing " & dicCases([Case]))
Execute(dicCases([Case]))
Exit For
End If
Next
End Function
-----------------------------------------------------------------------------------------
  我们可以看到以上两个函数都使用了自定义函数PrintLog,如下所示。该函数将步骤细节都录入到UFT报告中,并且在输出面板中打印出事件日志来。
-----------------------------------------------------------------------------------------
Function PrintLog(ByVal strStepName, ByVal strDetails)
Print(strStepName & " - " & strDetails)
Reporter.ReportEvent micDone, strStepName, strDetails
End Function
-----------------------------------------------------------------------------------------
  使用
Dim arrTemp, i
Dim dic
Dim Condition, IfIsTrue, IfIsElseIf, IFIsFalse
Set dic = CreateObject("Scripting.Dictionary")
Condition = "arrTemp(i)>70"
IfIsTrue = "PrintLog(Now()&"" Wear a T-Shirt!"")"
Set dic = CreateObject("Scripting.Dictionary")
dic.Add "arrTemp(i)>60", "PrintLog(arrTemp(i)&"" Wear a hat!"")"
dic.Add "arrTemp(i)>50", "PrintLog(""Testing [If]"", arrTemp(i)&"" Wear a long-sleeved shirt!"")"
Set IfIsElseIf = dic
IfIsFalse = "Wscript.Echo(Now()&"" Wear a coat!"")"
arrTemp = Array(50, 55, 60, 65, 70, 80)
For i = LBound(arrTemp) To UBound(arrTemp)
PrintLog("Testing [If]", "Temperature is " & arrTemp(i) & ": " & [If](Condition, IfIsTrue, IfIsElseIf, IfIsFalse))
Next
   ... ...
   查看全文内容,请点击下载:http://www.51testing.com/html/15/n-3715215.html
版权声明:51Testing软件测试网及相关内容提供者拥有51testing.com内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号