«

»

Aug 22 2016

A Flexible Data-Driven If Function

In a previous post we delved into a method to override the Select Case decision-making structure to enable more efficient, manageable and readable coding which uses a flexible data-driven approach.

This short article describes how to override the If-Else If-Else-End If structure in a similar fashion. The result is, again, code which turns out more concise, efficient, flexible, and as such also easier to maintain.

The function signature is:

Function [If](ByVal strExpression, ByVal strDoIfTrue, ByVal dicElseIf, ByVal strDoIfFalse)

Where:

  1. strExpression is an expression that evaluates as true or false
  2. strDoIfTrue is a valid procedure name or piece of code to execute if strExpression is true
  3. dicElseIf is a Dictionary object with key-value pairs defining each condition and the action to execute if evaluated as true. This way, instead of having multiple Else If statements, we can handle them using the previously mentioned [Select Case] function which accepts a Dictionary with the pairs of cases to evaluate and their corresponding commands.
  4. strDoIfFalse is a valid procedure name or piece of code to execute if strExpression is false
  5. Note: The square brackets is a not too well known (or documented) feature of VBScript which enables using special characters and reserved keywords in the names of identifiers.

    The [If] function that overrides the If statement is:

    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

    As can be seen above, the [If] function uses the [Select Case] (improved with additional logged events; seee below).

    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

    As can be seen in both functions above, they use the custom PrintLog function which can be found below. The function just sends the step details to the UFT report and prints the log of events to the Output pane.

    Function PrintLog(ByVal strStepName, ByVal strDetails)
    	Print(strStepName & " - " & strDetails)
    	Reporter.ReportEvent micDone, strStepName, strDetails
    End Function

    Usage

    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

    Output (UFT log)
    [If] – Executing Call PrintLog(“Testing [If]”, Now()&” Wear a coat!”)
    Testing [If] – 22-Aug-16 11:41:54 PM Wear a coat!
    Testing [If] – Temperature is 50: False
    [Select Case] – Call PrintLog(“Testing [If]”, arrTemp(i)&” Wear a long-sleeved shirt!”)
    Testing [If] – 55 Wear a long-sleeved shirt!
    [If] – Executed ElseIf
    Testing [If] – Temperature is 55: False
    [Select Case] – Call PrintLog(“Testing [If]”, arrTemp(i)&” Wear a long-sleeved shirt!”)
    Testing [If] – 60 Wear a long-sleeved shirt!
    [If] – Executed ElseIf
    Testing [If] – Temperature is 60: False
    [Select Case] – Call PrintLog(“Testing [If]”, arrTemp(i)&” Wear a hat!”)
    Testing [If] – 65 Wear a hat!
    [If] – Executed ElseIf
    Testing [If] – Temperature is 65: False
    [Select Case] – Call PrintLog(“Testing [If]”, arrTemp(i)&” Wear a hat!”)
    Testing [If] – 70 Wear a hat!
    [If] – Executed ElseIf
    Testing [If] – Temperature is 70: False
    [If] – Executing Call PrintLog(“Testing [If]”, Now()&” Wear a T-Shirt!”)
    Testing [If] – 22-Aug-16 11:41:54 PM Wear a T-Shirt!
    Testing [If] – Temperature is 80: True

About Meir Bar-Tal

Meir Bar-Tal is an Automation Architect with extensive technical, educational and leadership experience in Software Design, Development and Testing. For many years Meir has helped companies establish solid foundations for their testing activities with a wide variety of software applications and systems and across different technologies and platforms. Meir is a well-known expert in Data-Driven, Keyword Driven and Hybrid methodologies and has published a number of influential articles on the applications of Design Patterns to Test Automation. For the last five years, he has been pushing forward an ambitious program towards the development of an Object Oriented automation framework named Sunscrit, a huge project which is still underway. Meir is the owner of www.AdvancedQTP.com of which he was a co-founder in 2007 and served as its Editor in Chief, Author and Forums Administrator ever since. Meir lives in Israel and cooperates closely with HP R&D and other colleagues to improve the user experience and productivity with HP Software tools.