Tuto #2 : Test a web application with Squash TA and Sahi

[Last Update: 29/03/2013]

[Estimated duration: 30 min]

In this tutorial, you will learn how to create and run an automated test for a web application, using the Sahi controller.

Pre-requisites

This tutorial assumes that you have a running Squash TA toolbox. If you don't, please refer to the relevant section of the first tutorial: Installing the test automation environment. It works under Windows XP or better.

    • Ensure you have Squash TA version 1.1.0 or above

    • Ensure all the required software components are installed on your workstation. If not, please refer to the Install the Squash-TA Toolbox.

    • Create a Squash-TA project as described in the Set up a Squash-TA project.

    • Ensure Mozilla Firefox (up to version 12.0) is installed on your workstation. If not, please install it.

Setup of the System Under Test (SUT)

For this tutorial, we will use a packaged 'PetShop web application' available here.

To set it up, please unzip it somewhere on your disk.

    • To start it, launch the '<unzip-location>/startup_all.bat' script.

    • To stop it, launch the '<unzip-location>/stop_all.bat' script

Once the PetShop application is running, it is available at the following URL: http://localhost:8080/

Creating the PetShopTest project

This tutorial assumes that the Squash TA maven archetype is referenced in your toolbox Eclipse for Squash TA environment. If this is not the case, please review the procedure in the FAQ : Set up a Squash-TA project. Then, create a new project named 'tutorial-petshop'.

Create test configuration and resources

    • We will first create the Web target configuration for the PetShop SUT. Create a new file named 'petshop.properties' in the 'src/squashTA/targets' subdirectory and type the following contents in it:

#!http

squashtest.ta.http.endpoint.url=http://localhost:8080/

