Table of Contents
Learn how to test native Windows applications.
Table of Contents
This tutorial will show you how to create, run, and modify tests for a sample native Windows 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).
When we show how to achieve something using the IDE we will often 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 application as our
AUT. The application is shipped with Squish in
SQUISHDIR/examples/win/Addressbook
.
The Address Book application is a very basic application 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 contains many of 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 supports in-place editing and also has a pop-up modal dialog for adding 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 various Windows-specific features, as well as all the standard editing widgets.
The screenshot shows the application in action with a user adding a new name and address.
The Windows Addressbook
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, navigate to the example folder
( |
![]() | Windows Security Dialog |
---|---|
Depending on your security settings, Windows might pop up a dialog asking if you want to allow or block the squishserver and/or your AUT from running. If you get this dialog, you must choose for Squish to be able to function correctly. |
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:
an application to test—known as 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. |
In most cases, nothing special needs to be done to make an application testable, since the toolkit's API (e.g., Qt) provides enough functionality to implement and record test scripts. The connection to the squishserver is also established automatically, when the Squish IDE starts the AUT.
![]() | 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 works. 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 version of Windows you are using and the theme, colors and fonts that you use, and so on.
Once Squish has started click | to pop-up the New Squish Test Case wizard (Section 8.3.10) 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_py
and will put it inside an
Addressbook
folder. (For your own tests you might
use a more meaningful name such as "suite_addressbook"; we chose
"suite_py" because our tutorials offer several
suites, one 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, choose the toolkit your AUT uses. For this example, we must click Windows since we are testing a native Windows 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. (So if you want to use multiple scripting languages, just create multiple test suites, one for each scripting language you want to use.) The functionality offered by Squish is the same for all languages. If you are new to scripting, Python is the easiest to learn and use, and arguably the best for readability and maintenance. 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 pop-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. In the case of Windows programs, the AUT is the
application's executable (e.g., addressbook.exe
),
although it is also possible to use batch (.bat
) files.
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.7.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, register the AUT with squishserver; and third, create a test suite configuration file.
|
We are now ready to record our first test.
Squish records tests using the scripting language that was specified for the test suite. Once a test has been recorded, we can run the test and Squish will faithfully repeat all the actions that we performed when recording the test, without the pauses that humans are prone to but which computers don't need. 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 () button (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.py
. (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.py
) appear
in an Editor view (Section 8.2.6), click—or double-click
depending on the | |
setting—the test case. This selects the Script as the active one
and makes visible its corresponding () and (
) buttons.
The checkboxes are used to control which test cases are
run when the ) toolbar button is clicked; we can also run
a single test case by clicking its (
) button. If the test case is not
currently active, the button may be invisible until the mouse is hovered over
it.
Initially, the
script's main()
logs "Hello World" to the test results. If
we were to create a test manually (as we will do later on in the tutorial), we
must create a main
function, and we
should import the same imports at the top.
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. 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 test case has been created, we are free to write
test code manually, or to record a test. Clicking on the test case's
) replaces the test's code with a new recording.
It is also possible to record snippets and insert them into existing test
cases, covered in the Users Guide and not in this tutorial.
![]() | 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 a 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:
Click MyAddresses.adr
filename, then click the button.
Click the second row, then click 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's second (surname) column, delete its text and
replace it with Doe
. (You can do this simply by overtyping.)
Then press Enter to confirm your edit.
Now click the first row, then click
Jane Doe
entry
should now be the first one.
Click the Squish Control Bar (the second button from the left), then click .
toolbar button in theThis will make the Squish IDE appear. In the Application
Objects view, expand the
Address_Book__MyAddresses_adr_Window_0
object, then the
Table_0
object. Click the TableRow_6
object,
and then the Jane_TableCell_0
object
to make its properties appear in the
Properties view (Section 8.2.12), and then check the
text
property's checkbox. Now click the
Doe_TableCell_1
object and check its
text
property. Finally, click the
button (at the
bottom of 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.
We've now completed the test, so in the AUT 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.py
file in an editor window as shown in the
screenshot.
Now that we've recorded the test we should be able to play it back, i.e., run it. This in itself is useful in that if the play back failed it might mean that the application has been broken. Furthermore, 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 a verification, or later on we might want to insert a new verification. We can easily insert additional verifications into a recorded test script as we will see in the next section, Inserting Additional Verification Points (Section 4.1.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_py --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 hovered or selected in the Test Suites view (Section 8.2.19).
When we have two or more test cases, we can run them all, one after another,
(or only those that are checked) by clicking (
).
![]() | 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 execute the squishrunner program and specify the test suite our recorded script is in and 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_py --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
and waitForObjectItem
calls as parameters to various
other calls such as mouseClick
,
clickButton
, and type
. The waitForObject
function waits 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 click 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 less than 35 lines. Here's an extract that just shows how Squish records clicking the menu's option, typing in Jane Doe's details into the Add dialog, and clicking at the end to close the dialog and update the table.
![]() | Scripting Language Support |
---|---|
Although the screenshots only show the Python 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 the combobox at the top of the page to select the language you use—this will hide the code snippets in other languages.) |
mouseClick(waitForObjectItem(names.address_Book_MyAddresses_adr_Menubar, "Edit")) mouseClick(waitForObjectItem(names.edit_MenuItem_2, "Add...")) type(waitForObject(names.address_Book_Add_Forename_Edit), "Jane") type(waitForObject(names.address_Book_Add_Forename_Edit), "<Tab>") type(waitForObject(names.address_Book_Add_Surname_Edit), "Doe") type(waitForObject(names.address_Book_Add_Surname_Edit), "<Tab>") type(waitForObject(names.address_Book_Add_Email_Edit), "jane.doe@nowhere.com") type(waitForObject(names.address_Book_Add_Email_Edit), "<Tab>") type(waitForObject(names.address_Book_Add_Phone_Edit), "555 123 4567") clickButton(waitForObject(names.address_Book_Add_OK_Button))
mouseClick(waitForObjectItem(names.addressBookMyAddressesAdrMenubar, "Edit")); mouseClick(waitForObjectItem(names.editMenuItem_2, "Add...")); type(waitForObject(names.addressBookAddForenameEdit), "Jane"); type(waitForObject(names.addressBookAddForenameEdit), "<Tab>"); type(waitForObject(names.addressBookAddSurnameEdit), "Doe"); type(waitForObject(names.addressBookAddSurnameEdit), "<Tab>"); type(waitForObject(names.addressBookAddEmailEdit),"jane.doe@nowhere.com"); type(waitForObject(names.addressBookAddEmailEdit),"<Tab>"); type(waitForObject(names.addressBookAddPhoneEdit), "555 123 4567"); clickButton(waitForObject(names.addressBookAddOKButton));
mouseClick(waitForObjectItem($Names::address_book_myaddresses_adr_menubar, "Edit")); mouseClick(waitForObjectItem($Names::edit_menuitem_2, "Add...")); type(waitForObject($Names::address_book_add_forename_edit), "Jane"); type(waitForObject($Names::address_book_add_forename_edit), "<Tab>"); type(waitForObject($Names::address_book_add_surname_edit), "Doe"); type(waitForObject($Names::address_book_add_surname_edit), "<Tab>"); type(waitForObject($Names::address_book_add_email_edit),"jane.doe\@nowhere.com"); type(waitForObject($Names::address_book_add_email_edit),"<Tab>"); type(waitForObject($Names::address_book_add_phone_edit), "555 123 4567"); clickButton(waitForObject($Names::address_book_add_ok_button));
mouseClick(waitForObjectItem(Names::Address_Book_MyAddresses_adr_Menubar, "Edit")) mouseClick(waitForObjectItem(Names::Edit_MenuItem_2, "Add...")) type(waitForObject(Names::Address_Book_Add_Forename_Edit), "Jane") type(waitForObject(Names::Address_Book_Add_Forename_Edit), "<Tab>") type(waitForObject(Names::Address_Book_Add_Surname_Edit), "Doe") type(waitForObject(Names::Address_Book_Add_Surname_Edit), "<Tab>") type(waitForObject(Names::Address_Book_Add_Email_Edit),"jane.doe@nowhere.com") type(waitForObject(Names::Address_Book_Add_Email_Edit), "<Tab>") type(waitForObject(Names::Address_Book_Add_Phone_Edit), "555 123 4567") clickButton(waitForObject(Names::Address_Book_Add_OK_Button))
invoke mouseClick [waitForObjectItem $names::Address_Book_MyAddresses_adr_Menubar "Edit"] invoke mouseClick [waitForObjectItem $names::Edit_MenuItem_2 "Add..."] invoke type [waitForObject $names::Address_Book_Add_Forename_Edit] "Jane" invoke type [waitForObject $names::Address_Book_Add_Forename_Edit] "<Tab>" invoke type [waitForObject $names::Address_Book_Add_Surname_Edit] "Doe" invoke type [waitForObject $names::Address_Book_Add_Surname_Edit] "<Tab>" invoke type [waitForObject $names::Address_Book_Add_Email_Edit] "jane.doe@nowhere.com" invoke type [waitForObject $names::Address_Book_Add_Email_Edit] "<Tab>" invoke type [waitForObject $names::Address_Book_Add_Phone_Edit] "555 123 4567" invoke clickButton [waitForObject $names::Address_Book_Add_OK_Button]
As you can see, the tester used the keyboard to tab from one text field to another and clicked Squish will have recorded the actual actions that were taken.
using the mouse rather than with a key press. If the tester had clicked the button any other way (for example, by pressing Alt+O, or by tabbing to the button and pressing the spacebar), the outcome would be the same, but of course![]() | Object Names |
---|---|
Squish recordings refer to objects using variables that begin with a
|
![]() | Editor Context Menu |
---|---|
When a Symbolic Name is under the cursor, the editor's context menu allows you to Object Map, or , which places its inline key-value representation in your desired script language at the cursor, allowing you to hand-edit the values and properties in your script. , showing its entry in the |
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 many 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". In addition, it is possible to verify that a search image exists somewhere on the screen, or that certain text is found by OCR. 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 35. This is done simply by double-clicking, or right-clicking in the gutter (next to the line number in the editor) and selecting the Jane Doe”. The screenshot shows the verifications that were entered using 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.)
context menu item. We chose this line because it follows the script lines where the first address is removed, so at this point (just before invoking the menu to close the application), the first address should be that of “
Having set the breakpoint, we now run the test as usual by clicking the
) button, or by clicking the
| menu option. Unlike a normal test run the
test will stop when the breakpoint is reached (i.e., at line 35, 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 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 is rarely 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 DataGridView
's first row's texts, so
we expand the Address_Book__MyAddresses_adr_Window_0
item, and
its child items until we find the Table_0
, and within
that we look past the TableColumn
objects to the
TableRow
objects since the item we are interested in is in
a row. We expand the first TableRow
to reveal its
TableCell
s. The first table cell (Jane_TableCell_0
)
has the first text we want to verify. Once we click the item object
(i.e., the table cell) 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 text of the TableRow_6
item's Jane_TableCell_0
has the value “Jane”; we already have a verification for
this that we inserted during recording. Scroll down so that you can see
the jane_doe_nowhere_com_TableCell_2
item: this is the email address.
To make sure that this is verified every time the test is run, click the
jane_doe_nowhere_com_TableCell_2
item in the Application Objects view (Section 8.2.1) to make its properties
appear, and then check the text. 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 555_123_4567_TableCell_3
item in the Application Objects view (Section 8.2.1); then click its
text
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—a couple of which we have expanded to show their 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, email, and phone number 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 to inserting them 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 DataGridView
at the end of the test, just
before the AUT is terminated. The screenshot shows two of the lines of
code we entered to get this verification, 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 retrieving a reference to the object we are interested
in. 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), and then
returns a reference to it. (Otherwise it times out and raises a
catchable exception.) We then use this reference to access the item's
properties—in this case the DataGridView
's
rowCount—and verify that the value is what
we expect it to be using the test.verify
function. (Incidentally, we got the name for the object from the
previous line so we didn't need to set a breakpoint and manually add the
table's name to the Object Map to ensure that Squish would remember it
in this particular case because Squish had already added it during the
test recording.)
Here is the code we entered manually for the last 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. (For most verifications in most languages we use calls to the
test.verify
function—but for Tcl it is
often more convenient to use the test.compare
function as we have done here.)
table = waitForObject(names.address_Book_MyAddresses_adr_Table) test.compare(table.rowCount, 125)
var table = waitForObject(names.addressBookMyAddressesAdrTable); test.compare(table.rowCount, 125);
my $table = waitForObject($Names::address_book_myaddresses_adr_table); test::compare($table->rowCount, 125);
table = waitForObject(Names::Address_Book_MyAddresses_adr_Table) Test.compare(table.rowCount, 125)
set table [waitForObject $names::Address_Book_MyAddresses_adr_Table_2] test compare [property get $table rowCount] 125
The coding pattern is very simple: we retrieve a reference to the object we are interested in and then verify its properties using one of Squish's verification functions. And we can, of course, call methods on the object to interact with it if we wish.
We will see more examples of manually written code shortly, in the Creating Tests by Hand (Section 4.1.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 or real 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 use the Object Picker () or navigate to the object in the Application
Objects tree view and use the context menu to both add the object to the
Object Map (so that Squish will remember it) and to the clipboard (so that we can paste it
into our test script). At the end, we can click the toolbar button to terminate the AUT and return Squish
to the Squish Test Management Perspective (Section 8.1.2.2). (See How to Use the Spy (Section 5.21.3) in the User Guide (Chapter 5) for more
details on using the Spy.)
We can view the Object Map by clicking the ) toolbar 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
symbolic name used by Squish in recorded scripts by right-clicking the
object we are interested in and then clicking the context menu's
(or
, if you wish to make changes to it
in your script).
Squish's Object Map
Suppose we want to test the AUT's Add functionality by adding three new names and addresses. We could of course record such a test but it is just as easy to do everything in code. The steps we need the test script to do are: first click
to create a new address book, then for each new name and address, click , then fill in the details, and click . And finally, click 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 also refactor as we go, to make our code as neat and modular as possible.
First we must create a new test case. Click
tst_adding
. Squish will automatically create a
test.py
(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.py
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:
import names import os def main(): startApplication('"' + os.environ["SQUISH_PREFIX"] + '\\examples\\win\\Addressbook\\Addressbook.exe"') mouseClick(waitForObjectItem(names.address_Book_Unnamed_Menubar, "File")) mouseClick(waitForObjectItem(names.file_MenuItem, "Open..."))
import * as names from 'names.js'; function main() { startApplication('"' + OS.getenv("SQUISH_PREFIX") + '\\examples\\win\\Addressbook\\Addressbook.exe"'); mouseClick(waitForObjectItem(names.addressBookUnnamedMenubar, "File")); mouseClick(waitForObjectItem(names.fileMenuItem, "Open..."));
require 'names.pl'; use File::Spec; sub main { startApplication("\"$ENV{'SQUISH_PREFIX'}\\examples\\win\\Addressbook\\Addressbook.exe\""); mouseClick(waitForObjectItem($Names::address_book_unnamed_menubar, "File")); mouseClick(waitForObjectItem($Names::file_menuitem, "Open..."));
require 'names' include Squish def main startApplication("\"#{ENV['SQUISH_PREFIX']}\\examples\\win\\Addressbook\\Addressbook.exe\"") mouseClick(waitForObjectItem(Names::Address_Book_Unnamed_Menubar, "File")) mouseClick(waitForObjectItem(Names::File_MenuItem, "Open..."))
source [findFile "scripts" "names.tcl"] proc main {} { startApplication "\"$::env(SQUISH_PREFIX)\\examples\\win\\Addressbook\\Addressbook.exe\"" invoke mouseClick [waitForObjectItem $names::Address_Book_Unnamed_Menubar "File"] invoke mouseClick [waitForObjectItem $names::File_MenuItem "Open..."]
Notice that the pattern in the code is simple: start the AUT, then wait
for the menu bar, then click the menu bar; wait for the menu item,
then click the menu item. In both cases, we have used the
waitForObjectItem
function. This function
is used for 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.
![]() | Simplicity and Sharing |
---|---|
It may seem a waste to put our functions in
|
If you look at the recorded test (tst_general
) or
in the Object Map you will see that Squish sometimes uses
different names for the same object.
For example, the menubar is identified in two different ways,
initially as names.AddressBook_MenuBar
, then if the user clicks
, it is identified as
names.AddressBookUnnamed_MenuBar
, and if the user
clicks and opens the MyAddresses.adr
file, then the menubar is identified as names.AddressBookMyAddresses.adr_MenuBar
.
The reason for this is that Squish needs to uniquely identify every
object in a given context, and it uses heuristics to determine which properties
are good for identification. In the case of identifying the main window,
Squish uses the window title text to give it some context. (For
example, an application's or
menus may have different options depending on whether a file is loaded and what
state the application is in.)
Naturally, when we write test scripts we don't want to have to know or care which particular variation of a name to use, and Squish supports this need by providing alternative naming schemes, as we will see shortly.
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), or show an error like this:
Object Not Found Dialog
don't worry! It just means that Squish doesn't have an object with the
given name in the Object Map. From here, we can
,
, or after picking a new object,
. Picking a new object will update the object
map entry for the symbolic name.
In addition to the Object Picker (), we can also use the Spy's Application Objects view (Section 8.2.1) to locate the objects we
are interested in and use the context menu to access real or symbolic names
of them.
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 )
and set the test case's name to be
tst_dummy
. Then click
the dummy test case's ().
Once the AUT starts, click , then click the (empty)
table, then click and add an item, then press
Return or click . Finally,
click to finish, and say “No” to
saving changes. Then 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.
With all the object names we need in the Object Map, we can now write our
own test script completely from scratch. We will start with
main()
, and then we will look at the supporting
functions that main()
uses.
import names import os def main(): startApplication('"' + os.environ["SQUISH_PREFIX"] + '\\examples\\win\\Addressbook\\Addressbook.exe"') table = waitForObject({"type": "Table"}) invokeMenuItem("File", "New") test.verify(table.rowCount == 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) test.compare(waitForObject(table).rowCount, len(data)) closeWithoutSaving()
import * as names from 'names.js'; function main() { startApplication('"' + OS.getenv("SQUISH_PREFIX") + '\\examples\\win\\Addressbook\\Addressbook.exe"'); var table = waitForObject({"type": "Table"}); invokeMenuItem("File", "New"); test.verify(table.rowCount == 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]); test.compare(waitForObject(table).rowCount, data.length); closeWithoutSaving() }
require 'names.pl'; sub main { startApplication("\"$ENV{'SQUISH_PREFIX'}\\examples\\win\\Addressbook\\Addressbook.exe\""); my $table = waitForObject({"type" => "Table"}); invokeMenuItem("File", "New"); test::verify($table->rowCount == 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 my $oneNameAndAddress (@data) { addNameAndAddress(@{$oneNameAndAddress}); } test::compare(waitForObject($table)->rowCount, scalar(@data)); closeWithoutSaving(); }
require 'squish' require 'names' include Squish def main startApplication("\"#{ENV['SQUISH_PREFIX']}\\examples\\win\\Addressbook\\Addressbook.exe\"") table = waitForObject({:type => "Table"}) invokeMenuItem("File", "New") Test.verify(table.rowCount == 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 Test.compare(waitForObject(table).rowCount, data.length) closeWithoutSaving end
source [findFile "scripts" "names.tcl"] proc main {} { startApplication "\"$::env(SQUISH_PREFIX)\\examples\\win\\Addressbook\\Addressbook.exe\"" set table [waitForObject [::Squish::ObjectName type Table]] test compare [property get $table rowCount] 0 invokeMenuItem "File" "New" 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] } test compare [property get [waitForObject $table] rowCount] [llength $data] closeWithoutSaving }
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). Then we obtain a reference to the DataGridView
.
The object name we used was not put in the Object Map when the
tst_general
test case was recorded, so we recorded
a dummy test to make sure the name was added—we could just as
easily have used the Spy of course. We then copied the name from the
Object Map into our code. The waitForObject
function waits until an object is ready (visible and enabled) and
returns a reference to it—or it times out and raises a catchable
exception. We have used a symbolic name to access
the table—these are the names that Squish uses when recording
tests—rather than a real/multi-property name (which we will soon
see an example of). Once we have the table
reference we can use
it to access any of the DataGridView
's
public methods and properties.
invokeMenuItem()
is a function we wrote
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 , we verify that the table's row count
is 0.
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 three supporting functions, so as to
cover all the code in the tst_adding
test case,
starting with invokeMenuItem()
.
def invokeMenuItem(menu, item): mouseClick(waitForObjectItem({"type": "Menubar"}, menu)) mouseClick(waitForObject({'type': 'MenuItem', 'text' : item}))
function invokeMenuItem(menu, item) { mouseClick(waitForObjectItem({'type':'Menubar'}, menu)); mouseClick(waitForObject({'type':'MenuItem', 'text': item})); }
sub invokeMenuItem { my ($menu, $item) = @_; mouseClick(waitForObjectItem({"type" => "Menubar"}, $menu)); mouseClick(waitForObject({'type' => 'MenuItem', 'text' => $item})); }
def invokeMenuItem(menu, item) mouseClick(waitForObjectItem({:type => "Menubar"}, menu)) mouseClick(waitForObject({:type => 'MenuItem', :text => item})) end
proc invokeMenuItem {menu item} { invoke mouseClick [waitForObjectItem [::Squish::ObjectName type Menubar] $menu] invoke mouseClick [waitForObject [::Squish::ObjectName type MenuItem text $item]] }
As we mentioned earlier, the symbolic names Squish uses for menus and menu items (and other objects) can vary depending on the context, and often with the start of the name derived from the window's title. For applications that put the current filename in the title—such as the Address Book example—names will include the filename, and we must account for this.
In the case of the Address Book example, the main window's title is “Address Book” (at startup), or “Address Book - Unnamed” (after , but before or ), or “Address Book - filename” where the filename can of course vary. Our code accounts for all these cases by making use of real (multi-property) names.
Symbolic names are variables with a names.
prefix. Real names are
usually multi-property names, which specify a key-value mapping of matching properties to
identify the desired object. Multi-property names must specify at least the
type. Here we've used type to uniquely
identify the MenuBar
(since the application only has
one), and type and text to uniquely
identify the MenuItem
.
Once we have identified the object we want to interact with we use the
waitForObject
function or the
waitForObjectItem
function to retrieve a
reference to it and then we click it using the
mouseClick
function. The
waitForObject
and
waitForObjectItem
functions pause Squish until
the specified object (and its item in the latter case) 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 item. And as soon as the waiting is
over each time we click the object (or its item) using the mouseClick
function.
def addNameAndAddress(oneNameAndAddress): invokeMenuItem("Edit", "Add...") type(waitForObject(names.address_Book_Add_Forename_Edit), oneNameAndAddress[0]) type(waitForObject(names.address_Book_Add_Surname_Edit), oneNameAndAddress[1]) type(waitForObject(names.address_Book_Add_Email_Edit), oneNameAndAddress[2]) type(waitForObject(names.address_Book_Add_Phone_Edit), oneNameAndAddress[3]) clickButton(waitForObject(names.address_Book_Add_OK_Button))
function addNameAndAddress(oneNameAndAddress) { invokeMenuItem("Edit", "Add..."); type(waitForObject(names.addressBookAddForenameEdit), oneNameAndAddress[0]); type(waitForObject(names.addressBookAddSurnameEdit), oneNameAndAddress[1]); type(waitForObject(names.addressBookAddEmailEdit), oneNameAndAddress[2]); type(waitForObject(names.addressBookAddPhoneEdit), oneNameAndAddress[3]); clickButton(waitForObject(names.addressBookAddOKButton)); }
sub addNameAndAddress { my (@oneNameAndAddress) = @_; invokeMenuItem("Edit", "Add..."); type(waitForObject($Names::address_book_add_forename_edit), $oneNameAndAddress[0]); type(waitForObject($Names::address_book_add_surname_edit), $oneNameAndAddress[1]); type(waitForObject($Names::address_book_add_email_edit), $oneNameAndAddress[2]); type(waitForObject($Names::address_book_add_phone_edit), $oneNameAndAddress[3]); clickButton(waitForObject($Names::address_book_add_ok_button)); }
def addNameAndAddress(oneNameAndAddress) invokeMenuItem("Edit", "Add...") type(waitForObject(Names::Address_Book_Add_Forename_Edit), oneNameAndAddress[0]) type(waitForObject(Names::Address_Book_Add_Surname_Edit), oneNameAndAddress[1]) type(waitForObject(Names::Address_Book_Add_Email_Edit), oneNameAndAddress[2]) type(waitForObject(Names::Address_Book_Add_Phone_Edit), oneNameAndAddress[3]) clickButton(waitForObject(Names::Address_Book_Add_OK_Button)) end
proc addNameAndAddress {oneNameAndAddress} { invokeMenuItem "Edit" "Add..." invoke type [waitForObject $names::Address_Book_Add_Forename_Edit] [lindex $oneNameAndAddress 0] invoke type [waitForObject $names::Address_Book_Add_Surname_Edit] [lindex $oneNameAndAddress 1] invoke type [waitForObject $names::Address_Book_Add_Email_Edit] [lindex $oneNameAndAddress 2] invoke type [waitForObject $names::Address_Book_Add_Phone_Edit] [lindex $oneNameAndAddress 3] invoke clickButton [waitForObject $names::Address_Book_Add_OK_Button] }
For each set of name and address data we invoke the Add dialog. Then for each value
received, we populate the appropriate field by waiting for the relevant
Edit control to be ready and then typing in the text using
the type
function. We then type in a
tab to force the focus into the next focusable widget,
again using the type
function.
And at the end we press Return to click the dialog's
. We got the lines at the heart of the function by
copying them from the recorded tst_general
test and simply
parametrizing the first one by the field name and the text. Similarly,
we copied the code for clicking the button from the
code for the dummy test case we created to make Squish put the names of the
objects we wanted to interact with in the Object Map.
def closeWithoutSaving(): invokeMenuItem("File", "Quit") clickButton(waitForObject(names.address_Book_No_Button))
function closeWithoutSaving() { invokeMenuItem("File", "Quit"); clickButton(waitForObject(names.addressBookNoButton)); }
sub closeWithoutSaving { invokeMenuItem("File", "Quit"); clickButton(waitForObject($Names::address_book_no_button)); }
def closeWithoutSaving invokeMenuItem("File", "Quit") clickButton(waitForObject(Names::Address_Book_No_Button)) end
proc closeWithoutSaving {} { invokeMenuItem "File" "Quit" invoke clickButton [waitForObject $names::Address_Book_No_Button] }
Here we use invokeMenuItem()
to do ,
and then click the Save unsaved changes? dialog's
. The last line was copied from the recorded test.
The entire test is around 30 lines of code—and would be even less
if we put some of the common functions (such as
invokeMenuItem()
and closeWithoutSaving()
) in
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 DataGridView
.
In the next section we will create a new version of this test, only this
time we will pull in the data from an external data source, and check
that the data we add to the DataGridView
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).
[11]
Test data can either 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 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
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.
import names import os def main(): startApplication('"' + os.environ["SQUISH_PREFIX"] + '\\examples\\win\\Addressbook\\Addressbook.exe"') table = waitForObject({"type": "Table"}) invokeMenuItem("File", "New") test.verify(table.rowCount == 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") email = testData.field(record, "Email") phone = testData.field(record, "Phone") addNameAndAddress((forename, surname, email, phone)) insertionRow = row # Modern Python: insertionRow = row if row >= 1 else 1 if row < 1: insertionRow = 1 checkNameAndAddress(insertionRow, record) if row > limit: break test.compare(table.rowCount, row + 1) closeWithoutSaving()
import * as names from 'names.js'; function main() { startApplication('"' + OS.getenv("SQUISH_PREFIX") + '\\examples\\win\\Addressbook\\Addressbook.exe"'); var table = waitForObject({"type": "Table"}); invokeMenuItem("File", "New"); test.verify(table.rowCount == 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 email = testData.field(record, "Email"); var phone = testData.field(record, "Phone"); addNameAndAddress(new Array(forename, surname, email, phone)); var insertionRow = row < 1 ? 1 : row; checkNameAndAddress(insertionRow, record); if (row > limit) break; } test.compare(table.rowCount, row + 1); closeWithoutSaving() }
require 'names.pl'; sub main { startApplication("\"$ENV{'SQUISH_PREFIX'}\\examples\\win\\Addressbook\\Addressbook.exe\""); my $table = waitForObject({"type" => "Table"}); invokeMenuItem("File", "New"); test::verify($table->rowCount == 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 $email = testData::field($record, "Email"); my $phone = testData::field($record, "Phone"); addNameAndAddress($forename, $surname, $email, $phone); my $insertionRow = $row < 1 ? 1 : $row; checkNameAndAddress($insertionRow, $record); if ($row > $limit) { last; } } test::compare($table->rowCount, $row + 1); closeWithoutSaving(); }
require 'squish' require 'names' include Squish def main startApplication("\"#{ENV['SQUISH_PREFIX']}\\examples\\win\\Addressbook\\Addressbook.exe\"") table = waitForObject({:type => "Table"}) invokeMenuItem("File", "New") Test.verify(table.rowCount == 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]) insertionRow = row >= 1 ? row : 1 checkNameAndAddress(insertionRow, record) break if row > limit rows += 1 end Test.compare(table.rowCount, rows + 1) closeWithoutSaving end
source [findFile "scripts" "names.tcl"] proc main {} { startApplication "\"$::env(SQUISH_PREFIX)\\examples\\win\\Addressbook\\Addressbook.exe\"" set table [waitForObject [::Squish::ObjectName type Table]] test compare [property get $table rowCount] 0 invokeMenuItem "File" "New" 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 email [testData field $record "Email"] set phone [testData field $record "Phone"] set details [list $forename $surname $email $phone] addNameAndAddress $details set insertionRow $row if {$row < 1} { set insertionRow 1 } checkNameAndAddress $insertionRow $record if {$row > $limit} { break } } test compare [property get $table rowCount] [expr $row + 1] closeWithoutSaving }
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 the DataGridView
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 checkNameAndAddress()
.
We also added a limit to how many records we would compare, just to make the test run faster.
We have to specially compute the insertion row since the
Addressbook application inserts in
the row before the current one—except when the table is empty in
which case it inserts at the first row. Also, the Windows
DataGridView
uses 1-based indexing whereas Squish's data
handling functions use 0-based indexing, so we must also account for
this difference.
def checkNameAndAddress(insertionRow, record): for column in range(1, 1 + len(testData.fieldNames(record))): cell = waitForObject({'container' : names.table, 'row': insertionRow, 'column' : column, 'type' :'TableCell'}) test.compare(cell.text, testData.field(record, column - 1))
function checkNameAndAddress(insertionRow, record) { for (var column = 1; column <= testData.fieldNames(record).length; ++column) { var cell = waitForObject({'container' : names.table, 'row': insertionRow, 'column' : column, 'type' : 'TableCell'}); test.compare(cell.text, testData.field(record, column - 1)); } }
sub checkNameAndAddress { my($insertionRow, $record) = @_; my @columnNames = testData::fieldNames($record); for (my $column = 1; $column <= scalar(@columnNames); $column++) { my $cell = waitForObject({'container' => $Names::table, 'row' => $insertionRow, 'column'=> $column, 'type' => 'TableCell'}); test::compare($cell->text, testData::field($record, $column - 1)); } }
def checkNameAndAddress(row, record) for column in 1..TestData.fieldNames(record).length cell = waitForObject({:container => Names::Table, :row => row, :column => column, :type => 'TableCell'}) Test.compare(cell.text, TestData.field(record, column - 1)) end end
proc checkNameAndAddress {insertionRow record} { set columns [llength [testData fieldNames $record]] for {set column 1} {$column <= $columns} {incr column} { set cell [waitForObject [::Squish::ObjectName container $names::Table type TableCell row $insertionRow column $column]] test compare [property get $cell text] \ [testData field $record [expr {$column - 1}]] } }
This function compares the data in the DataGridView
at the
given insertion row (i.e., the data just added) with the
corresponding record (i.e., the data that was used for adding).
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. As previously noted, Squish's data handling functions
use 0-based indexing, but Window's DataGridView
uses
1-based indexing, so our test code must account for these differences.
Here, the insertion row and the column are 1-based so we deduct 1 from
the column when using the testData.field
function.
The screenshot shows Squish's Test Summary log after the data-driven tests have been run.
Squish after a successful data-driven test run
Squish can also do keyword-driven testing. This is a bit more sophisticated than data-driven testing. See How to Do Keyword-Driven Testing (Section 5.16).
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 User Guide (Chapter 5) provides many more examples, including those that show how tests can interact with particular widgets (controls or containers in Windows terminology) such as spinboxes, comboboxes, date/time editors, line editors, and various views.
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.
Table of Contents
This tutorial will show you how to create, run, and modify Behavior Driven Development (BDD) tests for an example application. You will learn about Squish's most frequently used features. By the end of the tutorial you will be able to write your own tests for your own applications.
For this chapter we will use a simple Address Book application as our Application Under Test (AUT). This is a very basic application that allows users to load an existing address book or create a new one, add, edit, and remove entries. The screenshot shows the application in action with a user adding a new name and address.
The Windows Addressbook
example.
Behavior-Driven Development (BDD) is an extension of the Test-Driven Development approach which puts the definition of acceptance criteria at the beginning of the development process as opposed to writing tests after the software has been developed. With possible cycles of code changes done after testing.
Behavior Driven Tests are built out of a set of Feature
files, which
describe product features through the expected application behavior in one or many
Scenarios
. Each Scenario
is built out of a sequence
of steps which represent actions or verifications that need to be
tested for that Scenario
.
BDD focuses on expected application behavior, not on implementation details. Therefore BDD tests are described in a human-readable Domain Specific Language (DSL). As this language is not technical, such tests can be created not only by programmers, but also by product owners, testers or business analysts. Additionally, during the product development, such tests serve as living product documentation. For Squish usage, BDD tests shall be created using Gherkin syntax. The previously written product specification (BDD tests) can be turned into executable tests. This step by step tutorial presents automating BDD tests with Squish IDE support.
Gherkin files describe product features through the expected application behavior in one or many Scenarios. An example showing the "Filling of addressbook" feature of the addressbook example application.
Feature: Filling of addressbook As a user I want to fill the addressbook with entries Scenario: Initial state of created address book Given addressbook application is running When I create a new addressbook Then addressbook should have zero entries Scenario: State after adding one entry Given addressbook application is running When I create a new addressbook And I add a new person 'John','Doe','john@m.com','500600700' to address book Then '1' entries should be present Scenario: State after adding two entries Given addressbook application is running When I create a new addressbook And I add new persons to address book | forname | surname | email | phone | | John | Smith | john@m.com | 123123 | | Alice | Thomson | alice@m.com | 234234 | Then '2' entries should be present Scenario: Forename and surname is added to table Given addressbook application is running When I create a new addressbook When I add a new person 'Bob','Doe','Bob@m.com','123321231' to address book Then previously entered forename and surname shall be at the top
Most of the above is free form text (does not have to be English). It's just the
Feature
/Scenario
structure and the leading keywords
like Given
, And
, When
and Then
that are fixed. Each of those keywords marks a
step defining preconditions, user actions and expected results. Above application
behavior description can be passed to software developers to implement these features and
at the same time the same description can be passed to software testers to implement
automated tests.
First, we need to create a Test Suite, which is a container for all Test Cases. Start the squishide and select | . Please follow the New Test Suite wizard, provide a Test Suite name, choose the Windows Toolkit and scripting language of your choice, and finally register AddressBook.exe application as AUT. Please refer to Creating a Test Suite (Section 4.7.1.2) for more details about creating new Test Suite.
Squish offers two types of Test Cases: "Script Test Case" and "BDD Test Case". As
"Script Test Case" is the default, in order to create new "BDD Test Case" we need to
use the context menu by clicking on the expander next to ()
and choosing .
The Squish IDE will remember your choice and the BDD Test Case will become the
default when clicking on the button in the future.
The newly created BDD Test Case consists of a test.feature
file (filled with a Gherkin template while creating a new BDD test case), a file
named test.(py|js|pl|rb|tcl)
which will drive the execution
(there is no need to edit this file), and a Test Suite Resources file named
steps/steps.(py|js|pl|rb|tcl)
where
step implementation code will be placed.
We need to replace the Gherkin template with a Feature
for the addressbook example application. To do this, copy the Feature
description below and paste it into the Feature
file.
Feature: Filling of addressbook As a user I want to fill the addressbook with entries Scenario: Initial state of created address book Given addressbook application is running When I create a new addressbook Then addressbook should have zero entries
When editing the test.feature
file, a Feature
file
warning No implementation found is displayed for each
undefined step. The implementations are in the
steps
subdirectory, in Test Case Resources,
or in Test Suite Resources. Running our
Feature
test now will currently fail at the first step with a
No Matching Step Definition and the following
steps will be skipped.
In order to record the Scenario
, press ()
next to the respective
Scenario
that is listed in the Scenarios
tab of Test Case Resources.
This will cause Squish to run the AUT so that we can interact with it. Additionally,
the Control Bar is displayed with a list of all steps that need to be
recorded. Now all interaction with the AUT or any verification points added to
the script will be recorded under the current step Given addressbook application is
running
(which is bolded in the Step list on the Control Bar). In order to verify that
this precondition is met, we will add a Verification Point. To do this, click on
in the Control Bar and select
.
As a result, the Squish IDE is put into Spy perspective which displays all
Application Objects and their Properties.
In the Application Objects View, select the
Address_Book
object.
Selecting it will update the Properties View to its right
side. Next, click on the checkbox in front of enabled in
the Properties View. Finally, click on
. The Squish IDE
disappears and the Control Bar is shown again.
When we are done with each step, we can move to the next
undefined step (playing back the ones that were previously defined)
by clicking on ) arrow button
in the Control Bar that is located to the left of the current step.
Next, for the step When I create a new addressbook
,
click on the toolbar button
of the AddressBook () and then
click (
).
Finally, for the step Then addressbook should have zero entries
verify that the table containing the address entries is empty. To record this
verification, click on while recording, and select
. In the Application Objects
view, navigate or use the Object Picker () tool to select (not check) the
TableView
object containing the address book entries (in our case this table is empty).
Check the rowCount property from the
Properties view and click
.
Finally, click on the last () arrow button in the Control Bar.
As a result, Squish will generate the following step
definitions in the steps.*
file (at
Test Suites+Test Suite
Resources):
@Given("addressbook application is running") def step(context): startApplication('"' + os.getenv("SQUISH_PREFIX") + '\\examples\\win\\Addressbook\\Addressbook.exe"') test.compare(waitForObjectExists(names.address_Book_Unnamed_Window).enabled, True) @When("I create a new addressbook") def step(context): mouseClick(waitForObject(names.new_ToolbarItem)) @Then("addressbook should have zero entries") def step(context): test.compare(waitForObjectExists(names.address_Book_Unnamed_Table).rowCount, 0)
Given("addressbook application is running", function(context) { startApplication('"' + OS.getenv("SQUISH_PREFIX") + '\\examples\\win\\Addressbook\\Addressbook.exe"'); test.compare(waitForObjectExists(names.addressBookUnnamedWindow).enabled, true); }); When("I create a new addressbook", function(context) { mouseClick(waitForObject(names.newToolbarItem)); }); Then("addressbook should have zero entries", function(context) { test.compare(waitForObjectExists(names.addressBookUnnamedTable).rowCount, 0); });
Given("addressbook application is running", sub { my $context = shift; startApplication("\"$ENV{'SQUISH_PREFIX'}\\examples\\win\\Addressbook\\Addressbook.exe\""); test::compare(waitForObjectExists($Names::address_book_unnamed_window)->enabled, 1); }); When("I create a new addressbook", sub { my $context = shift; clickButton(waitForObject($Names::new_toolbaritem)); }); Then("addressbook should have zero entries", sub { my $context = shift; test::compare(waitForObjectExists($Names::address_book_unnamed_table)->rowCount, 0); });
Given("addressbook application is running") do |context| startApplication("\"#{ENV['SQUISH_PREFIX']}\\examples\\win\\Addressbook\\Addressbook.exe\"") Test.compare(waitForObjectExists(Names::Address_Book_Unnamed_Window).enabled, true) end When("I create a new addressbook") do |context| clickButton(waitForObject(Names::New_ToolbarItem)) end Then("addressbook should have zero entries") do |context| Test.compare(waitForObjectExists(Names::Address_Book_Unnamed_Table).rowCount, 0) end
Given "addressbook application is running" {context} { startApplication "\"$::env(SQUISH_PREFIX)\\examples\\win\\Addressbook\\Addressbook.exe\"" test compare [property get [waitForObjectExists $names::Address_Book_Unnamed_Window] enabled] true } When "I create a new addressbook" {context} { invoke mouseClick [waitForObject $names::New_ToolbarItem] invoke mouseClick [waitForObject $names::New_ToolbarItem] } Then "addressbook should have zero entries" {context} { test compare [property get [waitForObjectExists $names::Address_Book_Unnamed_Table] rowCount] 0 }
The application is automatically started at the beginning of the first step due
to the recorded startApplication()
call. At the end of each
Scenario, the onScenarioEnd
hook is called, causing
detach()
to be called on the application context.
Because the AUT was started with startApplication()
,
this causes it to terminate.
This hook function is found in the file bdd_hooks.(py|js|pl|rb|tcl)
,
which is located in the Scripts tab of the Test
Suite Resources view. You can define additional hook functions here.
For a list of all available hooks, please refer to Performing Actions During Test Execution Via Hooks (Section 6.19.10).
@OnScenarioEnd def hook(context): for ctx in applicationContextList(): ctx.detach()
OnScenarioEnd(function(context) { applicationContextList().forEach(function(ctx) { ctx.detach(); }); });
OnScenarioEnd(sub { foreach (applicationContextList()) { $_->detach(); } });
OnScenarioEnd do |context| applicationContextList().each { |ctx| ctx.detach() } end
OnScenarioEnd {context} { foreach ctx [applicationContextList] { applicationContext $ctx detach } }
So far, our steps did not use any parameters and all values
were hardcoded. Squish has different types of parameters like
any
, integer
or word
, allowing our
step definitions to be more reusable. Let us add a
new Scenario
to our Feature
file which will provide
step parameters for both the Test Data and the expected results.
Copy the below section into your Feature file.
Scenario: State after adding one entry Given addressbook application is running When I create a new addressbook And I add a new person 'John','Doe','john@m.com','500600700' to address book Then '1' entries should be present
After auto-saving
the Feature
file, the Squish IDE provides a hint that only 2
steps need to be implemented: When I add a new person 'John',
'Doe','john@m.com','500600700' to address book
and Then '1' entries should
be present
. The remaining steps already have a matching
step implementation.
To record the missing steps, hit the ) next to the
Scenario in the Test Case Resources.
The script will play until it gets to the missing
step and then prompt you to implement it. If you select the Add
button, then you can type in the information for a new entry. Click on
(
) to move to the next step. For the second missing step, we
could record an object property verification like we did with the
step
Then addressbook should have zero entries
.
Or we could copy that step's implementation in the steps.(py|js|pl|rb|tcl)
file
and increment the number at the end of the test.compare
line.
Instead of testing for zero items, we are testing for one item.
Now we parametrize the generated step implementation by replacing the values
with parameter types. Since we want to be able to add different names, replace
'John' with '|word|'. Note that each parameter will be passed to the step
implementation function in the order of appearance in the descriptive name of
the step. Finish parametrizing by editing the typed values
into keywords, to look like this example step
When I add a new person 'John', 'Doe','john@m.com','500600700'
to
address book:
@When("I add a new person '|word|','|word|','|any|','|integer|' to address book") def step(context, forename, surname, email, phone): clickButton(waitForObject(names.add_ToolbarItem)) type(waitForObject(names.address_Book_Add_Forename_Edit), forename) type(waitForObject(names.address_Book_Add_Surname_Edit), surname) type(waitForObject(names.address_Book_Add_Email_Edit), email) type(waitForObject(names.address_Book_Add_Phone_Edit), phone) clickButton(waitForObject(names.address_Book_Add_OK_Button))
When("I add a new person '|word|','|word|','|any|','|integer|' to address book", function(context, forename, surname, email, phone) { mouseClick(waitForObject(names.addToolbarItem)); type(waitForObject(names.addressBookAddForenameEdit), forename); type(waitForObject(names.addressBookAddSurnameEdit), surname); type(waitForObject(names.addressBookAddEmailEdit), email); type(waitForObject(names.addressBookAddPhoneEdit), phone); clickButton(waitForObject(names.addressBookAddOKButton)); });
When("I add a new person '|word|','|word|','|any|','|integer|' to address book", sub { my $context = shift; my ($forename, $surname, $email, $phone) = @_; clickButton(waitForObject($Names::add_toolbaritem)); type(waitForObject($Names::address_book_add_forename_edit), $forename); type(waitForObject($Names::address_book_add_surname_edit), $surname); type(waitForObject($Names::address_book_add_email_edit), $email); type(waitForObject($Names::address_book_add_phone_edit), $phone); clickButton(waitForObject($Names::address_book_add_ok_button)); });
When("I add a new person '|word|','|word|','|any|','|integer|' to address book") do |context, forename, surname, email, phone| mouseClick(waitForObject(Names::Add_ToolbarItem)) type(waitForObject(Names::Address_Book_Add_Forename_Edit), forename) type(waitForObject(Names::Address_Book_Add_Surname_Edit), surname) type(waitForObject(Names::Address_Book_Add_Email_Edit), email) type(waitForObject(Names::Address_Book_Add_Phone_Edit), phone) clickButton(waitForObject(Names::Address_Book_Add_OK_Button)) end
When "I add a new person '|word|','|word|','|any|','|integer|' to address book" {context forename surname email phone} { invoke mouseClick [waitForObject $names::Add_ToolbarItem] invoke type [waitForObject $names::Address_Book_Add_Forename_Edit] $forename invoke type [waitForObject $names::Address_Book_Add_Surname_Edit] $surname invoke type [waitForObject $names::Address_Book_Add_Email_Edit] $email invoke type [waitForObject $names::Address_Book_Add_Phone_Edit] $phone invoke clickButton [waitForObject $names::Address_Book_Add_OK_Button] }
If we recorded the final Then
as a missing step, and
verified the rowCount is 1 in the table, we can modify the
step so that it takes a parameter, so it can verify other integer values later.
@Then("'|integer|' entries should be present") def step(context, entryCount): test.compare(waitForObjectExists(names.address_Book_Unnamed_Table).rowCount, entryCount)
Then("'|integer|' entries should be present", function(context, entryCount) { test.compare(waitForObjectExists(names.addressBookUnnamedTable).rowCount, entryCount); });
Then("'|integer|' entries should be present", sub { my $context = shift; my ($entryCount) = @_; test::compare(waitForObjectExists($Names::address_book_unnamed_table)->rowCount, $entryCount); });
Then("'|integer|' entries should be present") do |context, entryCount| Test.compare(waitForObjectExists(Names::Address_Book_Unnamed_Table).rowCount, entryCount) end
Then "'|integer|' entries should be present" {context entryCount} { test compare [property get [waitForObjectExists $names::Address_Book_Unnamed_Table] rowCount] $entryCount }
The next Scenario
will test adding multiple entries to the address
book. We could use step When I add a new person John','Doe','john@m.com','500600700'
to address book
multiple times just with different data. But lets instead define a
new step called When I add a new person to address book
which will handle data from a table.
Scenario: State after adding two entries Given addressbook application is running When I create a new addressbook And I add new persons to address book | forname | surname | email | phone | | John | Smith | john@m.com | 123123 | | Alice | Thomson | alice@m.com | 234234 | Then '2' entries should be present
The step implementation to handle such tables looks like this:
@When("I add new persons to address book") def step(context): table = context.table # Drop initial row with column headers table.pop(0) for (forename, surname, email, phone) in table: clickButton(waitForObject(names.add_ToolbarItem)) type(waitForObject(names.address_Book_Add_Forename_Edit), forename) type(waitForObject(names.address_Book_Add_Surname_Edit), surname) type(waitForObject(names.address_Book_Add_Email_Edit), email) type(waitForObject(names.address_Book_Add_Phone_Edit), phone) clickButton(waitForObject(names.address_Book_Add_OK_Button))
When("I add new persons to address book", function(context) { var table = context.table; // Drop initial row with column headers for (var i = 1; i < table.length; ++i) { var forename = table[i][0]; var surname = table[i][1]; var email = table[i][2]; var phone = table[i][3]; mouseClick(waitForObject(names.addToolbarItem)); type(waitForObject(names.addressBookAddForenameEdit), forename); type(waitForObject(names.addressBookAddSurnameEdit), surname); type(waitForObject(names.addressBookAddEmailEdit), email); type(waitForObject(names.addressBookAddPhoneEdit), phone); clickButton(waitForObject(names.addressBookAddOKButton)); } });
When("I add new persons to address book", sub { my %context = %{shift()}; my @table = @{$context{'table'}}; # Drop initial row with column headers shift(@table); for my $row (@table) { my ($forename, $surname, $email, $phone) = @{$row}; clickButton(waitForObject($Names::add_toolbaritem)); type(waitForObject($Names::address_book_add_forename_edit), $forename); type(waitForObject($Names::address_book_add_surname_edit), $surname); type(waitForObject($Names::address_book_add_email_edit), $email); type(waitForObject($Names::address_book_add_phone_edit), $phone); clickButton(waitForObject($Names::address_book_add_ok_button)); } });
When("I add new persons to address book") do |context| table = context.table # Drop initial row with column headers table.shift for forename, surname, email, phone in table do mouseClick(waitForObject(Names::Add_ToolbarItem)) type(waitForObject(Names::Address_Book_Add_Forename_Edit), forename) type(waitForObject(Names::Address_Book_Add_Surname_Edit), surname) type(waitForObject(Names::Address_Book_Add_Email_Edit), email) type(waitForObject(Names::Address_Book_Add_Phone_Edit), phone) clickButton(waitForObject(Names::Address_Book_Add_OK_Button)) end end
When "I add new persons to address book" {context} { set table [$context table] # Drop initial row with column headers foreach row [lreplace $table 0 0] { foreach {forename surname email phone} $row break invoke clickButton [waitForObject $names::Add_ToolbarItem] invoke type [waitForObject $names::Address_Book_Add_Forename_Edit] $forename invoke type [waitForObject $names::Address_Book_Add_Surname_Edit] $surname invoke type [waitForObject $names::Address_Book_Add_Email_Edit] $email invoke type [waitForObject $names::Address_Book_Add_Phone_Edit] $phone invoke clickButton [waitForObject $names::Address_Book_Add_OK_Button] } }
Lets add a new Scenario
to the Feature
file. This
time we would like to check not the number of entries in address book list, but if this
list contains proper data. Because we enter data into the address book in one
step and verify them in another, we must share information about
entered data between those steps in order to perform a verification.
Scenario: Forename and surname is added to table Given addressbook application is running When I create a new addressbook When I add a new person 'Bob','Doe','Bob@m.com','123321231' to address book Then previously entered forename and surname shall be at the top
Revisiting the step implementation function we wrote in Step parametrization (Section 4.7.2.3.4), we will use context.userData to share data between step implementations.
@When("I add a new person '|word|','|word|','|any|','|integer|' to address book") def step(context, forename, surname, email, phone): clickButton(waitForObject(names.add_ToolbarItem)) type(waitForObject(names.address_Book_Add_Forename_Edit), forename) type(waitForObject(names.address_Book_Add_Surname_Edit), surname) type(waitForObject(names.address_Book_Add_Email_Edit), email) type(waitForObject(names.address_Book_Add_Phone_Edit), phone) clickButton(waitForObject(names.address_Book_Add_OK_Button)) context.userData = {} context.userData['forename'] = forename context.userData['surname'] = surname
When("I add a new person '|word|','|word|','|any|','|integer|' to address book", function(context, forename, surname, email, phone) { mouseClick(waitForObject(names.addToolbarItem)); type(waitForObject(names.addressBookAddForenameEdit), forename); type(waitForObject(names.addressBookAddSurnameEdit), surname); type(waitForObject(names.addressBookAddEmailEdit), email); type(waitForObject(names.addressBookAddPhoneEdit), phone); clickButton(waitForObject(names.addressBookAddOKButton)); context.userData["forename"] = forename; context.userData["surname"] = surname; });
When("I add a new person '|word|','|word|','|any|','|integer|' to address book", sub { my $context = shift; my ($forename, $surname, $email, $phone) = @_; clickButton(waitForObject($Names::add_toolbaritem)); type(waitForObject($Names::address_book_add_forename_edit), $forename); type(waitForObject($Names::address_book_add_surname_edit), $surname); type(waitForObject($Names::address_book_add_email_edit), $email); type(waitForObject($Names::address_book_add_phone_edit), $phone); clickButton(waitForObject($Names::address_book_add_ok_button)); $context->{'userData'} = {}; $context->{'userData'}{'forename'} = $forename; $context->{'userData'}{'surname'} = $surname; });
When("I add a new person '|word|','|word|','|any|','|integer|' to address book") do |context, forename, surname, email, phone| mouseClick(waitForObject(Names::Add_ToolbarItem)) type(waitForObject(Names::Address_Book_Add_Forename_Edit), forename) type(waitForObject(Names::Address_Book_Add_Surname_Edit), surname) type(waitForObject(Names::Address_Book_Add_Email_Edit), email) type(waitForObject(Names::Address_Book_Add_Phone_Edit), phone) clickButton(waitForObject(Names::Address_Book_Add_OK_Button)) context.userData = Hash.new context.userData[:forename] = forename context.userData[:surname] = surname end
When "I add a new person '|word|','|word|','|any|','|integer|' to address book" {context forename surname email phone} { invoke mouseClick [waitForObject $names::Add_ToolbarItem] invoke type [waitForObject $names::Address_Book_Add_Forename_Edit] $forename invoke type [waitForObject $names::Address_Book_Add_Surname_Edit] $surname invoke type [waitForObject $names::Address_Book_Add_Email_Edit] $email invoke type [waitForObject $names::Address_Book_Add_Phone_Edit] $phone invoke clickButton [waitForObject $names::Address_Book_Add_OK_Button] $context userData [dict create forename $forename surname $surname] }
All data stored in context.userData can be accessed in all
steps and Hooks
in all Scenarios
of the given Feature
. Finally, we need to implement
the step Then previously entered forename and surname shall be at the top
.
@Then("previously entered forename and surname shall be at the top") def step(context): test.compare(waitForObjectExists(names.o1_1_TableCell).text, context.userData['forename']) test.compare(waitForObjectExists(names.o1_2_TableCell).text, context.userData['surname'])
Then("previously entered forename and surname shall be at the top", function(context) { test.compare(waitForObjectExists(names.o11TableCell).text, context.userData["forename"]); test.compare(waitForObjectExists(names.o12TableCell).text, context.userData["surname"]); });
Then("previously entered forename and surname shall be at the top", sub { my $context = shift; test::compare( waitForObject($Names::o1_1_tablecell)->text, $context->{'userData'}{'forename'}); test::compare( waitForObject($Names::o1_2_tablecell)->text, $context->{'userData'}{'surname'}); });
Then("previously entered forename and surname shall be at the top") do |context| Test.compare(waitForObjectExists(Names::O1_1_TableCell).text, context.userData[:forename]) Test.compare(waitForObjectExists(Names::O1_2_TableCell).text, context.userData[:surname]) end
Then "previously entered forename and surname shall be at the top" {context} { test compare [property get [waitForObjectExists $names::1_1_TableCell] text] [dict get [$context userData] forename] test compare [property get [waitForObjectExists $names::1_2_TableCell] text] [dict get [$context userData] surname] }
Assume our Feature
contains the following two Scenarios
:
Scenario: State after adding one entry Given addressbook application is running When I create a new addressbook And I add a new person 'John','Doe','john@m.com','500600700' to address book Then "1" entries should be present Scenario: State after adding one entry Given addressbook application is running When I create a new addressbook And I add a new person 'Bob','Koo','bob@m.com','500600800' to address book Then "1" entries should be present
As we can see, those Scenarios
perform the same actions using different
test data. The same can be achieved by using a Scenario Outline
(a
Scenario
template with placeholders) and Examples (a table with
parameters).
Scenario Outline: Adding single entries multiple time Given addressbook application is running When I create a new addressbook And I add a new person '<forename>','<surname>','<email>','<phone>' to address book Then '1' entries should be present Examples: | forename | surname | email | phone | | John | Doe | john@m.com | 500600700 | | Bob | Koo | bob@m.com | 500600800 |
Please note that the OnScenarioEnd
hook will be executed at the end of
each loop iteration in a Scenario Outline
.
In the Squish IDE, users can execute all Scenarios
in a
Feature
, or execute only one selected Scenario
. In
order to execute all Scenarios
, the proper Test Case has to be
executed by clicking on the button in the Test Suites view.
In order to execute only one Scenario
, you need to open the
Feature
file, right-click on the given Scenario
and
choose . An alternative approach is to click on the
() button next to the respective
Scenario
in the Scenarios tab of Test Case Resources.
After a Scenario
is executed, the Feature
file is
colored according to the execution results. More detailed information (like logs) can
be found in the Test Results View.
Squish offers the possibility to pause an execution of a Test Case at any point in
order to check script variables, spy application objects or run custom
code in Squish script console. To do this, a breakpoint has to be placed before
starting the execution, either in the Feature
file at any line
containing a step or at any line of executed code (i.e. in middle of
step definition code).
After the breakpoint is reached, you can inspect all application objects and their properties. If a breakpoint is placed at a step definition or a hook is reached, then you can additionally add Verification Points or record code snippets.
BDD test maintainability can be increased by reusing step definitions in test cases located in another directory. For more information, see collectStepDefinitions().
This chapter is aimed at users that have existing Squish tests and who would like to introduce Behavior Driven Testing. The first section describes how to keep the existing tests and add new tests with the BDD approach. The second section describes how to convert existing suites to BDD.
The first option is to keep existing Squish tests and extend them by
adding new BDD tests. It's possible to have a Test Suite
containing both script-based Test Cases and BDD feature files.
Simply open an existing Test Suite
and choose
option from drop down list.
Assuming your existing script-based tests make use of a library and you are calling shared functions to interact with the AUT, those functions can also be used from BDD step implementations. In the example below, a function is used from multiple script-based Test Cases:
def createNewAddressBook(): clickButton(waitForObject(names.new_ToolbarItem))
function createNewAddressBook(){ clickButton(waitForObject(names.newToolbarItem)); }
sub createNewAddressBook{ clickButton(waitForObject($Names::new_toolbaritem)); }
def createNewAddressBook clickButton(waitForObject(Names::New_ToolbarItem)) end
proc createNewAddressBook {} { invoke clickButton [waitForObject $names::New_ToolbarItem] }
New BDD Test Cases can easily use the same function:
@When("I create a new addressbook") def step(context): createNewAddressBook()
When("I create a new addressbook", function(context){ createNewAddressBook() });
When("I create a new addressbook", sub { createNewAddressBook(); });
When("I create a new addressbook") do |context| createNewAddressBook end
When "I create a new addressbook" {context} { createNewAddressBook }
The second option is to convert an existing Test Suite
that contains
script-based Test Cases into behavior driven tests. Since a Test
Suite
can contain script-based and BDD Test Cases,
migration can be done gradually. A Test Suite
containing a mix
of both Test Case types can be executed and results analyzed without
any extra effort required.
The first step is to review all Test Cases of the existing Test Suite
and group them by the Feature
they test. Each script-based Test Case will
be transformed into a Scenario
, which is a part of a
Feature
. For example, assume we have 5 script-based Test Cases. After
review, we realize that those script-based Test Cases examine two Features
.
Therefore, when migration is completed, our Test Suite will contain two BDD Test Cases,
each of them containing one Feature
. Each Feature
will contain multiple Scenarios
. In our example the first
Feature
contains three Scenarios
and the second
Feature
contains two Scenarios
.
At the beginning, open a Test Suite
in the Squish IDE that contains
script-based Squish tests that are planned to be migrated to BDD. Next,
create a new Test Case by choosing
option from the context menu to the right of (). Each BDD Test Case
corresponds to a
test.feature
file
that contains one Feature
, with one or more
Scenario
s. Next, open the test.feature
file to describe the Features
using the Gherkin language. Following the syntax from the template, edit the
Feature
name and optionally provide a short description. Next,
decide which actions and verifications performed in the script-based Test Case
should be migrated to steps. This is how an example Test Case for the addressbook
application could look like:
def main(): startApplication("Addressbook") test.log("Create new addressbook") clickButton(waitForObject(names.new_ToolbarItem)) test.compare(waitForObjectExists(names.address_Book_Unnamed_Table).rowCount, 0)
function main() { startApplication("Addressbook"); test.log("Create new addressbook"); clickButton(waitForObject(names.newToolbarItem)); test.compare(waitForObjectExists(names.addressBookUnnamedTable).rowCount, 0); }
sub main { startApplication("Addressbook"); test::log("Create new addressbook"); clickButton(waitForObject($Names::new_toolbaritem)); test::compare(waitForObjectExists($Names::address_book_unnamed_table)->rowCount, 0); }
def main startApplication("Addressbook") Test.log("Create new addressbook") clickButton(waitForObject(Names::New_ToolbarItem)) Test.compare(waitForObjectExists(Names::Address_Book_Unnamed_Table).rowCount, 0) end
proc main {} { startApplication "Addressbook" test log "Create new addressbook" invoke clickButton [waitForObject $names::New_ToolbarItem] test compare [property get [waitForObjectExists $names::Address_Book_Unnamed_Table] rowCount] 0 }
After analyzing the above script-based Test Case we can create
the following Scenario
and add it to the
test.feature
file:
Scenario: Initial state of created address book Given addressbook application is running When I create a new addressbook Then addressbook should have zero entries
Next, right-click on the Scenario
and choose the option
from the context menu. This
will create a skeleton of step definitions:
@Given("addressbook application is running") def step(context): test.warning("TODO implement addressbook application is running") @When("I create a new addressbook") def step(context): test.warning("TODO implement I create a new addressbook") @Then("addressbook should have zero entries") def step(context): test.warning("TODO implement addressbook should have zero entries")
Given("addressbook application is running", function(context) { test.warning("TODO implement addressbook application is running"); }); When("I create a new addressbook", function(context) { test.warning("TODO implement I create a new addressbook"); }); Then("addressbook should have zero entries", function(context) { test.warning("TODO implement addressbook should have zero entries"); });
Given("addressbook application is running", sub { my $context = shift; test::warning("TODO implement addressbook application is running"); }); When("I create a new addressbook", sub { my $context = shift; test::warning("TODO implement I create a new addressbook"); }); Then("addressbook should have zero entries", sub { my $context = shift; test::warning("TODO implement addressbook should have zero entries"); });
Given("addressbook application is running") do |context| Test.warning "TODO implement addressbook application is running" end When("I create a new addressbook") do |context| Test.warning "TODO implement I create a new addressbook" end Then("addressbook should have zero entries") do |context| Test.warning "TODO implement addressbook should have zero entries" end
Given "addressbook application is running" {context} { test warning "TODO implement addressbook application is running" } When "I create a new addressbook" {context} { test warning "TODO implement I create a new addressbook" } Then "addressbook should have zero entries" {context} { test warning "TODO implement addressbook should have zero entries" }
Now we put code snippets from the script-based Test Case into respective
step definitions and remove the lines containing
test.warning
. If your script-based Test Cases makes use of shared scripts, you
can call those functions from the step definitions as well. For
example, the final result could look like this:
@Given("addressbook application is running") def step(context): startApplication('"' + os.getenv("SQUISH_PREFIX") + '\\examples\\win\\Addressbook\\Addressbook.exe"') test.compare(waitForObjectExists(names.address_Book_Unnamed_Window).enabled, True) @When("I create a new addressbook") def step(context): mouseClick(waitForObject(names.new_ToolbarItem)) @Then("addressbook should have zero entries") def step(context): test.compare(waitForObjectExists(names.address_Book_Unnamed_Table).rowCount, 0)
Given("addressbook application is running", function(context) { startApplication('"' + OS.getenv("SQUISH_PREFIX") + '\\examples\\win\\Addressbook\\Addressbook.exe"'); test.compare(waitForObjectExists(names.addressBookUnnamedWindow).enabled, true); }); When("I create a new addressbook", function(context) { mouseClick(waitForObject(names.newToolbarItem)); }); Then("addressbook should have zero entries", function(context) { test.compare(waitForObjectExists(names.addressBookUnnamedTable).rowCount, 0); });
Given("addressbook application is running", sub { my $context = shift; startApplication("\"$ENV{'SQUISH_PREFIX'}\\examples\\win\\Addressbook\\Addressbook.exe\""); test::compare(waitForObjectExists($Names::address_book_unnamed_window)->enabled, 1); }); When("I create a new addressbook", sub { my $context = shift; clickButton(waitForObject($Names::new_toolbaritem)); }); Then("addressbook should have zero entries", sub { my $context = shift; test::compare(waitForObjectExists($Names::address_book_unnamed_table)->rowCount, 0); });
Given("addressbook application is running") do |context| startApplication("\"#{ENV['SQUISH_PREFIX']}\\examples\\win\\Addressbook\\Addressbook.exe\"") Test.compare(waitForObjectExists(Names::Address_Book_Unnamed_Window).enabled, true) end When("I create a new addressbook") do |context| clickButton(waitForObject(Names::New_ToolbarItem)) end Then("addressbook should have zero entries") do |context| Test.compare(waitForObjectExists(Names::Address_Book_Unnamed_Table).rowCount, 0) end
Given "addressbook application is running" {context} { startApplication "\"$::env(SQUISH_PREFIX)\\examples\\win\\Addressbook\\Addressbook.exe\"" test compare [property get [waitForObjectExists $names::Address_Book_Unnamed_Window] enabled] true } When "I create a new addressbook" {context} { invoke mouseClick [waitForObject $names::New_ToolbarItem] invoke mouseClick [waitForObject $names::New_ToolbarItem] } Then "addressbook should have zero entries" {context} { test compare [property get [waitForObjectExists $names::Address_Book_Unnamed_Table] rowCount] 0 }
Note that the test.log("Create new addressbook")
got removed while
migrating this script-based Test to BDD. When the step
I create a new addressbook
is executed, the step
name will be logged into Test Results, so the test.log
call would have been redundant.
Additionally, when the Test Case execution ends, Squish terminates
the AUT. The AUT is also terminated at the end of each
Scenario
, by the autogenerated hooks file containing an
OnScenarioEnd
hook.
@OnScenarioEnd def hook(context): currentApplicationContext().detach()
OnScenarioEnd(function(context) { currentApplicationContext().detach(); });
OnScenarioEnd(sub { currentApplicationContext()->detach(); });
OnScenarioEnd do |context| currentApplicationContext().detach() end
OnScenarioEnd { context } { applicationContext [currentApplicationContext] detach }
The above example was simplified for this tutorial. In order to take full advantage of Behavior Driven Testing in Squish, please familiarize yourself with the section Behavior Driven Testing (Section 6.19) in API Reference Manual (Chapter 6).
[10] 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.
[11]
Both .csv
and .tsv
files are
assumed to use the Unicode UTF-8 encoding—the same encoding used
for all test scripts.