RSS
 

Archive for June, 2010

#12: An example of testing TreeView CheckBox with TestPartner

29 Jun

In this post, I will give you an example script from TestPartner to test if the CheckBox is checked or not in the TreeView window. The suggestion B from post#11 is used here, i.e., the “BitmapExists” method is used to detect the CheckBox state in the TreeView window. In order to use the “BitmapExists” method, you need to capture a bitmap of the CheckBox and store it under Object Map Asset. How to do this? You can find the detailed instruction in Chapter7 Image Mapping of the TestPartner Advanced training guide.

Since multiple protocols from different measurement packages will be tested (each protocol has a CheckBox, and all the protocols are listed in the TreeView window), the data (package and protocol names) are saved in an Excel file, and will be used as input to the test script. The way to input data from Excel file is mentioned in post#9. Let’s have a look of how the data is organized in the Excel file (its path is defined in “Declaration_GlobalConstants” which is saved in Module Asset). One of the spreadsheet is called as the “Protocol” tab, and in this tab, the first row stores different packages names, and each cell stores one package name. Under each column, the first/top cell is the package name, and starting from the second cell, protocol names belong to this package are stored one after one in each cell.

'Testing condition: After clean install
'Check all the measurement protocols from all the measurement packages are enabled in the Preference panel - Measurement tab

'$TPinclude "Declaration_GlobalConstants"

Sub Main()

Dim RowCount As Integer
Dim ColumnCount As Integer
Dim SheetData() As String 'Dynamic array

'Use Shared Modules "Function_ExcelSheetRowCount" and "Function_ExcelSheetColumnCount"
'to get how many rows and columns in the Protocol sheet of "MeasurementTab.xlsx"
RowCount = Function_ExcelSheetRowCount.ExcelSheetRowCount(ExcelFilePath_MeasurementTab, "Protocol")
ColumnCount = Function_ExcelSheetColumnCount.ExcelSheetColumnCount(ExcelFilePath_MeasurementTab, "Protocol")

'Array is sized dynamically
ReDim SheetData(RowCount, ColumnCount) As String

'Use Shared Module "Function_GetExcelSheetData" to store data from the Protocol sheet of "MeasurementTab.xlsx" in the array
SheetData() = Function_GetExcelSheetData.GetExcelSheetData(ExcelFilePath_MeasurementTab, "Protocol")

'Open the workstation
    ProgramManagerWindow("Application=EXPLORER.EXE Caption='Program Manager'").Attach
        ListView("Index=1").Select "the workstation", tpMouseDoubleClick

'In the workstion, open Preference panel, then open the Measurement tab
    Window("Application=VSIAPP.EXE Caption=the workstation'").Attach
        Button("Caption=Prefs").Click
        TabControl("Parent.Caption=Preferences").Select "    Measurement    "

        'Select different Packages from the ComboBox, and check their protocols are enabled
        For i = 1 To ColumnCount
            For j = 1 To RowCount
                If j = 1 Then   'First row in the Protocol sheet is package name
                    'Select the package
                    ComboBox("Parent.Caption=Preferences").Select SheetData(1, i)
                    Dim Package As String
                    Package = SheetData(1, i)
                Else    'Other rows in the Protocol sheet are protocol names
                    'If the Excel cell is not empty, check all the CheckBoxes beside protocol nodes
                    If SheetData(j, i) <> "" Then
                        Dim ProtocolPath As String
                        ProtocolPath = "\Measurements\" & SheetData(j, i)
                        'Report error if the protocol name read from Excel sheet
                        'does NOT match the protocol name displayed in the workstation
                        On Error GoTo 0    'Disables error handling in the current procedure
                        TreeView("Parent.Caption=Preferences").SelectItem (ProtocolPath)

                        'Store the mouse position after "SelectItem", which is always
                        'at the very left and in the middle of the first charactor of the protocol name
                        Dim tempx, x, y As Integer
                        tempx = TreeView("Parent.Caption=Preferences").MouseX
                            'When the protocol name is longer than the width of the treeview window
                            'tempx = 0
                            If tempx <> 0 Then
                                x = tempx
                            End If
                        y = TreeView("Parent.Caption=Preferences").MouseY

                        'Handle the situation when the protocol name is longer than the width of the treeview window
                        On Error Resume Next    'Handle the situation when there is no scroll bar
                        TreeView("Parent.Caption=Preferences").Scroll 0, tpScrollHorizontal

                        'Search if the CheckBoxInTreeView.bmp (13*13 pixels) exists or not
                        'Write the results to result summary
                        If TreeView("Parent.Caption=Preferences").BitmapExists("CheckBoxInTreeView", x - 16, y - 8, 17, 17) Then
                            UserCheck "CheckBoxInTreeView", True, "The " & Package & ProtocolPath & " CheckBox is checked"
                        Else
                            UserCheck "CheckBoxInTreeView", False, "The " & Package & ProtocolPath & " CheckBox is NOT checked"
                        End If
                    End If
                End If
            Next
        Next

    'Close application to reset its original state
    Window.Close

