Selenium – Integrate the Selenium Tests into Maven Build

In this article, i will show you how to integrate the Selenium tests into a Maven webapp project using the selenium-maven-plugin and cargo-maven2-plugin.

In this example, i used JUnit as the testing framework. There is no problem if you use TestNG to replace JUnit. This example is a simple Java webapp Maven project which only shows a simple html page (index.jsp). During the build, a Selenium Server is started with the webapp deployed in the Embedded Jetty. It then runs an Unit Test (Selenium Test) during the Maven integration-test life cycle. Here comes to the code.

1. Create a Maven webapp project with the following hierarchy

2. Edit the following files
SeleniumExample.java

package com.wordpress.ykyuen;

public class SeleniumExample {
	// Your code
}

 

TestSeleniumExample.java

package com.wordpress.ykyuen;

import junit.framework.TestCase;

import com.thoughtworks.selenium.DefaultSelenium;

public class TestSeleniumExample extends TestCase {
	
	protected DefaultSelenium createSeleniumClient(String url) throws Exception {
		return new DefaultSelenium("localhost", 4444, "*iexplore", url);
	}
    
	public void testSomethingSimple() throws Exception {
		DefaultSelenium selenium = createSeleniumClient("http://localhost:8080/");
		selenium.start();
		selenium.open("/selenium-maven-integration-1.0/");
		assertTrue(selenium.isTextPresent("This is my test."));
		selenium.stop();
	}
}

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
	version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

index.jsp

<head><title>Selenium Maven Integration</title></head>
<body>
	<h1>Selenium Maven Integration</h1>
	<p>This is my test.</p>
</body>

 

pom.xml

<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>com.wordpress.ykyuen</groupId>
	<artifactId>selenium-maven-integration</artifactId>
	<version>1.0</version>
	<packaging>war</packaging>
	
	<!-- Pluging Repository for cargo-maven2-plugin -->
	<pluginRepositories>
		<pluginRepository>
			<id>codehaus snapshot repository</id>
			<url>http://snapshots.repository.codehaus.org/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
		</pluginRepository>
	</pluginRepositories>
	
	<!-- Repository for selenium-java-client-driver and selenium-server -->
	<repositories>
		<repository>
			<id>openqa</id>
			<name>OpenQA Repository</name>
			<url>http://nexus.openqa.org/content/repositories/releases/</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
			<releases>
				<enabled>true</enabled>
			</releases>
		</repository>
	</repositories>
	
	<!-- Build Configuration -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.10</version>
				<configuration>
					<!-- Log to the console. -->
						<requestLog implementation="org.mortbay.jetty.NCSARequestLog">
						<!-- This doesn't do anything for Jetty, but is a workaround for a Maven bug
							that prevents the requestLog from being set. -->
						<append>true</append>
					</requestLog>
				</configuration>
			</plugin>
			<!-- ******************************************************* -->
			<!-- Start selenium-server before the integration test start -->
			<!-- ******************************************************* -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>selenium-maven-plugin</artifactId>
				<executions>
					<execution>
						<id>start-selenium-server</id>
						<phase>pre-integration-test</phase>
							<goals>
								<goal>start-server</goal>
							</goals>
							<configuration>
								<background>true</background>
								<logOutput>true</logOutput>
								<multiWindow>true</multiWindow>
							</configuration>
					</execution>
					<execution>
						<id>stop-selenium-server</id>
						<phase>post-integration-test</phase>
						<goals>
							<goal>stop-server</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- ******************************************************** -->
			<!-- Force to run the testcases in the integration-test phase -->
			<!-- ******************************************************** -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<!-- Skip the normal tests, we'll run them in the integration-test phase -->
					<skip>true</skip>
				</configuration>
				<executions>
					<execution>
						<phase>integration-test</phase>
						<goals>
							<goal>test</goal>
						</goals>
						<configuration>
							<skip>false</skip>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<!-- *********************************************************** -->
			<!-- Deploy the war in the embedded jetty of cargo2-maven-plugin -->
			<!-- *********************************************************** -->
			<plugin>
				<groupId>org.codehaus.cargo</groupId>
				<artifactId>cargo-maven2-plugin</artifactId>
				<version>1.0.2-SNAPSHOT</version>
				<executions>
					<execution>
						<id>start-container</id>
						<phase>pre-integration-test</phase>
						<goals>
							<goal>start</goal>
						</goals>
					</execution>
					<execution>
						<id>stop-container</id>
						<phase>post-integration-test</phase>
						<goals>
							<goal>stop</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<wait>false</wait>
					<container>
						<containerId>jetty6x</containerId>
						<type>embedded</type>
					</container>
				</configuration>
			</plugin>
			<!-- ******************************************************************************* -->
			<!-- Configure maven-compiler-plugin (Not related to the Selenium Maven integration) -->
			<!-- ******************************************************************************* -->
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<optimize>true</optimize>
				</configuration>
			</plugin>
		</plugins>
	</build>
	
	<!-- Dependencies -->
	<dependencies>
		<dependency>
			<groupId>org.openqa.selenium.client-drivers</groupId>
			<artifactId>selenium-java-client-driver</artifactId>
			<version>0.9.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.openqa.selenium.server</groupId>
			<artifactId>selenium-server</artifactId>
			<version>0.9.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

 

3. Run the mvn install and enjoy the power of Selenium
 

If you are a Tapestry user, you need to modify the cargo-maven2-plugin in the pom.xml. Check it out in the next post: Run Selenium in Tapestry Maven Project
 

Done =)

Reference:

6 thoughts on “Selenium – Integrate the Selenium Tests into Maven Build”

  1. I can;t get it to work.. I am getting a error in maven.. Plugin org.codehaus.cargo:cargo-maven2-plugin:1.0.2-SNAPSHOT or one of its dependencies could not be resolved:

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.