ST Tutorial Java
ST Tutorial Java
ST Tutorial Java
Applications
SilkTest
2006 Release 2
Borland Software Corporation 20450 Stevens Creek Blvd., Suite 800
Cupertino, California 95014 USA http://www.borland.com
Borland Software Corporation may have patents and/or pending patent applications covering subject matter in this document. Please
refer to the product CD or the About dialog box for the list of applicable patents. The furnishing of this document does not give you
any license to these patents.
Copyright © 2002–2007 Borland Software Corporation and/or its subsidiaries. All Borland brand and product names are trademarks
or registered trademarks of Borland Software Corporation in the United States and other countries. All other marks are the property
of their respective owners.
January 2007
PDF
2
Table of Contents
Introduction to Java Tutorials............................................................................................. 4
Update the sample Java application BAT file paths .................................................. 4
To update AWT_TestApplication.bat ..............................................................................................4
To update JFC_TestApplication.bat................................................................................................4
Java AWT Tutorial .............................................................................................. 5
Objectives .......................................................................................................................................5
Step 1: Becoming familiar with the AWT application.......................................................................6
Step 2: Focusing on a part of the application to test .......................................................................7
Step 3: Identifing custom controls in the Drawing Area window .....................................................8
Step 4: Recording a class for the Drawing Area canvas.................................................................9
Step 5: Recording window declarations for the Drawing Area ......................................................10
Step 6: Preparing the test script file ..............................................................................................11
Step 7: Recording a test against Drawing Area controls...............................................................12
Step 8: Running the recorded test against the sample Java AWT application .............................14
Step 9: Extending the test programmatically.................................................................................15
Step 10: Running the extended test..............................................................................................17
Java JFC/Swing Tutorial .................................................................................... 18
Objectives .....................................................................................................................................18
Before you begin ...........................................................................................................................18
Step 1: Use the Basic Workflow Bar to enable Java support........................................................19
Step 2: Becoming familiar with the JFC Test Application..............................................................20
Step 3: Focusing on a part of the application to test .....................................................................21
Step 4: Recording window declarations for the Page List window................................................22
Step 5: Preparing the test script file ..............................................................................................23
Step 6: Recording a test against Page List controls .....................................................................24
Step 7: Running the recorded test against the sample JFC Test Application ...............................26
Step 8: Getting native methods for a predefined Swing class.......................................................27
Step 9: Recording new window declarations for the Page List window ........................................29
Step 10: Developing a new test using a native method ................................................................30
Step 11: Running the test that uses native methods.....................................................................31
Introduction to Java Tutorials
These tutorials are designed to introduce you to using SilkTest to test stand-alone Java applications
developed with AWT and Java Foundation Class (JFC). These tutorials use the following sample Java
applications that are included with SilkTest:
• Java AWT TestApp
• JFC Test Application
Borland no longer ships JRE 1.2 with SilkTest. In order to run the SilkTest sample Java applications,
you may need to update the Java reference in the *.bat file that launches each sample application. If
you do not have a local java.exe, you can download one from java.sun.com.
SilkTest also comes with test applications for .NET and for SWT controls:
• .NET 1.1 TestApp – for more information, see the “Overview of .NET support” topic in the
online help
• SWT Test Application – for more information, see the “Working with the SWT Test Application”
topic in the online help
Update the sample Java application BAT file paths
In order to run the SilkTest sample Java applications, you may need to update the Java reference in
the *.bat file that launches each sample application.
To update AWT_TestApplication.bat
This is the batch file that launches the Java AWT Test Application.
set JavaRun=C:\jdk1.4.1\bin
4 Save your changes. You can start the sample application by double-clicking the *.bat file, or by
clicking Start/Programs/Borland/SilkTest 2006 R2/Sample Applications/Java AWT TestApp
To update JFC_TestApplication.bat
Note: this is the batch file that launches the JFC Test Application.
set JavaRun=C:\Java\jdk1.5\bin
4 Save your changes. You can start the sample application by double-clicking the *.bat file, or by
clicking Start/Programs/Borland/SilkTest 2006 R2/Sample Applications/JFC Test Application.
Before you begin this tutorial, complete the SilkTest tutorial to learn the basics of recording testcases,
running testcases, and using the recovery system. If you installed the documentation when you
installed SilkTest, you can access the tutorial by clicking Start/Programs/Borland/SilkTest 2006
R2/Documentation/SilkTest Tutorials or Help/Tutorials.
Start at Step 1 Becoming familiar with the AWT application.
2 Click on the Control menu, and then click Drawing area. The Drawing area window opens. It
contains a canvas for drawing points and lines with the mouse, an Event Log that records mouse
actions in the canvas, a Reset button, and an Exit button.
3 Click and drag the mouse inside the canvas to draw points and lines. Note how the mouse actions
are recorded in the Event Log. Click the Exit button to close the Drawing Area window.
1 Select Control/Drawing area in the Java application. The Drawing Area window opens.
3 Move the mouse pointer inside the Event Log (the scrollable white rectangular area). SilkTest
sees the Event Log as a JavaAwtTextField object, a predefined AWT control.
4 Move the mouse pointer over the Reset button, then over the Exit button. SilkTest sees these
buttons as JavaAwtPushButton objects, predefined AWT controls.
5 Move the mouse pointer inside the drawing canvas (the gray rectangular area). SilkTest sees the
drawing canvas as a CustomWin object, a custom Java control.
We have determined that the drawing canvas is a custom Java control. Since we will use this custom
control in our test, we need to record a class for the drawing canvas; continue to Step 4: Recording a
class for the Drawing Area canvas.
1 Open a new include file. In SilkTest, select File/New/ 4Test Include File in the New dialog, and
then click OK. An untitled 4Test include file opens.
2 Save the include file as canvas.inc in the extend subdirectory of the directory where you installed
SilkTest.
3 Disable recording multiple tags. Select Options/Recorder, make sure Record multiple tags is not
selected, and then click OK.
4 With canvas.inc as the active window, select Record/Class/Scripted. The Record Class dialog
opens.
5 Make sure Show all classes is not checked in the Record Class dialog.
6 Move your mouse pointer over the drawing canvas in the sample Java application.
7 When DrawingCanvas appears in the Class Name field, press Ctrl-Alt. Native methods for the
drawing canvas appear in the Record Class dialog.
8 Click the Derived From drop-down menu and search the list of available 4Test classes. Since
there is no class type in the list that maps directly to drawing canvas, select AnyWin, a generic
class.
9 Click Paste to Editor to record the new class declaration in the include file.
Now, you're ready to record window declarations for the predefined Java controls and the custom
DrawingCanvas object in the Drawing Area window; continue to Step 5: Recording window
declarations for the Drawing Area.
1 Create a new test frame file. In SilkTest, select File/New, select Test Frame in the New dialog, and
click OK. The New Test Frame dialog opens.
2 Select Test Application in the Application field and edit the File name field so it reads:
3 Click OK. The 4Test Include File draw.inc opens and is automatically loaded in SilkTest.
4 Manually load the class include file canvas.inc, which you created in Task 5. In SilkTest, select
Options/Runtime, click the Add button in the Runtime Options dialog, select canvas.inc from the
extend directory, and click OK. The file canvas.inc is added to the Use Files field.
7 Move your mouse pointer over the title bar of the Drawing Area window. When DrawingArea
appears in the Identifier field, press Ctrl-Alt. Declarations are captured for the Drawing Area
window and all of its controls.
8 Click DrawingCanvas in the Window Declaration list and change the name in the Identifier field to
Canvas.
Now, you're ready to prepare the test script file. Continue to Step 6: Preparing the test script file.
1 Close all secondary windows in the sample Java application so that only the main Test Application
window is open.
2 Open a new test script. In the SilkTest menu bar, select File/New/ 4Test Script, and then click
OK. An untitled 4Test script file opens.
4 With draw.t as the active window, select Record/Testcase in the SilkTest menu bar.
5 Change the testcase name to LogMouseMoves, and then select DefaultBaseState from the
pulldown menu in the Application state field.
Now you're ready to record a test against the Event Log in the Drawing Area window; continue to Step
7: Recording a test against Drawing Area controls.
1 In the Record Testcase dialog, click Start Recording.The recovery system sets the
DefaultBaseState application state for the sample Java application. Once the DefaultBaseState is
established, the Record Status dialog opens and you can start recording your testcase.
2 Move your cursor into the main window of the sample application. When you see Test Application
at the bottom of the Record Status window, select Control/Drawing area.
3 Click once inside the drawing canvas (the gray rectangular area in the Drawing Area window).
Your mouse click is recorded as two mouse events in the Event Log: Button down and Button
up.
4 In the Event Log, drag your mouse pointer to select the text string Button down.
5 With your mouse pointer inside the Event Log, click Ctrl-Alt. The Verify Window dialog appears.
Make sure that JavaAwtTextField DrawingArea.EventLog appears as the window to verify.
6 Click the Method tab, and then check Include inherited to display the 4Test methods available
for the Event Log.
7 Scroll down and select VerifySelText. Look at the description of the VerifySelText. We are going
to use this method to verify that the Event Log correctly recorded the first mouse action in the
drawing canvas, which is Button down, the text you selected in step 4.
8 Click inside the text field labeled, The string or list of string you expect selected. We expect
that the Error Log recorded Button down, so type this text (including the quotation marks):
"Button down"
9 Repeat steps 4 through 8, this time for the second mouse action, "Button up".
10 Move your cursor over the Exit in the Drawing Area window. When Exit appears in the Record
Status window, click the Exit, and then click Done in the Record Status window. The Record
Testcase window reappears, displaying your actions as translated into 4Test commands.
11 In the Record Testcase window, click Paste to Editor. The testcase LogMouseMoves is pasted
into draw.t
12 Expand the testcase by clicking in LogMouseMoves, and then select Outline/Expand All.
Now you're ready to run the recorded test; continue to Step 8: Running the recorded test against the
sample Java AWT application.
1 Set keyboard and mouse delays. In the SilkTest menu bar, select Options/Agent. In the Agent
Options dialog, set keyboard event delay and mouse event delay both to 0.01. Click OK.
2 With draw.t as the active window, select Run/Run. SilkTest runs the test, restoring the sample
AWT application to its base state and interacting with the application. The testcase passes.
Now, let's extend the testcase by adding code to verify that the Event Log records the correct mouse
click coordinates; continue to Step 9: Extending the test programmatically.
1 Determine the range of acceptable values for point coordinates by checking the Rect property of
the drawing canvas, as follows:
• In the sample Java application, select Control/Drawing area.
• In SilkTest, select Record/Actions.
• Click in the title bar of Drawing area window and move your mouse pointer into the drawing
canvas. When you see Press <Ctrl-Alt> to verify window Canvas appear at the bottom of the
Record Actions dialog, press Ctrl-Alt to bring up the Verify Window.
• The values for the Rect property in the Properties to Verify window. On our system, the values
showed that the range of acceptable X coordinates was 0 to 314 and the range of acceptable Y
coordinates was 0 to 60. These values might be different on your system, depending on your
display settings and other system configuration parameters.
• Click Cancel to close the Verify Window, and then click Close to close the Record Actions
window.
• Click Exit to close the Drawing Area window.
2 Expand the testcase LogMouseMoves in your draw.t file and make the following changes:
• Above the recording block, declare two integer variables, iX to store an X-coordinate value and
iY to store a Y-coordinate value. Use the RandInt function to set iX and iY to two-digit random
numbers that fall within the range of acceptable coordinates, as determined from the Rect
property of the drawing canvas. See “RandInt function in the online Help for more details. We
set iX to between 10 and 99 and iY to a random number between 10 and 60. Here is some
sample code which you can copy and paste line by line into your test script. Substitute different
numbers in the RandInt function calls, if necessary.
int iX = RandInt(10,99)
int iY = RandInt(25,60)
• Inside the recording block, substitute iX and iY as the second and third arguments in the
command DrawingArea.Canvas.Click. This code forces your test to always draw a point at a
random location within the boundaries of the drawing canvas. The following is sample code
which you can copy and paste into your test script.
DrawingArea.Canvas.Click (1, iX, iY)
In this code, we determine the selection range of the X and Y coordinates in SetSelRange by
counting text characters from left to right in the string Button Down at (xx,yy). We force our
random integers to be two-digit numbers so the selection range will have a constant start and
end value for each coordinate.
Note that we use the str function to convert our integer variables to string arguments that can
be passed to the VerifySelText commands.
DrawingArea.EventLog.SetSelRange (1,17,1,19)
DrawingArea.EventLog.VerifySelText (str(iX))
DrawingArea.EventLog.SetSelRange (1,20,1,22)
DrawingArea.EventLog.VerifySelText (str(iY))
You're now ready to run the extended testcase LogMouseMoves; continue to Step 10: Running the
extended test.
To get started with this tutorial, make sure you meet all the requirements described in Before you
begin, below.
• Complete the SilkTest tutorial to learn the basics of recording testcases, running testcases, and
using the recovery system. The tutorial is located at Start/Programs/Borland/SilkTest 2006
R2/Documentation/SilkTest Tutorials
2 If you haven’t done so already, update the sample application batch file (see “Update the sample
Java application BAT file path” at the beginning of this document).
4 Click Enable Extensions on the Basic Workflow bar, then select the sample application. SilkTest
will prompt you to close and restart the sample application.
You are now ready to begin Step 2: Becoming familiar with the JFC Test Application.
1 Start the application. See "Running the sample Swing application" in the online Help if you need
directions for this. The Test Application dialog opens with a menu bar containing three menus.
2 Click on the Control menu, and then click Page list. The Page List window opens, displaying one
page labeled Tab 1, which is selected. The window contains the following controls:
o Area for adding pages and indicating which page is selected
o Item Text text field where you specify a label for each page you add
o Drop-down list for specifying where to display the page list (Top is the default)
o Add Item, Reset, and Exit buttons
o Add Image too and Enabled checkboxes
3 Click in the Item Text field, enter a label for a new page, select Right from the drop-down list, and
then click Add Item. Enter another label in the Item Text field, and select Add Image too, and
then click Add Item.
4 Click Exit to close the Page List window and experiment with other controls accessible from the
Control menu.
1 Create a new test frame file. In SilkTest, select File/New/Test Frame, and then click OK. The New
Test Frame dialog opens.
3 Select the test application in the Application field, and then click OK. The 4Test include file
pages.inc opens and is automatically loaded in SilkTest.
5 Select Control/Page list in the JFC Test Application, then move your mouse pointer over the title
bar of the Page List window. When xPageList appears in the Identifier field, press Ctrl-Alt.
Declarations are captured for the Page List window and all of its controls.
7 Click Paste to Editor to record the declarations in pages.inc, then save pages.inc, but leave the
file open.
8 Click Close to close the Record Window Declarations dialog, and then click Exit to close the Page
List window.
1 Close all secondary windows in the sample JFC Test Application so that only the main Test
Application window is open.
2 Open a new test script. In the SilkTest menu bar, select File/New, select 4Test Script, and then
click OK. An untitled 4Test script file opens.
3 Save the test script file as pages.t in the extend subdirectory of the directory where you installed
SilkTest.
4 With pages.t as the active window, select Record/Testcase in the SilkTest menu bar.
5 Change the testcase name to CheckAddPages and select DefaultBaseState from the pulldown
menu in the Application State field.
Now you're ready to record a test against the Page List window; continue to Step 6: Recording a test
against Page List controls.
1 In the Record Testcase dialog, click Start Recording. The recovery system sets the
DefaultBaseState application state for the sample Java application. Once the DefaultBaseState is
established, the Record Status dialog opens and you can start recording your testcase.
2 Move your cursor into the main window of the sample application. When you see Test Application
at the bottom of the Record Status window, click Control and move your cursor over Page list in
the Control menu. When you see PageList in the Record Status window, click the Page list menu
item. The Page List window opens, displaying one page called Tab 1.
3 Move your cursor over the Item Text field. When you see ItemText1 in the Record Status window,
add a page by clicking in the Item Text field, typing 2, and then clicking the Add Item button. A
second page called 2 is added to the page list.
4 Move your cursor back to the Item Text field. When ItemText1 appears in the Record Status
window, add another page by clicking just before the text 2 in the text field. Press the Delete key
once, type 3, and then click the Add Item button again. A third page called 3 is added to the page
list.
5 Click on Tab 1 in the list of pages, wait until you see ThePageList in the Record Status window,
and then press Ctrl-Alt. The Verify Window dialog opens; make sure that JavaJFCPageList
appears as the window to verify.
6 In the Verify Window dialog, click the Method tab, then check Include inherited to display the
4Test methods available for the page list. Scroll down and select the method GetPageCount. Click
OK to close the Verify Window dialog.
We're going to use the GetPageCount method to verify that AddItem adds the correct number of
pages to the page list. We started out with one page, then added two more, so the page count
should equal 3.
7 Back in the Page List window, move your cursor over the Exit button. When Exit appears in the
Record Status window, click the Exit button, and then click Done in the Record Status window.
The Record Testcase window reappears, displaying your actions as translated into 4Test
commands.
8 In the Record Testcase window, click Paste to Editor. The testcase CheckAddPages is pasted
into pages.t.
10 Edit pages.t to verify the page count by wrapping the Verify function around the call to
GetPageCount, as follows:
Verify ( PageListWindow.ThePageList.GetPageCount(), 3)
Now you're ready to run the recorded test; continue to Step 7: Running the recorded test against the
sample JFC Test Application.
Note At this point, we assume that the test script file pages.t is open.
To run the testcase CheckAddPages:
1 With pages.t as the active window, select Run/Run. SilkTest runs the test, restoring the sample
JFC Test Application to its base state and interacting with the application. The testcase passes.
Now, let's create another testcase to verify that pages are deleted from the list correctly. Our next step
will be to find a native method that allows us to delete pages from the page list; continue to Step 8:
Getting native methods for a predefined JFC class.
1 With the sample JFC Test Application running and not minimized, open the file javaex.inc in
SilkTest. Javaex,inc is located in <SilkTest install directory>\extend. Find the line that reads:
3 Open your test frame file pages.inc and click in the existing page list declaration—the line that
reads
4 Collapse this declaration if expanded and then comment it out by selecting Outline/Comment
Block.
5 Scroll to the bottom of pages.inc and create a new section by adding the following comment:
6 With pages.inc as the active window, select Record/Class. The Record Class dialog opens.
7 Make sure Show all classes is not checked in the Record Class dialog.
8 In the JFC Test Application, select Control/Page list to open the Page List window.
9 Move your mouse cursor over Tab 1 of the page list. When JavaJFCPageList appears in the Class
Name field, press Ctrl-Alt. Native methods and properties for the page list appear in the Record
Class dialog.
11 Click on the Derived From drop-down menu, and then select PageList from the list of available
4Test classes.
12 Click Paste to Editor to record the class declaration in the Native Page List Declarations section
that you just created in pages.inc.
13 Click Close to close the Record Class dialog, and save pages.inc.
Now you must record new declarations for the Page List window; continue to Step 9: Recording new
window declarations for the Page List window.
Note At this point, we assume that pages.inc is still open and that the Page List window is
still open in the sample Java Swing application.
To re-record window declarations for the Page List window:
2 Move your mouse pointer over the title bar of the Page List window in the JFC Test Application.
When xPageList appears in the Identifier field, press Ctrl-Alt. Declarations are captured for the
Page List window and all of its controls.
Now, you're ready to create a new testcase that uses the native method removeTabAt to verify that we
can remove pages from a page list; continue to Step 10: Developing a new test using a native method.
1 In pages.t, collapse the CheckAddPages testcase, select the testcase by clicking in the margin to
the left of the , and select Edit/Copy from the menu bar.
2 Open a new untitled test script file by selecting File/New/ 4Test Script, and then click OK.
3 Click in the new script file, and then select Edit/Paste to paste a copy of the CheckAddPages
testcase.
4 Change the name of the testcase to CheckDeletePages and expand the testcase.
5 Replace all instances of PageListWindow with PageListNative to match the identifier in our new
Page List window declaration in pages.inc.
6 Before the Verify function, insert a line that calls the native method, removeTabAt, to delete the
second page in the list. Here is the 4Test code (which you can copy and paste into your test
script):
PageListNative.ThePageList.removeTabAt(PageListNative.ThePageList.indexOfTab("2
"))
Note We pass the native method indexOfTab as an argument to removeTabAt to ensure that
we provide the correct zero-based index value for page 2.
7 To see how the Page List window adjusts the list after one page is removed, add a Select method
on the next line to refresh the Page List window. Here is the 4Test code (which you can copy and
paste into your test script):
PageListNative.ThePageList.Select("3")
8 Change the second argument in the Verify function from 3 to 2, since we've just removed one page
from the list.
9 Save the test script as pages2.t in the extend subdirectory of the directory where you installed
SilkTest.
Now, you're ready to run the test CheckDeletePages; continue to Step 11: Running the test that uses
native methods.
Note At this point, we assume that the test script file pages2.t is open.
To run the testcase CheckDeletePages:
With pages2.t as the active window, select Run/Run. SilkTest runs the test, restoring the sample JFC
Test Application to its base state and interacting with the application. You'll notice that Tab 2 is deleted
and, then, Tab 3 is selected. The testcase passes.
Congratulations! You have completed the Java JFC/Swing tutorial. Next, you might want to explore
"Setting up to test Java applets" and "Overview of testing standalone Java applications" in the online
Help.