Table of Contents
Learn how to test Tk applications.
Table of Contents
This tutorial will show you how to create, run, and modify tests for an example Tk application. In the process you will learn about Squish's most frequently used features so that by the end of the tutorial you will be able to start writing your own tests for your own applications.
This chapter presents most of the major concepts behind Squish and provides the information you need to get started using Squish for testing your own applications. This tutorial does not discuss all of Squish's features, and those that it does cover are not covered in full detail. After reading this tutorial we recommend reading the User Guide (Chapter 5), and at least skimming the API Reference Manual (Chapter 6) and the Tools Reference Manual (Chapter 7), so that you are familiar with all the features that Squish has to offer, even if you don't need to use them all straight away.
This tutorial is divided into several sections. If you are new to Squish it is best to read all of them. If you are already using Squish you might want to just skim the tutorial, stopping only to read those sections that cover any new features that you haven't used before—or you could just skip straight to the User Guide (Chapter 5).
Whenever we show how to achieve something using the IDE we will always follow with an explanation of how to do the same thing using the command line tools. Using an IDE is the easiest and best way to start, but once you build up lots of tests you will want to automate them, (e.g., doing nightly runs of your regression test suite), so it is worth knowing how to use the command line tools since they can be run from batch files or shell scripts.
For this chapter we will use a simple Address Book script as our
AUT. It is shipped with Squish as
SQUISHDIR/examples/tk/addressbook.tcl
. This is a Tcl/Tk
script that allows users to load an existing address book or
create a new one, add, edit, and remove entries, and save (or save as),
the new or modified addressbook. Despite the application's simplicity,
it has all the key features that most standard applications have: a menu
bar with pull down menus, a toolbar, and a central area—in this
case showing a table. It pop-up modal dialogs for adding and editing
items. All the ideas and practices that you
learn to test this application can easily be adapted to your own
applications. And naturally, the User Guide (Chapter 5) has many
more examples and shows how to test lots of Tk-specific features,
as well as all the standard editing widgets.
To execute the Tk Address Book application you will need to have a working Tcl/Tk installation on your system.
The screenshot shows the application in action with a user adding a new name and address.
The Tk addressbook.tcl
example.
![]() | Using the Examples |
---|---|
The first time you try running a test for one of the example AUTs you might get a fatal error that begins “Squish couldn't find the AUT to start...”. If this occurs, click the toolbar button, and in the Application Under Test (AUT) section choose the AUT from the combobox if it is available, or click the button and choose the AUT's executable via the file open dialog that pops up. (Some versions of Squish will automatically pop up this dialog if no AUT is specified.) This only needs to be done once per example AUT. (This doesn't arise when testing your own AUTs.) On *nix systems, the executable may be a script such as
|
In the following sections we will create a test suite and then create some tests, but first we will very briefly review some key Squish concepts.
To perform testing, two things are required:
The Application Under Test (AUT), and
a test script that exercises the AUT.
One fundamental aspect of Squish's approach is that the AUT and the test script that exercises it are always executed in two separate processes. This ensures that even if the AUT crashes, it should not crash Squish. (In such cases the test script will fail gracefully and log an error message.) In addition to insulating Squish and test scripts from AUT crashes, running the AUT and the test script in separate processes brings other benefits. For example, it makes it easier to store the test scripts in a central location, and it also makes it possible to perform remote testing on different machines and platforms. The ability to do remote testing is particularly useful for testing AUTs that run on multiple platforms, and also when testing AUTs that run on embedded devices.
Squish runs a small server (squishserver) that handles the communication between the AUT and the test script. The test script is executed by the squishrunner tool, which in turn connects to the squishserver. The squishserver starts the AUT and injects the Squish hook into it. The hook is a small library that makes the AUT's live running objects accessible and that can communicate with the squishserver. With the hook in place, the squishserver can query AUT objects regarding their state and can execute commands—all on behalf of the squishrunner. And the squishrunner itself requests that the AUT performs whatever actions the test script specifies. All the communication takes place using network sockets which means that everything can be done on a single machine, or the test script can be executed on one machine and the AUT can be tested over the network on another machine.
The following diagram illustrates how the individual Squish tools work together.
From the test engineer's perspective this separation is not noticeable, since all the communication is handled transparently behind the scenes.
Tests can be written and executed using the Squish IDE, in which case the squishserver is started and stopped automatically, and the test results are displayed in the Squish IDE's Test Results view (Section 8.2.18). The following diagram illustrates what happens behind the scenes when the Squish IDE is used.
The Squish tools can also be used from the command line without the Squish IDE—this is useful for those testers who prefer to use their own tools (for example, their favorite editor), and also for performing automatic batch testing (for example, when running regression tests overnight). In these cases, the squishserver must be started manually, and stopped when all the testing is complete (or, if preferred, started and stopped for each test).
![]() | Terminology |
---|---|
The Squish documentation mostly uses the term widget when referring to GUI objects (i.e., buttons, menus, menu items, labels, table controls, etc). Windows users might be more familiar with the terms control and container, but here we use the term widget for both. Similarly, macOS users may be used to the term view; again, we use the term widget for this concept. |
![]() | The Squish Directory |
---|---|
Throughout the manual, we often refer to the |
A test suite is a collection of one or more test cases (tests). Using a test suite is convenient since it makes it easy to share tests scripts and test data between tests.
Here, and throughout the tutorial, we will start by describing how to do things using the IDE, with the information for command line users following.
To begin with start up the Squish IDE, either by clicking or double-clicking the squishide icon, or by launching squishide from the taskbar menu or by executing squishide on the command line—whichever you prefer and that is suitable for the platform you are using. Once Squish starts up you might be greeted with a Welcome Page in case you're starting the squishide for the first time. Click the Workbench button in the upper right to dismiss it. Then, the squishide will look similar to the screenshot—but probably slightly different depending on the windowing system, colors, fonts, and theme that you use, and so on.
Now, click
| to pop-up the New Test Suite wizard shown below.
Enter a name for your test suite and choose the folder where you want
the test suite to be stored. In the screenshot we have called the test
suite suite_tcl
and will put it inside the
addressbook
folder. (For your own tests you might
use a more meaningful name such as "suite_addressbook"; we chose
"suite_tcl" because the tutorial contains test suites for each scripting
language that Squish supports.)
Naturally, you can choose whatever name and folder you prefer. Once the
details are complete, click to go on to the
Toolkit (or Scripting Language) page.
If you get this wizard page, click the toolkit your AUT uses. For this example, we must click Tk since we are testing a Tk application. Then click to go to the Scripting Language page.
Choose whichever scripting language you want—the only constraint is that you can only use one scripting language per test suite. Naturally, if you are a Tcl/Tk programmer, Tcl would be your choice here. Having chosen a scripting language, click
once more to get to the wizard's last page.
If you are creating a new test suite for an AUT that Squish already
knows about, simply click the combobox to drop-down the list of AUTs and
choose the one you want. If the combobox is empty or your AUT isn't
listed, click the button to the right
of the combobox—this will pop-up a file open dialog from which you
can choose your AUT. Once you have chosen the AUT, click
and Squish will create a sub-folder with
the same name as the test suite, and will create a file inside that
folder called suite.conf
that contains the test
suite's configuration details. Squish will also register the AUT with
the squishserver. The wizard will then close and Squish's IDE will
look similar to the screenshot below.
We are now ready to start creating tests. Read on to learn how to create test suites without using the IDE, or skip ahead to Recording Tests and Verification Points (Section 4.10.1.3) if you prefer.
![]() | For command-line users |
---|---|
To create a new test suite from the command line, three steps are necessary: first, create a directory for the test suite; second, create a test suite configuration file; and third, register the AUT with squishserver.
|
We are now ready to record our first test.
Squish records tests using the desired scripting language. Once a
test has been recorded we can () the test and
Squish will faithfully repeat all the actions that we performed when
recording the test, but without the human pauses that happened during
recording. It is also possible—and very
common—to edit recorded tests, or to copy parts of recorded tests
into manually created tests, as we will see later on in the tutorial.
Recordings are made into existing test cases. We begin by
creating a New Script Test Case.
There are two ways we can do this. One way
is to click | . This will pop up the New Squish Test Case wizard (Section 8.3.10)—simply enter the name
for the test case and then click . Another
way is to click the () (to
the right of the Test Cases label in the
Test Suites view); this will create a new test
case with a default name (which you can easily change). Use one of these
methods and give the new test case the name “tst_general”.
Squish automatically creates a sub-folder inside the test suite's
folder with this name and also a test file, for example
test.tcl
. (If we had chosen JavaScript as our
scripting language the file would be called
test.js
, and correspondingly for Perl, Ruby, or Tcl.)
![]() | Note |
---|---|
![]()
If you get a sample |
To make the test script file (e.g., test.tcl
) appear
in an Editor view (Section 8.2.6), click—or double-click
depending on the
| | setting—the test case.
(Incidentally, the checkboxes next to each test case are used to control which ones are
run when the () is clicked; we
can always run a single test case by clicking its (
).) Initially, the
script is empty. If we were to create a test manually (as we will do
later on in the tutorial), we must create a
main
function. The name "main" is special to
Squish—tests may contain as many functions and other code as we
like (providing it is legal for the scripting language), but when the
test is executed (i.e., run), Squish always executes the
main
function. This is actually very convenient since it
means we are free to create other functions, import libraries, and so
on, without problems. It is also possible to share commonly used code
between test scripts—this is covered in the User Guide (Chapter 5). (In fact, two other function names are special
to Squish, cleanup
and init
; see Tester-Created Special Functions (Section 6.1) for details.)
Once the new empty test case has been created, we are now free to write test code manually, or to record a test. If we choose to record, we can either replace all the test's code with the recorded code, or record a snippet into the middle of some existing test code.
![]() | For command-line users |
---|---|
Creating a new test case from the command line is an easy two-step process: first, create a test case directory; and second, create an empty test case script.
|
Before we dive into recording let's briefly review our very simple test scenario:
Open the MyAddresses.adr
address file.
Navigate to the second address and then add a new name and address.
Navigate to the fourth address (that was the third address) and change the surname field.
Navigate to the first address and remove it.
Verify that the first address is now the new one that was added.
We are now ready to record our first test. Click the )
to the right of the
tst_general
test case shown in the Test Suites view (Section 8.2.19)'s Test Cases list.
This will cause Squish to run the AUT so that we can interact with it. Once
the AUT is running, perform the following actions—and don't worry about how
long it takes since Squish doesn't record idle time:
From the main menu bar, click
Filename, and type into it: MyAddresses.adr
,
then click the button.
Click the second row, then from the menu bar, click
| , then in the Add dialog's first line edit type in "Jane". Now click (or tab to) the second line edit and type in "Doe". Continue similarly, to set an email address of "jane.doe@nowhere.com" and a phone number of "555 123 4567". Don't worry about typing mistakes—just backspace delete as normal and fix them. Finally, click the button. There should now be a new second address with the details you typed in.Click the fourth row, then click
| to pop up the Edit dialog. In this dialog change the surname to "Doe" and click to confirm the change.Now click the first row, then click
| . The first row should be gone, so your "Jane Doe" entry should now be the first one.Click the first row, then click
| to pop up the Edit dialog (which should show the "Jane Doe" entry's details).Now click the Squish Control Bar Window (Section 8.1.3) (the second button from the left) and select .
toolbar button in theThis will make the Squish IDE appear.
In the Application
Objects view expand the addressbook.tcl
Tk_Window object,
and then the dialog
object. Click the
“forename” object to make its properties appear in the
Properties view (Section 8.2.12), and then check the
getvalue
property's checkbox. Now click the
“surname” object and check its getvalue
property.
Finally, click the Verification Point Creator view (Section 8.2.22)) to have the forename and surname verifications for the first row inserted into the recorded test script. (See the screenshot below.) Once the verification points are inserted the Squish IDE's window will be hidden again and the Control Bar window and the AUT will be back in view.
button (at the bottom of theWe've now completed the test plan, so in the AUT, click
to close the Edit dialog, then click | , then click in the message box, since we don't want to save any changes.Once we quit the AUT, the recorded test will appear in Squish's IDE as the screenshot illustrates. (Note that the exact code that is recorded will vary depending on how you interact. For example, you might invoke menu options by clicking them or by using key sequences—it doesn't matter which you use, but since they are different, Squish will record them differently.)
If the recorded test doesn't appear, click (or double-click depending on
your platform and settings) the tst_general
test
case; this will make Squish show the test's
test.tcl
file in an editor window as shown in the
screenshot.
Now that we've recorded the test, we can play it back, i.e., run it. Now, the two verifications we put in will be checked on play back as the screenshot shows.
Inserting verification points during test recording is very convenient. Here we inserted two in one go, but we can insert as many as we like as often as we like during the test recording process. However, sometimes we might forget to insert one, or later we might want to insert a new one. We can insert additional verifications into a recorded test script, as we will see in the next section, Inserting Additional Verification Points (Section 4.10.1.4).
Before going further, we will look at how to record a test from the command line. Then we will see how to run a test, and we will also look at some of the code that Squish generated to record the test and discuss some of its features.
![]() | For command-line users |
---|---|
First and foremost, the squishserver must always be running when recording or running a test. This is handled automatically by the Squish IDE, but for command line users the squishserver must be started manually. (See squishserver (Section 7.4.4) for further details.) To record a test from the command line we execute the squishrunner program and specify the test suite we want to record inside and the name we want to give to the test case. For example (assuming we are in the directory that contains the test suite's directory): squishrunner --testsuite suite_tcl --record tst_general --useWaitFor
It is always best to record using the |
To run a test case in the IDE just click the ) that
appears when the test case is selected in the Test Suites view (Section 8.2.19). This will cause Squish to run
the AUT and replay every action (omitting human idle time, but allowing
just enough time for the GUI toolkit to keep up). It is worth trying out
since it has quite an impressive effect, especially if you haven't seen
it in action before.
When we have two or more test cases, we can run them individually by
selecting the test case we want to run and then clicking its
) or we can
run them all (as long as they're checked) by clicking the
(
) (which is above
and slightly to the left of the (
)).
![]() | For command-line users |
---|---|
As noted earlier, the squishserver must always be running when
recording or running a test, or the To play back a recorded test from the command line we use the squishrunner program and specify the test suite our recorded script is in and optionally, the test case we want to play. For example (assuming we are in the directory that contains the test suite's directory): squishrunner --testsuite suite_tcl --testcase tst_general --local |
If you look at the code in the screenshot (or the code snippet shown
below) you will see that it consists of lots of waitForObject
calls as parameters to various
other calls such as activateItem
,
clickItem
,
clickButton
,
and type
, as well as many calls to the
waitForObjectItem
. The waitForObject
and waitForObjectItem
functions wait until a GUI object
is ready to be interacted with (i.e., becomes visible and enabled), and
is then followed by some function that interacts with the object. The
typical interactions are activate (pop-up) a menu, click a menu option
or a button, or type in some text. (For a complete overview of
Squish's script commands see the User Guide (Chapter 5), the
API Reference Manual (Chapter 6), and the Tools Reference Manual (Chapter 7). Objects are
identified by names that Squish generates. (See How to Identify and Access Objects (Section 5.1) for full details.)
The generated code is about 80 lines of code. Here's an extract that just shows how Squish records clicking the Edit menu's Add option, typing in Jane Doe's details into the Add dialog, and clicking OK at the end to close the dialog and update the table.
![]() | Scripting Language Support |
---|---|
Although the screenshots only show the Tcl test suite in action, for the code snippets quoted here and throughout the tutorial, we show the code for all the scripting languages that Squish supports. In practice you would normally only use one of them of course, so feel free to just look at the snippets in the language you are interested in and skip the others. (In the HTML version of this manual you can use a combobox next to each code snippet to select the language you wish to see—this will hide the code snippets in other languages.) |
waitForObjectItem ":addressbook\\.tcl.#menuBar" "Edit" invoke activateItem ":addressbook\\.tcl.#menuBar" "Edit" waitForObjectItem ":addressbook\\.tcl.#menuBar.#edit" "Add..." invoke activateItem ":addressbook\\.tcl.#menuBar.#edit" "Add..." invoke type [waitForObject ":addressbook\\.tcl.dialog.forename"] "<Shift_L>" invoke type [waitForObject ":addressbook\\.tcl.dialog.forename"] "J" 17 invoke type [waitForObject ":addressbook\\.tcl.dialog.forename"] "ane" invoke type [waitForObject ":addressbook\\.tcl.dialog.forename"] "<Tab>" invoke type [waitForObject ":addressbook\\.tcl.dialog.surname"] "<Shift_L>" invoke type [waitForObject ":addressbook\\.tcl.dialog.surname"] "D" 17 invoke type [waitForObject ":addressbook\\.tcl.dialog.surname"] "oe" invoke type [waitForObject ":addressbook\\.tcl.dialog.surname"] "<Tab>" invoke type [waitForObject ":addressbook\\.tcl.dialog.phone"] "555" invoke type [waitForObject ":addressbook\\.tcl.dialog.phone"] "<space>" invoke type [waitForObject ":addressbook\\.tcl.dialog.phone"] "123" invoke type [waitForObject ":addressbook\\.tcl.dialog.phone"] "<space>" invoke type [waitForObject ":addressbook\\.tcl.dialog.phone"] "4567" invoke type [waitForObject ":addressbook\\.tcl.dialog.phone"] "<Tab>" invoke type [waitForObject ":addressbook\\.tcl.dialog.email"] "jane" invoke type [waitForObject ":addressbook\\.tcl.dialog.email"] "<period>" invoke type [waitForObject ":addressbook\\.tcl.dialog.email"] "doe" invoke type [waitForObject ":addressbook\\.tcl.dialog.email"] "<Shift_L>" invoke type [waitForObject ":addressbook\\.tcl.dialog.email"] "<at>" 17 invoke type [waitForObject ":addressbook\\.tcl.dialog.email"] "nowhere" invoke type [waitForObject ":addressbook\\.tcl.dialog.email"] "<period>" invoke type [waitForObject ":addressbook\\.tcl.dialog.email"] "com" invoke clickButton [waitForObject ":addressbook\\.tcl.dialog.buttonarea.ok"]
waitForObjectItem(":addressbook\\.tcl.#menuBar", "Edit") activateItem(":addressbook\\.tcl.#menuBar", "Edit") waitForObjectItem(":addressbook\\.tcl.#menuBar.#edit", "Add...") activateItem(":addressbook\\.tcl.#menuBar.#edit", "Add...") type(waitForObject(":addressbook\\.tcl.dialog.forename"), "<Shift_L>") type(waitForObject(":addressbook\\.tcl.dialog.forename"), "J", 17) type(waitForObject(":addressbook\\.tcl.dialog.forename"), "ane") type(waitForObject(":addressbook\\.tcl.dialog.forename"), "<Tab>") type(waitForObject(":addressbook\\.tcl.dialog.surname"), "<Shift_L>") type(waitForObject(":addressbook\\.tcl.dialog.surname"), "D", 17) type(waitForObject(":addressbook\\.tcl.dialog.surname"), "oe") type(waitForObject(":addressbook\\.tcl.dialog.surname"), "<Tab>") type(waitForObject(":addressbook\\.tcl.dialog.phone"), "555") type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<space>") type(waitForObject(":addressbook\\.tcl.dialog.phone"), "123") type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<space>") type(waitForObject(":addressbook\\.tcl.dialog.phone"), "4567") type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<Tab>") type(waitForObject(":addressbook\\.tcl.dialog.email"), "jane") type(waitForObject(":addressbook\\.tcl.dialog.email"), "<period>") type(waitForObject(":addressbook\\.tcl.dialog.email"), "doe") type(waitForObject(":addressbook\\.tcl.dialog.email"), "<Shift_L>") type(waitForObject(":addressbook\\.tcl.dialog.email"), "<at>", 17) type(waitForObject(":addressbook\\.tcl.dialog.email"), "nowhere") type(waitForObject(":addressbook\\.tcl.dialog.email"), "<period>") type(waitForObject(":addressbook\\.tcl.dialog.email"), "com") clickButton(waitForObject(":addressbook\\.tcl.dialog.buttonarea.ok"))
waitForObjectItem(":addressbook\\.tcl.#menuBar", "Edit"); activateItem(":addressbook\\.tcl.#menuBar", "Edit"); waitForObjectItem(":addressbook\\.tcl.#menuBar.#edit", "Add..."); activateItem(":addressbook\\.tcl.#menuBar.#edit", "Add..."); type(waitForObject(":addressbook\\.tcl.dialog.forename"), "<Shift_L>"); type(waitForObject(":addressbook\\.tcl.dialog.forename"), "J", 17); type(waitForObject(":addressbook\\.tcl.dialog.forename"), "ane"); type(waitForObject(":addressbook\\.tcl.dialog.forename"), "<Tab>"); type(waitForObject(":addressbook\\.tcl.dialog.surname"), "<Shift_L>"); type(waitForObject(":addressbook\\.tcl.dialog.surname"), "D", 17); type(waitForObject(":addressbook\\.tcl.dialog.surname"), "oe"); type(waitForObject(":addressbook\\.tcl.dialog.surname"), "<Tab>"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "555"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<space>"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "123"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<space>"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "4567"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<Tab>"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "jane"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "<period>"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "doe"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "<Shift_L>"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "<at>", 17); type(waitForObject(":addressbook\\.tcl.dialog.email"), "nowhere"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "<period>"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "com"); clickButton(waitForObject(":addressbook\\.tcl.dialog.buttonarea.ok"));
waitForObjectItem(":addressbook\\.tcl.#menuBar", "Edit"); activateItem(":addressbook\\.tcl.#menuBar", "Edit"); waitForObjectItem(":addressbook\\.tcl.#menuBar.#edit", "Add..."); activateItem(":addressbook\\.tcl.#menuBar.#edit", "Add..."); type(waitForObject(":addressbook\\.tcl.dialog.forename"), "<Shift_L>"); type(waitForObject(":addressbook\\.tcl.dialog.forename"), "J", 17); type(waitForObject(":addressbook\\.tcl.dialog.forename"), "ane"); type(waitForObject(":addressbook\\.tcl.dialog.forename"), "<Tab>"); type(waitForObject(":addressbook\\.tcl.dialog.surname"), "<Shift_L>"); type(waitForObject(":addressbook\\.tcl.dialog.surname"), "D", 17); type(waitForObject(":addressbook\\.tcl.dialog.surname"), "oe"); type(waitForObject(":addressbook\\.tcl.dialog.surname"), "<Tab>"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "555"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<space>"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "123"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<space>"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "4567"); type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<Tab>"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "jane"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "<period>"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "doe"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "<Shift_L>"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "<at>", 17); type(waitForObject(":addressbook\\.tcl.dialog.email"), "nowhere"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "<period>"); type(waitForObject(":addressbook\\.tcl.dialog.email"), "com"); clickButton(waitForObject(":addressbook\\.tcl.dialog.buttonarea.ok"));
waitForObjectItem(":addressbook\\.tcl.#menuBar", "Edit") activateItem(":addressbook\\.tcl.#menuBar", "Edit") waitForObjectItem(":addressbook\\.tcl.#menuBar.#edit", "Add...") activateItem(":addressbook\\.tcl.#menuBar.#edit", "Add...") type(waitForObject(":addressbook\\.tcl.dialog.forename"), "<Shift_L>", 0) type(waitForObject(":addressbook\\.tcl.dialog.forename"), "J", 1) type(waitForObject(":addressbook\\.tcl.dialog.forename"), "ane", 0) type(waitForObject(":addressbook\\.tcl.dialog.forename"), "<Tab>", 0) type(waitForObject(":addressbook\\.tcl.dialog.surname"), "<Shift_L>", 0) type(waitForObject(":addressbook\\.tcl.dialog.surname"), "D", 1) type(waitForObject(":addressbook\\.tcl.dialog.surname"), "oe", 0) type(waitForObject(":addressbook\\.tcl.dialog.surname"), "<Tab>", 0) type(waitForObject(":addressbook\\.tcl.dialog.phone"), "555", 0) type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<space>", 0) type(waitForObject(":addressbook\\.tcl.dialog.phone"), "123", 0) type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<space>", 0) type(waitForObject(":addressbook\\.tcl.dialog.phone"), "4567", 0) type(waitForObject(":addressbook\\.tcl.dialog.phone"), "<Tab>", 0) type(waitForObject(":addressbook\\.tcl.dialog.email"), "jane", 0) type(waitForObject(":addressbook\\.tcl.dialog.email"), "<period>", 0) type(waitForObject(":addressbook\\.tcl.dialog.email"), "doe", 0) type(waitForObject(":addressbook\\.tcl.dialog.email"), "<Shift_L>", 0) type(waitForObject(":addressbook\\.tcl.dialog.email"), "<Shift_L>", 0) type(waitForObject(":addressbook\\.tcl.dialog.email"), "<at>", 1) type(waitForObject(":addressbook\\.tcl.dialog.email"), "nowhere", 0) type(waitForObject(":addressbook\\.tcl.dialog.email"), "<period>", 0) type(waitForObject(":addressbook\\.tcl.dialog.email"), "com", 0) clickButton(waitForObject(":addressbook\\.tcl.dialog.buttonarea.ok"))
While recording the test above, the tester used the keyboard to tab from one text field to another and clicked Squish will have recorded different actions.
using the mouse, rather than with a key press. If the tester had clicked the button any other way (for example, by tabbing to the OK button and pressing the spacebar), the outcome should be the same, but![]() | Object Names |
---|---|
Notice that some object names are strings that begin with a colon. These are symbolic names. Squish supports several naming schemes, all of which can be used—and mixed—in scripts. The advantage of using symbolic names is that if the application changes in a way that results in different names being generated, we can simply update Squish's Object Map (which relates symbolic names to real names), and thereby avoid the need to change our test scripts. (See the Object Map (Section 7.11) and the Object Map view (Section 8.2.10) for more about the Object Map.) Unlike other editions of Squish, the real names used in Squish/Tk are hierarchical instead of multi-property names. This means that the object name represents a path of class names in the object tree that stores the GUI in memory. Symbolic names are based on this hierarchy also. |
Now that we have seen how to record and play back a test and have seen the code that Squish generates, let's go a step further and make sure that at particular points in the test's execution certain conditions hold.
In the previous section, we saw how easy it is to insert verification
points during the recording of test scripts. Verification points can
also be inserted into existing test scripts, either by setting a
breakpoint and using the Squish IDE, or simply by editing a test script and
putting in calls to Squish's test functions such as test.compare
and test.verify
.
Squish supports four kinds of verification points: those that verify that object properties have particular values—known as "Object Property Verifications"; those that verify that an entire table has the contents we expect—known as "Table Verifications"; those that verify that two images match—known as "Screenshot Verifications"; and a hybrid verification type that includes properties and screenshots from multiple objects, known as "Visual Verifications". The most commonly used kind is object property verifications, and it is these that we will cover in the tutorial. For further reading, see How to Create and Use Verification Points (Section 5.22)).
Regular (non-scriptified) property
verification points are stored as XML files in the test case or test suite
resources, and contain the value(s) that need to be passed to
test.compare()
. These verification points can be reused across test
cases, and can verify many values in a single line of script code.
Scriptified property verification points are direct
calls to the test.compare
function, with two
arguments—the value of a particular property for a particular
object, and an expected value. We can manually insert calls to the test.compare
function in a recorded or hand
written script, or we can get Squish to insert them for us using scriptified
verification points. In the previous section we showed how to use the Squish IDE to insert
verifications during recording. Here we will first show how to use the
Squish IDE to insert verifications into an existing test script, and then we
will show how to insert a verification by hand.
Before asking Squish to insert verification points, it is best to make
sure that we have a list of what we want to verify and when. There are many
potential verifications we could add to the tst_general
test case, but since our concern here is simply to show how to do it, we
will only do two—we will verify that the "Jane Doe"
entry's email address and phone number match the ones entered, and put
the verifications immediately after the ones we inserted during
recording.
To insert a verification point using the IDE, we start by putting a break point in the script (whether recorded or manually written—it does not matter to Squish), at the point where we want to verify.
The Squish IDE showing the tst_general test case with a breakpoint
As the above screenshot shows, we have set a breakpoint at line 71. This is done simply by right-clicking the line number and then clicking the Squish IDE during recording. Our additional verifications will follow them. (Note that your line number may be different if you recorded the test in a different way, for example, using keyboard shortcuts rather than clicking menu items.)
menu item in the context menu. We chose this line because it follows the script lines where the first address is removed, and where we have invoked the Edit dialog. So at this point (just before invoking the File menu to close the application), the first address (and the one shown in the Edit dialog) should be that of "Jane Doe". The screenshot shows the verifications that were entered using the
Having set the breakpoint, we now run the test as usual by clicking the
) or by clicking the
| menu option. Unlike a normal test run the
test will stop when the breakpoint is reached (i.e., at line 71, or at
whatever line you set), and Squish's main window will reappear (which
will probably obscure the AUT). At this point the Squish IDE will
automatically switch to the Squish Test Debugging Perspective (Section 8.1.2.3).
![]() | Perspectives and Views |
---|---|
The Squish IDE works just like the Eclipse IDE. If you aren't used to Eclipse it is crucial to understand one key concept: Views and Perspectives. In Eclipse (and therefore in the Squish IDE), a View is essentially a child window (perhaps a dock window, or a tab in an existing window). And a Perspective is a collection of Views arranged together. Both are accessible through the menu. The Squish IDE is supplied with three Perspectives—the Squish Test Management Perspective (Section 8.1.2.2) (which is the Perspective that the Squish IDE starts with, and the one we have seen in all previous screenshots), Squish Test Debugging Perspective (Section 8.1.2.3), and Squish Spy Perspective (Section 8.1.2.1). You can change these Perspectives to include additional Views (or to get rid of any Views that you don't want), and you can create your own Perspectives with exactly the Views you want. So if your windows change dramatically it just means that the Perspective changed; you can always use the menu to change back to the Perspective you want. In practice, Squish will automatically change perspective to reflect the current situation, so it isn't really necessary to change perspective manually. |
As the screenshot below shows, when Squish stops at a breakpoint the Squish IDE automatically changes to the Squish Test Debugging Perspective (Section 8.1.2.3). The perspective shows the Variables view (Section 8.2.21), the Editor view (Section 8.2.6), the Debug view (Section 8.2.5), the Application Objects view (Section 8.2.1), and the Properties view (Section 8.2.12), Methods view (Section 8.2.9), and Test Results view (Section 8.2.18).
To insert a verification point we can expand items in the Application
Objects view until we find the object we want to verify. In this example
we want to verify the table's first row's texts, so
we expand the addressbook.tcl
Tk_Window object, and
then the dialog
object to find the items we are interested
in: in this case the phone
and email
items.
Once we click an object its
properties are shown in the Properties view (Section 8.2.12) as
the screenshot shows.
The normal Squish Test Management Perspective (Section 8.1.2.2) can be returned to at any time by choosing it from the menu (or by clicking its toolbar button), although the Squish IDE will automatically return to it if you stop the script or run it to completion.
Here, we can see that the getvalue property of the forename
item has the value “Jane”; we already have a verification for
this that we inserted during recording. Scroll down so that you can see
the phone item. To make sure that
this is verified every time the test is run, click the
phone item in the Application Objects view (Section 8.2.1) to
make its properties appear, and then click the getvalue
property to check its check box. When we check it the Verification Point Creator view (Section 8.2.22) appears as shown in the
screenshot.
At this point the verification point has not been added to the test script. We could easily add it by clicking the button. But before doing that we'll add one more thing to be verified.
Scroll down and click the email item in the
Application Objects view (Section 8.2.1); then click its
getvalue
property. Now both verifications will appear in
the Verification Point Creator view (Section 8.2.22) as the screenshot
shows.
We have now said that we expect these properties to have the values shown, that is, an email address of “jane.doe@nowhere.com” and phone number of “555 123 4567”. We must click the button to actually insert the verification point, so do that now.
We don't need to continue running the test now, so we can either stop running the test at this point (by clicking the
toolbar button), or we can continue (by clicking the button).
Once we have finished inserting verifications and stopped or finished
running the test we should now disable the break point. Just right click
the break point and click the ) button. This
time we will get some additional test results—as the screenshot
shows—one of which we have expanded to show its details. (We have
also selected the lines of code that Squish inserted to perform the
verifications—notice that the code is structurally identical to
the code inserted during recording.)
These particular verification points generate four tests comparing the forename, surname, phone number, and email of the newly inserted entry.
Another way to insert verification points is to insert them in code
form. In theory we can just add our own calls to Squish's test
functions such as test.compare
and test.verify
anywhere we like in an existing
script. In practice it is best to make sure that Squish knows about
the objects we want to verify first so that it can find them when the
test is run. This involves a very similar procedure as using the Squish IDE.
First we set a breakpoint where we intend adding our verifications. Then
we run the test script until it stops. Next we navigate in the
Application Objects view (Section 8.2.1) until we find the
object we want to verify. At this point it is wise to right-click the
object we are interested in and click the context menu option. This will ensure that Squish
can access the object. Then right click again and click the
context
menu option—this gives us the name of the object that Squish
will use to identify it. Now we can edit the test script to add in our
own verification and finish or stop the execution. (Don't forget to
disable the break point once it isn't needed any more.)
Although we can write our test script code to be exactly the same style as the automatically generated code, it is usually clearer and easier to do things in a slightly different style, as we will explain in a moment.
For our manual verifications we want to check the number of addresses
present in the table after reading in the
MyAddresses.adr
file, then after the new address is
added, and finally after the first address is removed. The screenshot
shows three of the lines of code we entered to get one of these three
verifications, plus the results of running the test script.
When writing scripts by hand, we use Squish's test
module's functions to verify conditions at certain points during our
test script's execution. As the screenshot (and the code snippets below)
show, we begin by making sure that the object we are interested
in is available. Using the waitForObject
function is
standard practice for manually written test scripts. This function waits
for the object to be available (i.e., visible and enabled).
(Otherwise it times out and raises a
catchable exception.)
Once we know that the object is available we use the tcleval
function to interact with the object—in
this case to retrieve the number of rows (which the table reports as its
“size”). We then compare the actual number of rows with the
expected number using the
test.compare
function. Notice that Squish functions normally require object names
prefixed by the AUT's name, but the tcleval
function does not need the prefix.
Here is the code we entered manually for the first verification for all
the scripting languages that Squish supports. Naturally, you only need
to look at the code for the language that you will be using for your own
tests. It is usually more convenient in Tcl to use the test.compare
function, but we can of course use
any of Squish's test
functions, such as the test.verify
function which we use for the other
scripting languages.
waitForObject ":addressbook\\.tcl.view.tree" set rows [invoke tcleval ".view.tree size"] test compare $rows "125"
waitForObject(":addressbook\\.tcl.view.tree") rows = tcleval(".view.tree size") test.verify(rows == "125")
waitForObject(":addressbook\\.tcl.view.tree"); var rows = tcleval(".view.tree size"); test.verify(parseInt(rows) == 125);
waitForObject(":addressbook\\.tcl.view.tree"); my $rows = tcleval(".view.tree size"); test::verify($rows eq 125);
waitForObject(":addressbook\\.tcl.view.tree") rows = tcleval(".view.tree size") Test.verify(rows == "125")
The coding pattern is very simple: we ensure that the object
we are interested in is available, access its properties using
the tcleval
function, and then verify
the results using Squish's verification functions.
We will see more examples of manually written code shortly, in the Creating Tests by Hand (Section 4.10.1.5) section, and further examples are in the User Guide (Chapter 5).
For complete coverage of verification points, see How to Create and Use Verification Points (Section 5.22) in the User Guide (Chapter 5).
After each test run finishes, the test results—including those for the verification points—are shown in the Test Results view at the bottom of the Squish IDE.
This is a detailed report of the test run and would also contain details of any failures or errors, etc. If you click on a Test Results item, the Squish IDE highlights the script line which generated the test result. And if you expand a Test Results item, you can see additional details of the test.
Now that we have seen how to record a test and modify it by inserting verification points, we are ready to see how to create tests manually. The easiest way to do this is to modify and refactor recorded tests, although it is also perfectly possible to create manual tests from scratch.
Potentially the most challenging part of writing manual tests is to use the right object names, but in practice, this is rarely a problem. We can either copy the symbolic names that Squish has already added to the Object Map when recording previous tests, or we can copy object names directly from recorded tests. And if we haven't recorded any tests and are starting from scratch we can use the Spy. We do this by clicking the toolbar button. This starts the AUT and switches to the Squish Spy Perspective (Section 8.1.2.1). We can then interact with the AUT until the object we are interested in is visible. Then, inside the Squish IDE we can navigate to the object in the Application Objects view and use the context menu to both Add to Object Map (so that Squish will remember it) and to Copy Real Name to clipboard (so that we can paste it into our test script). When finished, we can click the toolbar button to terminate the AUT and return Squish to the Squish Test Management Perspective (Section 8.1.2.2).
To see the object map entry for a particular symbolic name, you can highlight
it in the Editor view, right click, and select Open Symbolic Name.
We can also see the Object Map by clicking the () button (see also, the
Object Map view (Section 8.2.10)). Every application object that
Squish interacts with is listed here, either as a top-level object, or
as a child object (the view is a tree view). We can retrieve the
object names used for it by right-clicking the
object map entry and clicking Copy Object Name or,
if a string is desired instead, Copy Real Name.
This is useful for when we want to modify existing test scripts or
when we want to create test scripts from scratch, as we will see later
on in the tutorial.
Squish's Object Map
Suppose we want to test the AUT's Add functionality by adding three new names and addresses. We could record such a test, but it may be easier to code things manually. The steps we need the test script to do are: first click File|New to create a new address book, then for each new name and address:
Click Edit|Add
Fill in the details
Click
Finally, click File|Quit without saving.
We also want to verify at the start that there are no rows of data and at the end that there are three rows. We will refactor as we go, to make our code as neat and modular as possible.
First we must create a new empty test case. Click
tst_adding
. Squish will automatically create an
empty test.tcl
(or test.js
, and
so on) file.
Command line users can simply create a tst_adding
directory inside the test suite's directory and create and edit the
test.tcl
file (or test.js
and
so on) within that directory.
The first thing we need is a way to start the AUT and then invoke a menu
option. Here are the first few lines from the
recorded tst_general
script:
proc main {} { startApplication "addressbook.tcl" waitForObjectItem ":addressbook\\.tcl.#menuBar" "File" invoke activateItem ":addressbook\\.tcl.#menuBar" "File" waitForObjectItem ":addressbook\\.tcl.#menuBar.#file" "Open..." invoke activateItem ":addressbook\\.tcl.#menuBar.#file" "Open..."
def main(): startApplication("addressbook.tcl") waitForObjectItem(":addressbook\\.tcl.#menuBar", "File") activateItem(":addressbook\\.tcl.#menuBar", "File") waitForObjectItem(":addressbook\\.tcl.#menuBar.#file", "Open...") activateItem(":addressbook\\.tcl.#menuBar.#file", "Open...")
function main() { startApplication("addressbook.tcl"); waitForObjectItem(":addressbook\\.tcl.#menuBar", "File"); activateItem(":addressbook\\.tcl.#menuBar", "File"); waitForObjectItem(":addressbook\\.tcl.#menuBar.#file", "Open..."); activateItem(":addressbook\\.tcl.#menuBar.#file", "Open...");
sub main { startApplication("addressbook.tcl"); waitForObjectItem(":addressbook\\.tcl.#menuBar", "File"); activateItem(":addressbook\\.tcl.#menuBar", "File"); waitForObjectItem(":addressbook\\.tcl.#menuBar.#file", "Open..."); activateItem(":addressbook\\.tcl.#menuBar.#file", "Open...");
def main startApplication("addressbook.tcl") waitForObject(":addressbook\\.tcl.#menuBar") activateItem(":addressbook\\.tcl.#menuBar", "") waitForObjectItem(":addressbook\\.tcl.#menuBar", "File") activateItem(":addressbook\\.tcl.#menuBar", "File") waitForObjectItem(":addressbook\\.tcl.#menuBar.#file", "Open...") activateItem(":addressbook\\.tcl.#menuBar.#file", "Open...")
Notice that the pattern in the code is simple: start the AUT, then wait
for the menu bar, then activate the menu bar; wait for the menu item,
then activate the menu item. In both cases we have used the
waitForObjectItem
function. This function
is used for a multi-valued objects (such as lists, tables,
trees—or in this case, a menubar and a menu), and allows us to
access the object's items (which are themselves objects of course), by
passing the name of the object containing the item and the item's text
as arguments.
![]() | Note |
---|---|
It may seem a waste to copy our functions into
|
Sometimes the AUT will appear to freeze during test execution. When this happens, just wait for Squish to time out the AUT (about 20 seconds), and then it will pop up an Object Not Found dialog (Section 8.3.14), indicating an error like this:
Error Script Error Apr 9, 2010 Detail LookupError: Item 'New...' in object ':addressbook.tcl' not found or ready. Called from: C:\squish\examples\tk\addressbook\suite_py\tst_adding\test.tcl: 18 Location C:\squish\examples\tk\addressbook\suite_py\tst_adding\test.tcl:3
This usually means that Squish doesn't have an object with the given
name, or property values, in the Object Map. From here, we can
Pick a new object, Debug,
Throw Error or, after picking
a new object, Retry. In addition to the Spy's Object Picker () we
can use the Application Objects view (Section 8.2.1) to locate the
objects we are interested in and use a context menu action to
Add to the Object Map. In general, recording a dummy test
case that interacts with all of the relevant AUT objects is a much more
efficient way to initially populate the Object Map.
We've spent a bit of time on the issue of naming since it is probably the part of writing scripts that leads to the most error messages (usually of the "object ... not found" kind shown above.) Once we have identified the objects we are going to access in our tests, writing test scripts using Squish is very straightforward. And of course you can almost certainly use the scripting language you are most familiar with since Squish supports the most popular ones available.
We are now almost ready to write our own test script. It is probably
easiest to begin by recording a dummy test. So click
tst_dummy
. Then click the dummy test case's
(). Once the AUT starts, click File|New, then
click the (empty) table, then click Edit|Add and add an
item, then press Return or click . Finally,
click File|Quit to finish, and say No to saving changes.
Replay this test just to confirm that everything works okay. The sole purpose of this is to make sure that Squish adds the necessary names to the Object Map since it is probably quicker to do it this way than to use the Spy for every object of interest. After replaying the dummy test you can delete it if you want to.
proc main {} { startApplication "addressbook.tcl" invokeMenuItem "File" "New" verifyRows 0 set data [list \ [list "Andy" "Beach" "andy.beach@nowhere.com" "555 123 6786"] \ [list "Candy" "Deane" "candy.deane@nowhere.com" "555 234 8765"] \ [list "Ed" "Fernleaf" "ed.fernleaf@nowhere.com" "555 876 4654"] ] for {set i 0} {$i < [llength $data]} {incr i} { addNameAndAddress [lindex $data $i] } verifyRows [llength $data] closeWithoutSaving }
def main(): startApplication("addressbook.tcl") invokeMenuItem("File", "New") verifyRows(0) data = [("Andy", "Beach", "andy.beach@nowhere.com", "555 123 6786"), ("Candy", "Deane", "candy.deane@nowhere.com", "555 234 8765"), ("Ed", "Fernleaf", "ed.fernleaf@nowhere.com", "555 876 4654")] for oneNameAndAddress in data: addNameAndAddress(oneNameAndAddress) verifyRows(len(data)) closeWithoutSaving()
function main() { startApplication("addressbook.tcl"); invokeMenuItem("File", "New"); verifyRows(0); var data = new Array( new Array("Andy", "Beach", "andy.beach@nowhere.com", "555 123 6786"), new Array("Candy", "Deane", "candy.deane@nowhere.com", "555 234 8765"), new Array("Ed", "Fernleaf", "ed.fernleaf@nowhere.com", "555 876 4654")); for (var row = 0; row < data.length; ++row) addNameAndAddress(data[row]); verifyRows(data.length); closeWithoutSaving(); }
sub main { startApplication("addressbook.tcl"); invokeMenuItem("File", "New"); verifyRows(0); my @data = (["Andy", "Beach", "andy.beach\@nowhere.com", "555 123 6786"], ["Candy", "Deane", "candy.deane\@nowhere.com", "555 234 8765"], ["Ed", "Fernleaf", "ed.fernleaf\@nowhere.com", "555 876 4654"]); foreach $oneNameAndAddress (@data) { addNameAndAddress(@{$oneNameAndAddress}); } verifyRows(scalar(@data)); closeWithoutSaving; }
def main startApplication("addressbook.tcl") invokeMenuItem("File", "New") verifyRows(0) data = [["Andy", "Beach", "andy.beach@nowhere.com", "555 123 6786"], ["Candy", "Deane", "candy.deane@nowhere.com", "555 234 8765"], ["Ed", "Fernleaf", "ed.fernleaf@nowhere.com", "555 876 4654"]] data.each do |oneNameAndAddress| addNameAndAddress(oneNameAndAddress) end verifyRows(data.length) closeWithoutSaving end
We begin by starting the application with a call to the startApplication
function. The name we pass as a
string is the name registered with Squish (normally the name of the
executable).
The invokeMenuItem
function is one we have created
specially for this test. It takes a menu name and a menu option name and
invokes the menu option. After using the invokeMenuItem
function to do
File|New, we verify that the table's row count is 0. The
test.verify
function is useful when we simply
want to verify that a condition is true rather than compare two
different values. (For Tcl we usually use the test.compare
function rather than the test.verify
function simply because it is
slightly simpler to use in Tcl.)
Next, we create some sample data and call a custom
addNameAndAddress
to populate the table with the
data using the AUT's Add dialog. Then we again compare the table's row
count, this time to the number of rows in our sample data. And finally
we call a custom closeWithoutSaving
to terminate
the application.
We will now review each of the four supporting functions (and a fifth
helper function), so as to
cover all the code in the tst_adding
test case,
starting with invokeMenuItem
.
proc invokeMenuItem {menu item} { waitForObjectItem ":addressbook\\.tcl.#menuBar" $menu invoke activateItem ":addressbook\\.tcl.#menuBar" $menu set menuName [string tolower $menu] waitForObjectItem ":addressbook\\.tcl.#menuBar.#$menuName" $item invoke activateItem ":addressbook\\.tcl.#menuBar.#$menuName" $item }
def invokeMenuItem(menu, item): waitForObjectItem(":addressbook\\.tcl.#menuBar", menu) activateItem(":addressbook\\.tcl.#menuBar", menu) waitForObjectItem(":addressbook\\.tcl.#menuBar.#%s" % menu.lower(), item) activateItem(":addressbook\\.tcl.#menuBar.#%s" % menu.lower(), item)
function invokeMenuItem(menu, item) { waitForObjectItem(":addressbook\\.tcl.#menuBar", menu); activateItem(":addressbook\\.tcl.#menuBar", menu); var menuText = menu.toLowerCase(); waitForObjectItem(":addressbook\\.tcl.#menuBar.#" + menuText, item); activateItem(":addressbook\\.tcl.#menuBar.#" + menuText, item); }
sub invokeMenuItem { my ($menu, $item) = @_; waitForObjectItem(":addressbook\\.tcl.#menuBar", $menu); activateItem(":addressbook\\.tcl.#menuBar", $menu); my $menuText = lc $menu; waitForObjectItem(":addressbook\\.tcl.#menuBar.#$menuText", $item); activateItem(":addressbook\\.tcl.#menuBar.#$menuText", $item); }
def invokeMenuItem(menu, item) waitForObjectItem(":addressbook\\.tcl.#menuBar", menu) activateItem(":addressbook\\.tcl.#menuBar", menu) waitForObjectItem(":addressbook\\.tcl.#menuBar.#%s" % menu.downcase, item) activateItem(":addressbook\\.tcl.#menuBar.#%s" % menu.downcase, item) end
Symbolic names are based on the hierarchy of an object in its GUI. In Squish/Tk, real names are strings that also represent that hierarchy. Here, we've used the string names that Squish generated, except that for the menus and item, we parametrize the label text.
Once we have identified the object we want to interact with, we use the
waitForObjectItem
function to retrieve a
reference to it and in this case we then apply the
activateItem
function to it. The waitForObjectItem
function pauses Squish until
the specified object and its item are visible and enabled. So, here, we
waited for the menu bar and one of its menu bar items, and then we
waited for a menu bar item and one of its menu items. And as soon as the
waiting is over each time we activate the object and its item using the
activateItem
function.
proc verifyRows {expected_rows} { waitForObject ":addressbook\\.tcl.view.tree" set rows [invoke tcleval ".view.tree size"] test compare $rows "$expected_rows" }
def verifyRows(expected_rows): waitForObject(":addressbook\\.tcl.view.tree") rows = tcleval(".view.tree size") test.verify(cast(rows, int) == expected_rows)
function verifyRows(expected_rows) { waitForObject(":addressbook\\.tcl.view.tree"); var rows = tcleval(".view.tree size"); test.verify(parseInt(rows) == expected_rows); }
sub verifyRows { my $expected_rows = shift; waitForObject(":addressbook\\.tcl.view.tree"); my $rows = tcleval(".view.tree size"); test::verify($rows eq $expected_rows); }
def verifyRows(expected_rows) waitForObject(":addressbook\\.tcl.view.tree") rows = tcleval(".view.tree size") Test.compare(String(rows), String(expected_rows)) end
Rather than duplicate the three lines needed to verify the row count in
two separate places we have packaged the functionality up into a tiny
function. (Note that for the Python version we used the cast
function since Squish has its own
int
object; see also, Squish's Python Modules (Section 6.14.3).)
proc addNameAndAddress {oneNameAndAddress} { invokeMenuItem "Edit" "Add..." set fieldNames [list "forename" "surname" "phone" "email"] for {set field 0} {$field < [llength $fieldNames]} {incr field} { set fieldName [lindex $fieldNames $field] set text [lindex $oneNameAndAddress $field] invoke type [waitForObject ":addressbook\\.tcl.dialog.$fieldName"] $text } invoke clickButton [waitForObject ":addressbook\\.tcl.dialog.buttonarea.ok"] }
def addNameAndAddress(oneNameAndAddress): invokeMenuItem("Edit", "Add...") for fieldName, text in zip(("forename", "surname", "phone", "email"), oneNameAndAddress): type(waitForObject(":addressbook\\.tcl.dialog.%s" % fieldName), text) clickButton(waitForObject(":addressbook\\.tcl.dialog.buttonarea.ok"))
function addNameAndAddress(oneNameAndAddress) { invokeMenuItem("Edit", "Add..."); var fieldNames = new Array("forename", "surname", "phone", "email"); for (var i = 0; i < oneNameAndAddress.length; ++i) { var fieldName = fieldNames[i]; var text = oneNameAndAddress[i]; type(waitForObject(":addressbook\\.tcl.dialog." + fieldName), text); } clickButton(waitForObject(":addressbook\\.tcl.dialog.buttonarea.ok")); }
sub addNameAndAddress { my (@oneNameAndAddress) = @_; invokeMenuItem("Edit", "Add..."); my @fieldNames = ("forename", "surname", "phone", "email"); my $fieldName = ""; for (my $i = 0; $i < scalar(@fieldNames); $i++) { $fieldName = $fieldNames[$i]; my $text = $oneNameAndAddress[$i]; type(waitForObject(":addressbook\\.tcl.dialog.$fieldName"), $text); } clickButton(waitForObject(":addressbook\\.tcl.dialog.buttonarea.ok")); }
def addNameAndAddress(oneNameAndAddress) invokeMenuItem("Edit", "Add...") ["forename", "surname", "email", "phone"].each_with_index do |fieldName, index| text = oneNameAndAddress[index] type(waitForObject(":addressbook\\.tcl.dialog.#{fieldName}"), text) end clickButton(waitForObject(":addressbook\\.tcl.dialog.buttonarea.ok")) end
For each set of name and address data we invoke the
Edit|Add menu option to pop up the Add dialog. Then for
each value received, we populate the
appropriate field by waiting for the relevant line edit to be ready and
then typing in the text using the type
function. We got the type
function call by
copying it from the recorded tst_general
test and
parametrizing it by the field name and the text. And at the end, we
click the dialog's , again using code we copied from the
recorded tst_general
test case.
proc closeWithoutSaving {} { invokeMenuItem "File" "Quit" invoke clickButton [waitForObject ":addressbook\\.tcl.__tk__messagebox.no"] }
def closeWithoutSaving(): invokeMenuItem("File", "Quit") clickButton(waitForObject(":addressbook\\.tcl.__tk__messagebox.no"))
function closeWithoutSaving() { invokeMenuItem("File", "Quit"); clickButton(waitForObject(":addressbook\\.tcl.__tk__messagebox.no")); }
sub closeWithoutSaving { invokeMenuItem("File", "Quit"); clickButton(waitForObject(":addressbook\\.tcl.__tk__messagebox.no")); }
def closeWithoutSaving invokeMenuItem("File", "Quit") clickButton(waitForObject(":addressbook\\.tcl.__tk__messagebox.no")) end
Here we use the invokeMenuItem
to do
File|Quit,
and then click the Save unsaved changes? dialog's
. The last line was copied from the recorded test.
The entire test is around 75 lines of code—and would be even less
if we moved the common functions (such as invokeMenuItem
,
enterText
, verifyRows
, and
closeWithoutSaving
) into a shared script. And much of the code was
copied directly from the recorded test, and in some cases parametrized.
This should be sufficient to give a flavor of writing test scripts for an AUT. Keep in mind that Squish provides far more functionality than we used here, (all of which is covered in the API Reference Manual (Chapter 6) and the Tools Reference Manual (Chapter 7)). And Squish also provides access to the entire public APIs of the AUT's objects.
However, one aspect of the test case is not very satisfactory. Although embedding test data as we did here is sensible for small amounts, it is rather limiting, especially when we want to use a lot of test data. Also, we didn't test any of the data that was added to see if it correctly ended up in the table. In the next section we will create another version of this test, only this time we will pull in the data from an external data source, and check that the actual data we added to the table is correct.
In the previous section we put three hard-coded names and addresses in
our test. But what if we want to test lots of data?
Or what if we want to change the data without having to change our test
script's source code. One approach is to import a dataset into Squish
and use the dataset as the source of the values we insert into our
tests. Squish can import data in .tsv
(tab-separated values format), .csv
(comma-separated values format), .xls
or
.xlsx
(Microsoft® Excel™ spreadsheet formats).
[16]
Test data can be imported using the Squish IDE, or manually using a file manager or console commands. We will describe both approaches, starting with using the Squish IDE.
For the addressbook.tcl application we want to
import the MyAddresses.tsv
data file. To do this, we
must start by clicking | to pop-up the
Import Squish Resource dialog (Section 8.3.7). Inside the dialog, click
the button to choose the file to
import—in this case MyAddresses.tsv
. Make
sure that the Import As combobox is set to
“TestData”. By default the Squish IDE will import the test data
just for the current test case, but we want the test data to be
available to all the test suite's test cases: to do this check the
radio button. Now
click the button. You can now see the file
listed in the Test Suite Resources view (in the Test Data tab), and if
you click the file's name it will be shown in an Editor view (Section 8.2.6). The screenshot shows Squish after the
test data has been added.
![]() | For command-line users |
---|---|
It is also possible to import test data outside the Squish IDE using a file
manager (such as File Explorer) or console commands. To do this, create or go to
a directory inside the test suite's directory called
|
Squish with some imported test data
Although in real life we would modify our
tst_adding
test case to use the test data, for the
purpose of the tutorial we will make a new test case called
tst_adding_data
that is a copy of
tst_adding
and which we will modify to make use of
the test data.
The only function we have to change is main
, where
instead of iterating over hard-coded items of data, we iterate over all
the records in the dataset. We also need to update the expected row
count at the end since we are adding a lot more records now, and we will
also add a function to verify each record that's added.
proc main {} { startApplication "addressbook.tcl" invokeMenuItem "File" "New" verifyRows 0 # Set a limit to avoid testing 100s of rows set limit 10 set data [testData dataset "MyAddresses.tsv"] set columns [llength [testData fieldNames [lindex $data 0]]] set row 0 for {} {$row < [llength $data]} {incr row} { set record [lindex $data $row] set forename [testData field $record "Forename"] set surname [testData field $record "Surname"] set phone [testData field $record "Phone"] set email [testData field $record "Email"] set details [list $forename $surname $phone $email] addNameAndAddress $details checkNameAndAddress $record if {$row > $limit} { break } } verifyRows [expr $row + 1] closeWithoutSaving }
def main(): startApplication("addressbook.tcl") invokeMenuItem("File", "New") verifyRows(0) limit = 10 # To avoid testing 100s of rows since that would be boring for row, record in enumerate(testData.dataset("MyAddresses.tsv")): forename = testData.field(record, "Forename") surname = testData.field(record, "Surname") phone = testData.field(record, "Phone") email = testData.field(record, "Email") addNameAndAddress((forename, surname, phone, email)) # pass as a single tuple checkNameAndAddress(record) if row > limit: break verifyRows(row + 1) closeWithoutSaving()
function main() { startApplication("addressbook.tcl"); invokeMenuItem("File", "New"); verifyRows(0); var limit = 10; // To avoid testing 100s of rows since that would be boring var records = testData.dataset("MyAddresses.tsv"); for (var row = 0; row < records.length; ++row) { var record = records[row]; var forename = testData.field(record, "Forename"); var surname = testData.field(record, "Surname"); var phone = testData.field(record, "Phone"); var email = testData.field(record, "Email"); addNameAndAddress(new Array(forename, surname, phone, email)); checkNameAndAddress(record); if (row > limit) break; } verifyRows(row + 1); closeWithoutSaving(); }
sub main { startApplication("addressbook.tcl"); invokeMenuItem("File", "New"); verifyRows(0); my $limit = 10; # To avoid testing 100s of rows since that would be boring my @records = testData::dataset("MyAddresses.tsv"); my $row = 0; for (; $row < scalar(@records); ++$row) { my $record = $records[$row]; my $forename = testData::field($record, "Forename"); my $surname = testData::field($record, "Surname"); my $phone = testData::field($record, "Phone"); my $email = testData::field($record, "Email"); addNameAndAddress($forename, $surname, $phone, $email); checkNameAndAddress($record); if ($row > $limit) { last; } } verifyRows($row + 1); closeWithoutSaving; }
def main startApplication("addressbook.tcl") invokeMenuItem("File", "New") verifyRows(0) limit = 10 # To avoid testing 100s of rows since that would be boring rows = 0 TestData.dataset("MyAddresses.tsv").each_with_index do |record, row| forename = TestData.field(record, "Forename") surname = TestData.field(record, "Surname") email = TestData.field(record, "Email") phone = TestData.field(record, "Phone") addNameAndAddress([forename, surname, email, phone]) # pass as a single Array checkNameAndAddress(record) break if row > limit rows += 1 end verifyRows(rows + 1) closeWithoutSaving end
Squish provides access to test data through its testData
module's functions—here we used the testData.dataset
function to access the data file
and make its records available, and the testData.field
function to retrieve each record's
individual fields.
Having used the test data to populate table we
want to be confident that the data in the table is the same as what we
have added, so that's why we added the
checkNameAndAddress
function. We also added a limit to
how many records we would compare, just to make the test run faster.
proc checkNameAndAddress {record} { set columns [llength [testData fieldNames $record]] for {set column 0} {$column < $columns} {incr column} { set expected_text [testData field $record $column] waitForObject ":addressbook\\.tcl.view.tree" # New items are always inserted before the current one, so the row is always 0 set cell [toString [invoke tcleval ".view.tree cellindex 0,$column"]] set actual_text [invoke tcleval ".view.tree cellcget $cell -text"] test compare $expected_text $actual_text } }
def checkNameAndAddress(record): for column in range(len(testData.fieldNames(record))): expected_text = testData.field(record, column) waitForObject(":addressbook\\.tcl.view.tree") # New items are always inserted before the current one, so the row is always 0 actual_text = tcleval(".view.tree cellcget [.view.tree cellindex 0,%d] -text" % column) test.compare(expected_text, actual_text)
function checkNameAndAddress(record) { for (var column = 0; column < testData.fieldNames(record).length; ++column) { var expected_text = testData.field(record, column); waitForObject(":addressbook\\.tcl.view.tree"); var actual_text = tcleval(".view.tree cellcget [.view.tree cellindex 0," + column + "] -text"); test.compare(expected_text, actual_text); } }
sub checkNameAndAddress { my $record = shift; my @columnNames = testData::fieldNames($record); for (my $column = 0; $column < scalar(@columnNames); $column++) { my $expected_text = testData::field($record, $column); waitForObject(":addressbook\\.tcl.view.tree"); # New items are always inserted before the current one, so the row is always 0 my $actual_text = tcleval(".view.tree cellcget [.view.tree cellindex 0,$column] -text"); test::compare($expected_text, $actual_text); } }
def checkNameAndAddress(record) for column in 0...TestData.fieldNames(record).length actual_text = tcleval( ".view.tree cellcget [.view.tree cellindex 0,#{column}] -text") Test.compare(actual_text, TestData.field(record, column)) end end
This function accesses the table's
first row and extracts each of its columns' values. We use Squish's
testData.fieldNames
function to get a
column count and then use the test.compare
function to check that each value in the table is the same as the value in the test
data we used. Note that for this particular test we always insert new
rows at the start of the table. The effect of this is that every new
name and address is always added as the first row, so this is why we
hard-coded the row to be 0.
The screenshot shows Squish's Test Summary log after the data-driven tests have been run.
Squish after a successful data-driven test run
We have now completed the tutorial! Squish can of course do much more than we have shown here, but the aim has been to get you started with basic testing as quickly and easily as possible.
The API Reference Manual (Chapter 6) and Tools Reference Manual (Chapter 7) give full details of Squish's testing API and the numerous functions it offers to make testing as easy and efficient as possible. It is well worth reading the User Guide (Chapter 5) and at least skimming the API Reference Manual (Chapter 6) and Tools Reference Manual (Chapter 7)—especially since the time invested will be repaid because you'll know what functionality Squish provides out of the box and can avoid reinventing things that are already available.
In addition to the documented examples listed above, further Tk example
applications and their corresponding tests are provided in
SQUISHDIR/examples/tk
.
[15] Each AUT must be registered with the squishserver so that test scripts do not need to include the AUT's path, thus making the tests platform-independent. Another benefit of registering is that AUTs can be tested without the Squish IDE—for example, when doing regression testing.
[16]
Both .csv
and .tsv
files are
assumed to use the Unicode UTF-8 encoding—the same encoding used
for all test scripts.