End Sub

From this post, we learned the following:
1, Get the coordinates of the current cursor position (MouseX and MouseY);
2, Error handling (“On Error GoTo 0″ and “On Error Resume Next”), see post#13 for a detailed explanation;
3, How to move the scroll bar horizontally to x=0 (Scroll 0, tpScrollHorizontal);
4, How to use “BitmapExists” method (“BitmapExists(“CheckBoxInTreeView”, x – 16, y – 8, 15, 15)” means start searching from x-16 and y-8, and search for 15 by 15 pixels area; in this area, search for bitmap “CheckBoxInTreeView”);
5, How to write check results to final result summary (UserCheck).

 
No Comments

Posted by Jia Qi in TestPartner (TP)

 

#11: Who can test Windows TreeView with Checkbox? QTP or TestPartner?

23 Jun


TreeView is a Windows Standard Class, and many Windows Applications use TreeViews. Such as the Workstation I was testing in post#10, the measurement Protocols belong to each measurement Package are listed in a TreeView Window as nodes. Each Portocol Node also has a CheckBox beside it, and I need to test if these CheckBoxes are checked or not.

In TestPartner/QTP, TreeView window is identified as the “TTreeView”/”WinTreeView” object:
1, The nodes inside the TreeView window can NOT be identified by the identification tool;
2, The TreeView window is identified as a TestPartner/QTP TreeView object, not a Windows Standard TreeView object, which means you can ONLY use the TestPartner/QTP built-in methods and properties for the “TTreeView”/”WinTreeView” object. You can NOT use the Windows Standard TreeView methods nor properties.

TestPartner/QTP do have some built-in methods and properties for the “TTreeView”/”WinTreeView” object to allow you to get access to or fetch the properties of the nodes inside the TreeView window. For example, in TestPartner, the most useful method is the “SelectItem” method. However, it disappointingly returns a Boolean…Actually, NONE of the TestPartner built-in “TTreeView” methods or properties can help you to get the properties of the CheckBoxes beside the TreeView nodes. For this particular feature, QTP is much more powerful. It has a built-in method called “CheckItemProperty” for the “WinTreeView” object, which can be used to check all kinds of properties of a TreeView node, including if the CheckBox is checked or not.

The way to use “CheckItemProperty” method in QTP is:
object.CheckItemProperty (Item, PropertyName, PropertyValue, [TimeOut])
One of its “PropertyName” (a String value) is “state”: indicates whether the tree-view control node has a check box, and whether it is selected, and its possible values:
0–the tree-view control node does not have a check box
1–the tree-view control node’s check box is not selected
2–the tree-view control node’s check box is selected
The script to test the Ultrasound workstation will look like this:

Window("the workstation").WinTreeView("SysTreeView32").CheckItemProperty "Measurements;Protocol name", "State", 2

Now we know that we can use QTP to easily check if a CheckBox besides a TreeView node is checked or not. However, how do we handle this issue with TestPartner? (The built-in “TTreeView” methods and properties in TestPartner are insufficient for us to test the TreeView CheckBox).

Here I provide two solutions to use TestPartner to test the TreeView CheckBox:
A, Write a separate script to identify the TreeView object in any Windows Applications as a Windows Standard TreeView Object. In this way, you will be able to use the standard TreeView methods and properties. For example, you can use the “SelectedImageIndex” Property to get the image list index value of the image that is displayed when a tree node is selected (images can be “no CheckBox”, “with CheckBox but not checked”, “with CheckBox and checked”).

This kind of script will be difficult to write. 8thString has wrote a script about how to get TreeView item information using Win32 api. The script is very long, if you are interested, you may want to have a look of it. Besides it is long, it is also very backended. The script talks “Process”, “Memory” and “kernel32″ a lot, which is actually a developer level’s code.

If you do not understand the above script from 8thString, you may want to read my second suggestion to solve the TreeView CheckBox issue with TestPartner:
B, Use the “BitmapExists” method to verify the existence of a pixel from the check mark (saved as a bitmap image called as “MeasurementTreeViewImage”) in the CheckBox area of the TreeView window. The script to test the Ultrasound workstation will look like the following:

Window("the workstation'").Attach
    Set obj = TreeView("Parent.Caption=Preferences")
    If obj.SelectItem("\Measurements\Protocol name") Then
        y = obj.MouseY
        obj.Scroll (tpScrollHorizontal)
        For x = 0 to obj.Width 'you can reduce the search area from the TreeView area to the ChceckBox area by hard coding like this: For x = 40 To 51
            If obj.BitmapExists("MeasurementTreeViewImage", Left:=x, Top:=y, Width:=1, Height:=1) Then
                UserCheck "mycheck", True, "'Protocol CheckBox is checked"
                Exit For
            End If
        Next
    End If
 

#10: Exercise the GetExcelSheetData function with TestPartner to test a Windows Application

16 Jun



In this post, we will practice with the function I wrote in my last post#9 about storing Excel sheet data into an array. We will also start to use TestPartner to test an ultrasound workstation. You do not need to know anything about ultrasound device. You just need to know that the workstation is a Windows Application, i.e., a software.

Users will analyze ultrasound images on the workstation, and they will draw some measurements there. The measurements are built in features, users just need to select the measurement they want to use, and draw it on the image area, and the workstation will automatically calculate the values for the measurement.

There are thousands of build in measurements, which make this a perfect candidate for automation testing. The measurements are organized in 3 levels: Package, Protocol, and Measurement. Each Package contains many Protocols, and each Protocols contains many Measurements.

Today, we will test the following:
Launch the workstation -> Open the Preference Panel -> Select each measurement Packages in the Measurement Package ComboBox, and check “Enable Package” CheckBox is checked.

In the main script, we will use a Global constant called “Declaration_GlobalConstants” which is saved in the Module asset:

Public Const ExcelFilePath_MeasurementTab = "C:\...\MeasurementTab.xlsx"

3 functions will be called in the main script too, and they are saved in the Shared Module asset. Their names are “Function_ExcelSheetRowCount”, “Function_ExcelSheetColumnCount”, and “Function_GetExcelSheetData”. For their scripts please refer to my last post#9

'Testing condition: After clean install
'Check all the measurement packages are enabled in the Preference panel - Measurement tab

'$TPinclude "Declaration_GlobalConstants"

Sub Main()

Dim RowCount As Integer
Dim ColumnCount As Integer
Dim SheetData() As String 'Dynamic array

'Use Shared Modules "Function_ExcelSheetRowCount" and "Function_ExcelSheetColumnCount" 'to get how many rows and columns in the Package sheet of  "MeasurementTab.xlsx"
RowCount = Function_ExcelSheetRowCount.ExcelSheetRowCount(ExcelFilePath_MeasurementTab, "Package")
ColumnCount = Function_ExcelSheetColumnCount.ExcelSheetColumnCount(ExcelFilePath_MeasurementTab, "Package")

'Array is sized dynamically
ReDim SheetData(RowCount, ColumnCount) As String

'Use Shared Module "Function_GetExcelSheetData" to store data from the Package sheet of "MeasurementTab.xlsx" in the array
SheetData() = Function_GetExcelSheetData.GetExcelSheetData(ExcelFilePath_MeasurementTab, "Package")

'Open the workstation
    ProgramManagerWindow("Application=EXPLORER.EXE Caption='Program Manager'").Attach
        ListView("Index=1").Select "the workstation", tpMouseDoubleClick

