
On my page about creating a web app I described how to quickly set up a web app project with Maven and Eclipse. I then showed how to run the web app using Jetty. Here we show how to set up some integration testing using Selenium.
First we need to write at least one test case in Java for us to run. If you're familiar with Unit testing and Maven, you may be used to placing your test cases in src/test/java and post-fixing the class name with "Test" so that Maven picks it us and runs it as a Unit test. We're going to post-fix our tests with "IT" instead. This is the default for Integration Tests. Unit testing should always be quick, so that teams can check in code and get quick feedback on errors if a CI system is in operation. As selenium tests take longer to run than pure Unit testing, we want to move these into a different phase - the Integration Test phase.
First open up eclipse and if you're following the demo project we created earlier, select that project. This project doesn't have a source folder for tests, so that the first thing to create.
To create a source folder for our tests, right click on the project, and select "Build Path > New Source Folder...". Then enter a name of /src/test/java and click Finish.
Now create a new Java class in the new source folder as below. The class name should end in "IT" to be picked up as an integration test.
Now put in the following code to our Java class. The com.thoughtworks.selenium.DefaultSelenium package is not in our classpath at the moment. We'll tackle that in a minute, by adding it to Maven's POM file. You would probably want to expand on this very simple selenium test for a real project!
package uk.co.itforeveryone.demo; import junit.framework.TestCase; import com.thoughtworks.selenium.DefaultSelenium; public class HomeUrlIT extends TestCase { protected DefaultSelenium createSeleniumClient(String url) throws Exception { return new DefaultSelenium("localhost", 4444, "*firefox", url); } public void testBaseForm() throws Exception { DefaultSelenium selenium = createSeleniumClient("http://localhost:8080/"); selenium.start(); selenium.open("/"); assertTrue(selenium.isTextPresent("Hello")); assertTrue(selenium.isTextPresent("World")); selenium.stop(); } }
Now, let's edit the POM file. There are quite a few additions here, so let's go through them:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>uk.co.itforeveryone</groupId> <artifactId>demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>demo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <scope>test</scope> <version>2.0rc3</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <scope>test</scope> <version>2.0rc3</version> </dependency> </dependencies> <build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.0.0.M3</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <contextPath>/</contextPath> <scanIntervalSeconds>10</scanIntervalSeconds> <stopKey>STOP</stopKey> <stopPort>8005</stopPort> <port>8080</port> </configuration> <executions> <execution> <id>start-jetty</id> <phase>pre-integration-test</phase> <goals> <!-- stop any previous instance to free up the port --> <goal>stop</goal> <goal>run-exploded</goal> </goals> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> <daemon>true</daemon> </configuration> </execution> <execution> <id>stop-jetty</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> <!-- Selenium server (default port 4444) --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>selenium-maven-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>start-selenium</id> <phase>pre-integration-test</phase> <goals> <goal>start-server</goal> </goals> <configuration> <background>true</background> </configuration> </execution> <execution> <id>stop-selenium</id> <phase>post-integration-test</phase> <goals> <goal>stop-server</goal> </goals> </execution> </executions> </plugin> <!-- Failsafe for integration testing --> <!-- This ensures that post-integration tests are run even if testing fails --> <!-- Integration tests are *IT.java by default --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.9</version> <executions> <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> </goals> </execution> <execution> <id>verify</id> <goals> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
This is simple a matter of running the Maven "verify" goal. You may also want to run the "clean" goal too.
You can run mvn verify (or mvn clean verify) from the command line, or right mouse button click on "pom.xml" and Select "Run as > Run Configurations" then add verify as a run configuration.After the run, Test reports can be found in target/failsafe-reports. You may need to refresh the view in Eclipse to see this folder.
You can change the browser type used for testing by changing the call to DefaultSelenium in the Java Test class. Here
we use IE instead:
return new DefaultSelenium("localhost", 4444, "*iexplore", url);
Simply running mvn package will create the project war file in the target directory. Now that you have run your automated tests, feel free to deploy it with careless adandon!