Sunday, 23 September 2007
Adding Selenium Support To Appfuse
Selenium is a very nice browser control/automated testing system. I wanted to integrate it into my Appfuse project so I could use it to do functional testing in lieu of Canoo, which i feel is less capable. Selenium RC (remote Control) is particularly useful because it lets one automate web testing from inside of Java and hence inside of Junit. Here's how I did it.
Step 1 - Download Selenium
Go to http://www.openqa.org/selenium-rc/download.action
and download the selenium 0.9.2 release
Unpacked the file in
lib/
underneath your Appfuse root directory.
Now the files are in
lib/selenium-remote-control-0.9.2
Step 2 - Setup Library and Paths
edit /lib/lib.properties and add the follwoing:
#
# Selenium RC - http://www.openqa.org/
# selenium.version=0.9.2
selenium.dir=${lib.dir}/selenium-remote-control-${selenium.version}/
selenium-client.jar=${selenium.dir}/selenium-java-client-driver-${selenium.version}/selenium-java-client-driver.jar
selenium-server.jar=${selenium.dir}/selenium-server-${selenium.version}/selenium-server.jar
In properties.xml setup the selenium build and test environment
<!-- Selenium Compile Classpath -->
<path id="selenium.compile.classpath">
<path refid="web.compile.classpath"/>
<pathelement location="${selenium-client.jar}"/>
<pathelement location="${selenium-server.jar}"/>
</path>
<path id="selenium.test.classpath">
<path refid="web.test.classpath"/>
<pathelement location="${selenium-client.jar}"/>
<pathelement location="${selenium-server.jar}"/>
</path>
Step 3 - Setup Source Directories and add a Test
Make two source directories
test/selenium
and
src/selenium
Put your tests in test/selenium
An example Selenium test:
package appfuse.selenium;
import org.openqa.selenium.server.SeleniumServer;
import junit.framework.TestCase;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class SeleniumTest extends TestCase {
private Selenium selenium;
public void setUp() throws Exception {
String url = "http://localhost:8080/appfuse";
selenium = new DefaultSelenium("localhost", SeleniumServer.getDefaultPort(), "*firefox", url);
selenium.start();
}
protected void tearDown() throws Exception {
selenium.stop();
}
public void testLocal() throws Throwable {
selenium.open("http://localhost:8080/appfuse"
;
selenium.waitForPageToLoad("30000"
;
assertTrue(selenium.getTitle().equals("Login | AppFuse"
);
}
}
Step 5 - Setup proper build targets
In build.xml add the following tasks to support running selenium tests:
<!-- Start Selenium Test Server -->
<target name="start-selenium-server" depends="init">
<java fork="true" spawn="true" classname="org.openqa.selenium.server.SeleniumServer">
<classpath refid="web.compile.classpath"/>
</java>
</target>
<!-- Compile Selenium Modules -->
<target name="compile-selenium" depends="package-service,stage-web" description="Compile web module">
<compile module="selenium"/>
</target>
<!-- Use cargo to startup app server and run selenium tests against them -->
<target name="test-selenium" if="tomcat.home" depends="war,check-debug,start-selenium-server,compile-selenium"
description="Runs Canoo WebTests using Cargo">
<taskdef resource="cargo.tasks">
<classpath>
<fileset dir="${cargo.dir}" includes="*.jar"/>
</classpath>
</taskdef>
<echo>running canoo tests at:
http://${tomcat.server}:${http.port}/${webapp.name}</echo>
<if>
<isset property="tomcat5"/>
<then><property name="cargoId" value="tomcat5x"/></then>
<else><property name="cargoId" value="tomcat4x"/></else>
</if>
<echo message="Starting ${cargoId}..."/>
<cargo containerId="${cargoId}" id="${cargoId}" home="${tomcat.home}" action="start"
output="${test.dir}/cargo.log" wait="false">
<configuration home="${test.dir}/${cargoId}">
<property name="cargo.logging" value="high"/>
<property name="cargo.servlet.port" value="${http.port}"/>
<!--<property name="cargo.jvmargs" value="${run.brainReboot.test.debugargline}"/>-->
<deployable type="war" file="${webapp.dist}/${webapp.war}"/>
</configuration>
</cargo>
<test-module module="selenium"/>
<cargo refid="${cargoId}" action="stop"/>
</target>
Step 6 - Run Tests
Now run
ant test-selenium ;
and you will see tomcat startup and the selenium Firefox instance pop open and try and complete any tests you've setup.
Technorati Tags: java appfuse selenium automated testing