'In the workstion, open Preference panel, then open the Measurement tab
    Window("Application=VSIAPP.EXE Caption='the workstation").Attach
        Button("Caption=Prefs").Click
        TabControl("Parent.Caption=Preferences").Select "    Measurement    "

        'Select different Packages from the ComboBox, and check if "Enable Package" is checked or not
        For i = 1 To ColumnCount
            For j = 1 To RowCount
                ComboBox("Parent.Caption=Preferences").Select SheetData(j, i)
                ExecuteCheck "Check_PackageEnable"
            Next
        Next

    'Close application to reset its original state
    Window.Close

End Sub

From this post, we learned the following:
1, How to define a Global Constant, and how to use the Global Constant;
2, How to call functions;
3, When call scripts from Module, you need to include ‘$TPinclude “Module Name” at the beginning of your script; but if it is a Shared Module, then no need to do so;
4, How to insert a property check (Insert->Check->Property…).

 
1 Comment

Posted by Jia Qi in TestPartner (TP)

 

#9: Get data from Excel for TestPartner and QTP

11 Jun

QTP and TestPartner provide integrated features for you to test multiple data in one test case. In QTP, the feature is called Data Table, and in TestPartner it is called Active Data. However, both of the features have an disadvantage, which is they only support Data Table or Active Data that has a single sheet.

For example, in TestPartner, to make the Active Data working, first, you need to import an excel file to the Active Data asset. If your excel file has multiple data sheets, and you want to use each of them, this becomes impossible, since in the Active Data asset, you can only select one data sheet (done through the option tab). If you really want to use another data sheet, then you have to rename your excel file, and import it again, and this time, select another data sheet in the option tab.

Someone like me, who wants to save space on the Database and make data easy to maintain, would like to use one Excel file that has multiple sheets instead of use multiple Excel files contain only one sheet per file.

How to do this, we should really get ride of using the Active Data from TestPartner or the Data Table from QTP. We can get data directly from an external Excel file (contains single or multiple work sheets). I have written a function for this, and will show it to you below.

In TestPartner, you may want to save the following code in the Shared Module, since this script can be used across different projects:

Function GetExcelSheetData(FilePath As String, SheetName As String)

'On error jump to "Error Handler:" line
On Error GoTo ErrorHandler

'Variable declaration
Dim SheetCount As Integer
Dim RowCount As Integer
Dim ColumnCount As Integer
Dim CellArray() As String 'A dynamic array that is not sized in the Dim statement

'Open the Excel file
Set ObjExcel = CreateObject("Excel.Application")
Set ObjWorkBook = ObjExcel.Workbooks.Open(FilePath)

'Count how many sheets in the Excel file
SheetCount = ObjWorkBook.WorkSheets.Count

'Store the data from a specific worksheet into an array
For i = 1 To SheetCount
    If ObjWorkBook.WorkSheets(i).Name = SheetName Then

        'Count how many rows and columns in the Excel file (only for the used range)
        RowCount = ObjWorkBook.WorkSheets(i).UsedRange.Rows.Count
        ColumnCount = ObjWorkBook.WorkSheets(i).UsedRange.Columns.Count

        'Array is sized with the ReDim statement
        'after RowCount and ColumnCount are given values.
        ReDim CellArray(1 To RowCount, 1 To ColumnCount) As String

        'Store the data from the specific worksheet into CellArray
        For j = 1 To RowCount
            For k = 1 To ColumnCount
               CellArray(j, k) = ObjWorkBook.WorkSheets(i).UsedRange.Cells(j, k)
            Next
        Next

    End If
Next

'Close the Excel file
ObjWorkBook.Close

'Return CellArray
GetExcelSheetData = CellArray()

Exit Function

ErrorHandler: ' This is a normal VBA line label
    MsgBox Err.Description
    ObjWorkBook.Close

End Function

From the above script, you learned the following:
1, How to write a function with input parameters
2, When deal with external files, you better write something to handle errors
3, How to use “On Error GoTo” (more details see post#13)
4, How to define an array dynamically (Dim and Redim)
5, How to use Excel Object Model, including Excel.Application, Excel.Workbook, Excel. Worksheet, and Excel.Range. (see here)
6, How to return a value in a function

Make some modifications for the Function_GetExcelSheetData Function above, you should be able to write another 2 functions: one is called as Function_ExcelSheetRowCount, and the other one is called as Function_ExcelSheetColumnCount. These 2 functions will return how many rows or columns in a specific Excel sheet, and they will be used together with Function_GetExcelSheetData in my next post.

In order to test the above function, you can write a separate test script to execute it (“Function_GetExcelSheetData” is the file name):

Sub Main()
Dim A
A = Function_GetExcelSheetData.GetExcelSheetData("C:\...\MeasurementTab.xlsx", "Sheet1")
End Sub

In TestPartner, if the function does not have a return value, but it has more than 1 input values, then in order to execute the function, you still need to define a variable in your test script, and assign the function to the variable. For example, you have a function “ActiveExcelSheet(FilePath As String, SheetName As String)”, which has no return value, just to active a specific sheet in the Excel file. To execute the function, your script needs to look like this:

Sub Main()
Dim A
A = Function_ActiveExcelSheet.ActiveExcelSheet("C:\...\MeasurementTab.xlsx", "Sheet1")
MsgBox (A) 'this tells you A is nothing
End Sub

If the test script looks like this:

Sub Main()
Function_ActiveExcelSheet.ActiveExcelSheet("C:\...\MeasurementTab.xlsx", "Sheet1")
End Sub

TestPartner will inform you there is a “Syntax error” and won’t allow you to run the script. However, if the function has only one input variable and no return value, then you can execute it without assign it to a variable.

Now, let’s talk about how to take the above function (e.g., the “ActiveExcelSheet” function) to QTP. Since TestPartner uses VBA, but QTP uses VBScript, you need to make the following changes in the function script:
A, The function script needs to be created in the QTP Function Library (File – New – Function Library);
B, In the function script, delete anything related to the ErrorHandler (2 paragraphs);
C, In the function script, delete any definition for variable type, e.g., modify “(FilePath As String, SheetName As String)” to “(FilePath, SheetName)”, and change “Dim SheetCount As Integer” to “Dim SheetCount”.
D, Create a Test/Action to execute the function script, and the test/action will look like this (compare to TestPartner test script, no “Sub” nor “End Sub”, no need to assign the function to a variable, and no function file name nor brackets, i.e., “Function_ActiveExcelSheet.ActiveExcelSheet(…)” changes to “ActiveExcelSheet …”):

ActiveExcelSheet "C:\...\MeasurementTab.xlsx", "Sheet1"

E, Associate the Function file with the Test/Action file (right click on the Function file script and choose “Associate…” on the pop up menu), then you can run the function by running the test/action.

From this post, you may have a better understanding of QTP and TestPartner… Beside the object identifier, all the other features that you need for automation testing actually can be achieved by writing VB scripts in notepad, and save them as .vbs files…

Ok, these are just theoretically speaking, we will still write VB scripts in QTP and TestPartner instead of in notepad, right? Just for the convenience.

 

#8: Start using TestPartner to test a Windows application

01 Jun

In the coming weeks, I will show you how to use TestPartner 6.2 to test a Windows application.

Please read the TestPartner Tutorials first, it should be easy to understand if you already read my previous blogs about using QTP 9.0 to test YouPlayOff.com.

The TestPartner Tutorials has the following sections:

Visual Test Tutorial — Use the TestPartner’s visual, storyboard-based interface to create a visual test that simulates ordering parts from the Bolts ‘N Things sample web application. Learn advanced testing concepts and how to use TestPartner’s integrated time-saving features to eliminate the need for complicated scripting.

- TestPartner Basics: This module explicitly walks you through the step-by-step process of recording and playing back a visual test. Additional lessons provide you with instruction on how to analyze playback results, insert test logic to retrieve data from an external file and a test script, and how to use TestPartner’s basic debugging features.

- TestPartner Advanced Lessons: This module builds on the lessons in the first module and contains examples of advanced testing concepts and how to use TestPartner’s integrated time-saving features to insert test logic in visual tests to eliminate the need for complicated scripting.

Test Script Tutorial — Develop a traditional test script using Visual Basic for Applications (VBA) that simulates bidding on an item from the NuBid sample web application.

I also found the following TestPartner Guides, which may be useful for you. The advanced guide is a scripting level guide.

TestPartner Basic Training Guide.pdf for TestPartner 5.1
TestPartner Advanced Training Guide.pdf for Test Partner 5.2 (I suggest you to read this one)
TestPartner Basic Training Guide.pdf for TestPartner 5.3

 
No Comments

Posted by Jia Qi in TestPartner (TP)