Relevant Codes

A Test Development Resource for HP QuickTest Professional.

QTP: Working with SiebCalculator

by Anshoo Arora on August 10, 2009

This is the 4th article in the Siebel series. In this article, we will detail an effective way to work with the Siebel Calculator Object.

By default, a Siebel Calculator has a small set of input keys to work with. The idea is to verify that each input key is valid before processing the output through the calculator. The method IsKeyLoaded verifies if the key really exists in the calculator object (all possible keys are hard-coded in the array ‘arrFixedKeys’) whereas the method SiebCalculatorClickKeys presses the keys in an array one after the other.

Usage:

Set oCalc = SiebApplication( "uiname:=MyApp" ).SiebScreen( "uiname:=MyScreen" )_
            .SiebView( "uiname:=MyView" ).SiebApplet( "uiname:=MyApplet" )_
            .SiebCalculator( "uiname:=MyCalculator" )
 
SiebCalculatorClickKeys oCalc, Array( "1", "+", "2", "-", "3", "*", "99" )

The above will simulate (1+2-3*99) in the SiebCalculator object.

How its done:

'————————————————————————————————————————————— 
' Name: Function IsKeyLoaded
' Purpose: Checks if array of keys to be input to Calculator is valid
' Input:
'    arrKeys
' Output: 
'    Boolean
' Date:
' Author: 
' Remarks: 
'————————————————————————————————————————————— 
Private Function IsKeyLoaded( arrKeys )
'————————————————————————————————————————————— 

    Dim arrFixedKeys
    Dim blnExists
    Dim inKey, fixKey
 
    arrFixedKeys = Array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "/", "CE", "*", "C", "-", "%", &_
                     "±", ".", "+", "=" )
 
    For Each inKey in arrKeys
        blnExists = False
        For Each fixKey in arrFixedKeys
            If Trim(LCase(CStr(inKey))) = Trim(LCase(CStr(fixKey))) Then
                blnExists = True
                Exit For
            End If
        Next
        If Not blnExists Then
            IsKeyLoaded = blnExists
            Exit Function
        End If
    Next
 
    IsKeyLoaded = blnExists
 
End Function
 
'————————————————————————————————————————————— 
' Name: Sub SiebCalculatorClickKeys
' Purpose: 
' Input:
'    oCalculator - object reference to the Calculator hierarchy
'    arrKeys - Keys to be pressed on the calculator object
' Output: 
'    Integer
'Dependancy: IsKeyLoaded
' Date:
' Author: 
' Comments: 
'————————————————————————————————————————————— 
Public Sub SiebCalculatorClickKeys( ByVal oCalculator, ByVal arrKeys )
'————————————————————————————————————————————— 

    Dim i
 
    ' Check if the keys entered by the user are valid and exist as keys on the 
    '_ SiebCalculator object.
    isValid = IsKeyLoaded(arrKeys)
 
    ' The following component can be modified to also work with 'ClickKeys':
    '_ enter an array with multiple values in each element and use clickKeys if the length
    '_ is greater than 1, unless if it is the key "CE"
    If Not isValid Then
        Reporter.ReportEvent micWarning, "Invalid Key(s)", "Array: " & Join(arrKeys, ",")
    Else
        'object.OpenPopup
        For i = LBound(arrKeys) to UBound(arrKeys)
            oCalc.ClickKey arrKeys(i)
        Next
        'object.ClosePopup
    End If
 
End Sub

Download this code

When the procedure ends, the output will be visible in the calculator’s output field.

If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.

Leave a Comment

Previous post:

Next post: