Tuesday, July 22, 2008

VisualTest - Pressing Shortcut Key

Each GUI testing tool contains a way to press shortcut keys. Visual Test also has the feature to invoke shortcuts. It is similar to SendKeys in Windows Host Script. Below code I have written around eight years ago.

Visual Test code - To invoke Shortcut key



'---------------------
' Method : ShortcutKeys
' Author : T.Palani Selvam
' Purpose : Play shortcut keys.
' Parameters: sDatavalue, String - Contains a file name.
' Returns : Returns Integer data type(True or False.)
' Caller : SwitchSpecialTags
' Calls : - Nil
'----------------------
Function ShortcutKeys(sDataValue As String) As Integer
Dim iPosition As Integer
Dim sFirst As String
Dim sLast As String

ShortcutKeys = False

While (Instr(LCase(sDataValue),"+")<> 0 )
iPosition = Instr(LCase(sDataValue),"+")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 1)
sDataValue = sFirst + sLast
Wend


If (Instr(LCase(sDataValue),"ctrl")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"ctrl")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 4)
sDataValue = sFirst + "^" + sLast
End IF

If (Instr(LCase(sDataValue),"shift")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"shift")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 5)
sDataValue = sFirst + "+" + sLast
End IF

If (Instr(LCase(sDataValue),"alt")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"alt")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 3)
sDataValue = sFirst + "%" + sLast
End IF

If (Instr(LCase(sDataValue),"tab")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"tab")
sFirst = Left(sDataValue, iPosition - 1)
sLast = Mid$(sDataValue, iPosition + 3)
sDataValue = sFirst + "{TAB}" + sLast
End IF

If (Instr(UCase(sDataValue),"DELETE")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"DELETE")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 6)
sDataValue = sFirst + "{DEL}" + sLast
ElseIf (Instr(UCase(sDataValue),"DEL")<> 0 ) Then
iPosition = Instr(UCase(sDataValue),"DEL")
sFirst = Left(sDataValue, iPosition - 1)
sLast = Mid$(sDataValue, iPosition + 3)
sDataValue = sFirst + "{DEL}" + sLast
End IF


If (Instr(LCase(sDataValue),"enter")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"enter")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 5)
'sDataValue = sFirst + "{ENTER}" + sLast
sDataValue = sFirst + "~" + sLast
End IF

If (Instr(LCase(sDataValue),"right")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"right")
sFirst = Left(sDataValue, iPosition - 1)
sLast = Mid$(sDataValue, iPosition + 5)
sDataValue = sFirst + "{RIGHT}" + sLast
End IF

If (Instr(LCase(sDataValue),"left")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"Left")
sFirst = Left(sDataValue, iPosition - 1)
sLast = Mid$(sDataValue, iPosition + 4)
sDataValue = sFirst + "{LEFT}" + sLast
End IF

If (Instr(LCase(sDataValue),"down")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"down")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 4)
sDataValue = sFirst + "{DOWN}" + sLast
End IF

If (Instr(LCase(sDataValue),"up")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"up")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{UP}" + sLast
End IF

If (Instr(UCase(sDataValue),"ESCAPE")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"ESCAPE")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 6)
sDataValue = sFirst + "{ESC}" + sLast
ElseIf (Instr(UCase(sDataValue),"ESC")<> 0 ) Then
iPosition = Instr(UCase(sDataValue),"ESC")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 3)
sDataValue = sFirst + "{ESC}" + sLast
End IF

If (Instr(UCase(sDataValue),"INSERT")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"INSERT")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 6)
sDataValue = sFirst + "{INS}" + sLast
ElseIf (Instr(UCase(sDataValue),"INS")<> 0 ) Then
iPosition = Instr(UCase(sDataValue),"INS")
sFirst = Left(sDataValue, iPosition - 1)
sLast = Mid$(sDataValue, iPosition + 3)
sDataValue = sFirst + "{INS}" + sLast
End IF

If (Instr(UCase(sDataValue),"BREAK")<> 0 ) Then
iPosition = Instr(UCase(sDataValue),"BREAK")
sFirst = Left(sDataValue, iPosition - 1)
sLast = Mid$(sDataValue, iPosition + 5)
sDataValue = sFirst + "{BREAK}" + sLast
End IF

If (Instr(LCase(sDataValue),"f1")<> 0 ) Then
iPosition = Instr(LCase(sDataValue),"f1")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F1}" + sLast
End IF

If (Instr(uCase(sDataValue),"F2")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F2")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F2}" + sLast
End IF

If (Instr(uCase(sDataValue),"F3")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F3")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F3}" + sLast
End IF

If (Instr(uCase(sDataValue),"F4")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F4")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F4}" + sLast
End IF

If (Instr(uCase(sDataValue),"F5")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F5")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F5}" + sLast
End IF

If (Instr(uCase(sDataValue),"F6")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F6")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F6}" + sLast
End IF

If (Instr(uCase(sDataValue),"F7")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F7")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F7}" + sLast
End IF

If (Instr(uCase(sDataValue),"F8")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F8")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F8}" + sLast
End IF

If (Instr(uCase(sDataValue),"F9")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F9")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 2)
sDataValue = sFirst + "{F9}" + sLast
End IF

If (Instr(uCase(sDataValue),"F10")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F10")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 3)
sDataValue = sFirst + "{F10}" + sLast
End IF

If (Instr(uCase(sDataValue),"F11")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F11")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 3)
sDataValue = sFirst + "{F11}" + sLast
End IF

If (Instr(uCase(sDataValue),"F12")<> 0 ) Then
iPosition = Instr(uCase(sDataValue),"F12")
sFirst = Left(sDataValue, iPosition -1)
sLast = Mid$(sDataValue, iPosition + 3)
sDataValue = sFirst + "{F12}" + sLast
End IF

Logwrite ("Shortcut key processed: " + sDataValue, 1)

sDataValue = Trim(lCase(sDataValue)) 'Because some times, shortcut keys are not functioned in Upper case.

Play sDataValue

ShortcutKeys = True

End Function

No comments: