Sunteți pe pagina 1din 23

Canoo WebTest Jumpstart

Struts University Series


Abstract
Layering your
application means
layering your tests
to match.
Let's see how you
can use Canoo
WebTest to test
the V in MVC.
SimpleTest
<?xml version="1.0"?>
<project name="SimpleTest" basedir="." default="main">

<taskdef file="${webtest.home}/webtestTaskdefs.properties">
<classpath>
<fileset dir="${webtest.home}" includes="**/lib/*.jar"/>
</classpath>
</taskdef>

<target name="main">
<testSpec name="myTest">
<config
host="www.myserver.com"
port="8080"
protocol="http"
basepath="myApp" />
<steps>
<invoke
stepid="get Login Page"
url="login" />
<verifytitle
stepid="we should see the login title"
text="Login Page" />
</steps>
</testSpec>
</target>
</project>
ant -buildfile SimpleTest.xml
<?xml version="1.0"?>
<project name="SimpleTest" basedir="." default="main">

<taskdef file="${webtest.home}/webtestTaskdefs.properties">
<classpath>
<fileset dir="${webtest.home}" includes="**/lib/*.jar"/>
</classpath>
</taskdef>

<target name="main">
<testSpec name="myTest">
<config
host="www.myserver.com"
port="8080"
protocol="http"
basepath="myApp" />
<steps>
<invoke
stepid="get Login Page"
url="login" />
<verifytitle
stepid="we should see the login title"
text="Login Page" />
</steps>
</testSpec>
</target>
</project>
<?xml version="1.0"?>
<project name="SimpleTest" basedir="." default="main">

<taskdef file="${webtest.home}/webtestTaskdefs.properties">
<classpath>
<fileset dir="${webtest.home}" includes="**/lib/*.jar"/>
</classpath>
</taskdef>

<target name="main">
<testSpec name="myTest">
<config
host="www.myserver.com"
port="8080"
protocol="http"
basepath="myApp" />
<steps>
<invoke
stepid="get Login Page"
url="login" />
<verifytitle
stepid="we should see the login title"
text="Login Page" />
</steps>
</testSpec>
</target>
</project>
Simplifying Test Scripts
Include XML Entities
Ant Techniques
 Include Buildfiles

 Access Property Files

 Define Immutable Properties


definition.xml
<taskdef file="${webtest.home}/webtestTaskdefs.properties">
<classpath>
<fileset dir="${webtest.home}" includes="**/lib/*.jar"/>
</classpath>
</taskdef>

config.xml
<config
host="www.myserver.com"
port="8080"
protocol="http"
basepath="myApp" />
<?xml version="1.0"?>
<!DOCTYPE project SYSTEM "WebTest.dtd"[
<!ENTITY definition SYSTEM "definition.xml">
<!ENTITY config SYSTEM "config.xml">
]>

<project name="SimpleDemo" basedir="." default="main">

&definition;

<target name="main">
<testSpec name="myTest">
&config;
<steps>
<invoke
stepid="get Login Page"
url="login" />
<verifytitle
stepid="we should see the login title"
text="Login Page" />
</steps>
</testSpec>
</target>
</project>

ant -buildfile SimpleTest.xml


..
<property name="url" value="login?scott="/>

<target name="main">
<testSpec name="myTest">
&config;
<steps>
<invoke
stepid="bad login"
url="${url}wolf" />
<verifytitle text="Sorry, bad login" />
<invoke
stepid="proper login"
url="${url}tiger" />
<verifytitle text="Welcome" />
</steps>
</testSpec>
</target>
..
Putting it all Together
<?xml version="1.0"?>

<!DOCTYPE project [
<!ENTITY login SYSTEM "file:../modules/login.xml">
<!ENTITY step SYSTEM "file:../modules/step.xml">
]>

<project name="loginAndStep"
default="case" basedir="..">

<target name="case">
<testSpec name="loginAndStep">
&config;
<steps>
&login;
&step;
</steps>
</testSpec>
</target>
</project>
loginAndStep.xml
<invoke
stepid="open start page"
url="${start.page}" />

<setinputfield
stepid="set user name"
name="username"
value="${user}" />

<setinputfield
stepid="set password"
name="password"
value="${login.ok.password}" />

<clickbutton
stepid="Click the submit button"
label="OK"/>

<verifytitle
stepid="Is it the next page?"
text="${login.ok.title}" />

login.xml
properties/english.properties properties/french.properties

user=scott user=pascal
login.ok.password=tiger login.ok.password=fleur
login.ok.title=Welcome login.ok.title=Bienvenue
<?xml version="1.0"?>

<project name="LanguageIndependentUseCase"
default="testSuite" basedir=".">

<property name="start.page" value="myStartPage"/>

<target name="testSuite">
<ant antfile="UseCases/LoginAndStep.xml"/>
<ant antfile="UseCases/UseCase2.xml"/>
</target>

</project>
<?xml version="1.0"?>
<!DOCTYPE project SYSTEM "WebTest.dtd"[
<!ENTITY definition SYSTEM "includes/definition.xml">
<!ENTITY config SYSTEM "includes/config.xml">
]>

<project name="FullSuite" default="testSuite" basedir=".">

<property name="suite.xml" value="AllUseCases.xml" />

<target name="testSuite"
depends="testSuiteFrench,testSuiteEnglish" />

<target name="testSuiteFrench">
<ant antfile="${suite.xml}">
<property file="properties/french.properties"/>
</ant>
</target>
<target name="testSuiteEnglish">
<ant antfile="${suite.xml}">
<property file="properties/english.properties"/>
</ant>
</target>

</project>
Testing PDF Documents
<project name="SimplePDFTest" basedir="." default="main">

<taskdef file="${webtest.home}/webtestTaskdefs.properties">
<classpath>
<fileset dir="${webtest.home}" includes="**/lib/*.jar"/>
</classpath>
</taskdef>

<target name="main">
<testSpec name="myTest">
<config
host="www.myserver.com"
port="8080"
protocol="http"
basepath="myApp"
saveresponse="true"/>
<steps>
<invoke
stepid="get overview page"
url="overview.html" />
<invoke
stepid="get PDF document"
url="publicData.pdf"/>
<verifyPdfTitle
title=".+public.*"
regex="true"/>
<verifyPdfPageCount
count="2"/>
</steps>
</testSpec>
</target>
</project>
<project name="EncryptedPDFTest" basedir="." default="main">

<taskdef file="${webtest.home}/webtestTaskdefs.properties">
<classpath>
<fileset dir="${webtest.home}" includes="**/lib/*.jar"/>
</classpath>
</taskdef>

<target name="main">
<testSpec name="myTest">
<config
host="www.myserver.com"
port="8080"
protocol="http"
basepath="myApp"
saveresponse="true"/>
<steps>
<invoke
stepid="get overview page"
url="overview.html" />
<invoke
stepid="get PDF document"
url="privateData.pdf"/>
<decryptPdfDocument
password="mymaster"/>
<verifyPdfText
startPage="2"
endPage="3"
text="All rights reserved."/>
</steps>
</testSpec>
</target>
</project>
Debugging PDF Documents
.../WEBTEST_HOME/lib/java -jar pdfUnit.jar
Best Practices
Strike Two - You're Out!
Keep targets short
Use stepid
Fail first
One test at a time
Use a proper editor
Learn XPATH
Database Setup
Summary
SimpleTest
Simplifying Tests
 Using Entities
 Using Properties

Putting it all Together


PDF Documents
Best Practices
Struts University Series

S-ar putea să vă placă și