在上一篇文章里,我们对于能够覆盖选择案例决策结构的一个数据驱动方法进行了研究,这种灵活的方法让此类结构案例变得更加有效、可管理,并且让代码也变得可读。
而这篇短文描述了以一种类似方式的方法去覆盖"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内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。