The first line tags the file as a Web target definition file, and the second defines the System Under Test (SUT) base URL as 'http://localhost:8080/' You now just have to save the file.

    • We will now create the sahi script to call our "Pet shop" SUT

    • First make sure the web application is up (for example, connect to the http://localhost:8080/ URL with your browser to check that you obtain the pet shop welcome page. If not, execute the 'startup_all.bat' script from the pet shop demo installation directory.

    • Now close all windows of the browser you want to work with, and launch sahi from the toolbox SAHI dashboard icon. The Sahi dashboard opens, displaying a list of available browsers on your workstation. Click on the relevant icon for your chosen browser. The SAHI welcome screen appears.

    • As written on the page, double-click on the page while holding the ALT key to invoke the SAHI command center.

    • Now go back to the welcome page, type the SUT URL 'http://localhost:8080/' and click on the 'Go' button.

    • The Pet Shop welcome page displays.

    • At that point, go back to the Sahi controller window, enter the script name (we will use 'petshop_sahi') in the Script Name field, then click on the 'Record' button.

    • Then, go back to the PetShop welcome page and click on the 'Enter the store' link.

    • Click on the 'Reptiles' link.

    • Next, click on the 'RP-SN-01' reference link for the Rattlesnake to open the list of available rattlesnakes.

Let's live dangerously and add the rattleless rattlesnake to the cart (click on the 'Add to Cart' link on the second row).

    • Check the cart out by clicking 'Proceed to checkout', then click on the 'Continue' link

    • The application now requires a user login to finalize the transaction. Let's use :

      • Username='j2ee'

      • Password='j2ee'

      • and click on 'Submit'. The order is summed up on screen.

    • Then, confirm the order by clicking on the 'Continue' button.

    • Done! we have now created our script. You can now go back to the Sahi Controller and click on the 'Stop' button.

    • Now open a file explorer window and browse to the '<toolbox-install-dir>\sahi_v35_20110719\userdata\scripts' directory. There copy the script file which is named 'petshop_sahi.sah'.

    • Under Eclipse, paste the file in the 'src/squashTA/resources' subdirectory.

    • We will now add a JDBC driver to our test project in order to connect to the database of the SUT.

    • Open the 'pom.xml' file, located at the root of the project

    • In the plugin section named 'squash-ta-maven-plugin', add a dependency for the JDBC hsqldb driver. In order to do that, add the following lines.

<project> ...

<build>

<plugins>

<plugin>

<groupId>org.squashtest.ta</groupId>

<artifactId>squash-ta-maven-plugin</artifactId>

<version>1.0.1-RELEASE</version>

<dependencies>

<dependency>

<groupId>hsqldb</groupId>

<artifactId>hsqldb</artifactId>

<version>1.8.0.7</version>

</dependency>

</dependencies>

....

    • We will now create the database target file :

      • Using the 'New' option in the contextual menu of the 'targets' subdirectory, open the creation wizard, and select 'file'.

    • Name the file 'petshop_db.properties'

    • Type the following contents in the file:

squashtest.ta.database.driver=org.hsqldb.jdbcDriver

squashtest.ta.database.url=jdbc:hsqldb:hsql://localhost:9002

squashtest.ta.database.username=sa

squashtest.ta.database.password=

    • We will now create an SQL script to check that the command has been registered.

    • Create a new file named 'checkQuery.sql' in the 'resources' subdirectory.

    • Enter the following contents in this file:

select count(1) as "COUNT" from ORDERS where ORDERDATE='${now().format(yyyy-MM-dd)}'

    • Next, we will create a DbUnit dataset as reference.

      • Create a new file named 'expectedCount.xml' in the 'resources' subdirectory.

      • Type the following content in the file:

<?xml version="1.0"?>

<dataset>

<default count="1"/>

</dataset>

      • We will then create a clean up SQL script as follows :

        • Create a new file named 'cleanup.sq'l in the 'resources' subdirectory.

        • Type the following content in the file :

delete from LINEITEM where ORDERID in (select ORDERID from ORDERS where ORDERDATE='${now().format(yyyy-MM-dd)}')

delete from ORDERSTATUS where ORDERID in (select ORDERID from ORDERS where ORDERDATE='${now().format(yyyy-MM-dd)}')

delete from ORDERS where ORDERDATE='${now().format(yyyy-MM-dd)}'

    • The last resource file to create is the sahi configuration file.

      • Create a new file named 'sahi_config.properties' in the 'resources' subdirectory

      • Type the following contents in it:

browserType=ie (If you use Internet Explorer for your test)

browserType=firefox (For Firefox)

    • Last, but not least, we'll write the test script.

      • Create a new file named 'petshop.ta' in the 'tests' subdirectory

      • Give it the following content :

setup :

//execute cleanup script

# EXECUTE_SQL cleanup.sql ON petshop_db AS dummy1

test :

// shortcut to execute the sahi script and check success

# EXECUTE_SAHI petshop_sahi.sah ON petshop USING sahi_config.properties

// additionnaly, we test db side if the command was created

# EXECUTE_SQL checkQuery.sql ON petshop_db AS checkResultSet

//now we compare it to our expected result

CONVERT checkResultSet TO dataset.dbunit AS actual

LOAD expectedCount.xml AS expectedCount

CONVERT expectedCount TO xml AS expectedCount.xml

CONVERT expectedCount.xml TO dataset.dbunit AS expectedCount.dbu

ASSERT actual DOES contain THE expectedCount.dbu

teardown :

//we erase the created order

# EXECUTE_SQL cleanup.sql ON petshop_db AS dummy2

Executing the test

    • Before executing the tests, make sure you have the test application and the sahi proxy up and running.

    • Select the 'petshop.ta' file by clicking on it

    • Select 'Run>Configurations' in the toolbar of Eclipse

    • Select 'Maven Build> Run selected test(s)' in the run configurations list, on the left of the dialog box. This run configuration enables you to run the test you have selected in your Squash-TA project (here 'petshop.ta').

    • Click on 'Run'

    • An instance of Mozilla Firefox should open and your testcase should be played back.

    • Once the execution is completed, you should see a 'BUILD SUCCESS' message in the Eclipse Consoletab, as follows: