Added by jongb, last edited by knutm on Mar 18, 2009  (view change)

Labels:

maven maven Delete
webtesting webtesting Delete
jetty jetty Delete
selenium selenium Delete
Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.
Full Size
A Gliffy Diagram named: CiSeleniumSetup

Note!The setup is version dependent! (not for JUnit)

Junit:

<dependencies>
...
 <!-- Junit -->
 <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.5</version>
    <scope>test</scope>
 </dependency>

  <!-- Selenium -->
  <dependency>
     <groupId>org.openqa.selenium.client-drivers</groupId>
     <artifactId>selenium-java-client-driver</artifactId>
     <version>1.0-beta-1</version>
     <scope>test</scope>
  </dependency>
...
</dependencies>

Plugins:

    <build>
        <plugins>

<plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.10</version>
        <configuration>
 
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
        </configuration>
        <executions>
                <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                                <goal>run</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>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>selenium-maven-plugin</artifactId>
                <version>1.0-rc-1</version>
                <executions>
                    <execution>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start-server</goal>
                        </goals>
                        <configuration>
                            <background>true</background>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <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>


        </plugins>
    </build>

Selenium tests can be recorded, exported to Java code and used without any major changes.

The test below has been sligtly changed.

package tutorial;

import org.junit.Test;

import com.thoughtworks.selenium.SeleneseTestCase;
import org.junit.Before;
import org.junit.Ignore;


public class IndexActionTest extends SeleneseTestCase {


    @Before
	public void setUp() throws Exception {
		setUp("http://localhost:8080/", "*chrome");
	}

    @Test
	public void testNew() throws Exception {
        selenium.open("/coolapp/index.action");
        selenium.waitForPageToLoad("1500000");
        selenium.isTextPresent("Welcome, World!");
	}

}

If you don't want to have tests to have to run in Maven and have the dependency on external stuff, you can use Jetty directly from your JUnit test:

import net.sourceforge.jwebunit.WebTester;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

public class IndexActionTest {

    private static int webServerPort;

    @BeforeClass
    public static void startJetty() throws Exception {
        Server server = new Server(0);
        server.addHandler(new WebAppContext("src/main/webapp", "/"));
        server.start();

        webServerPort = server.getConnectors()[0].getLocalPort();
    }

    private WebTester tester = new WebTester();

    @Before
    public void startClient() {
        tester.getTestContext().setBaseUrl("http://localhost:" + webServerPort + "/");
    }

    @Test public void indexPageShouldContainWelcomeText() {
        tester.beginAt("index.html");
        tester.assertTextPresent("Hello World");
    }

}

This test is 100 % self contained: It can be run from your IDE or from Maven and it requires not other setup.

Dependencies:

  • org.mortbay.jetty:jetty (tested with 6.1.14)
  • junit (tested with 4.5)
  • net.sourceforge.jwebunit:jwebunit-htmlunit-plugin (tested with 1.3)

This example uses JWebUnit instead of Selenium. There's no reason why Selenium couldn't be used the same way, but I've always run into quicksand when I've tried to use it. (New repositories, dependency madness, external running processes, and lately: A java.lang.SecurityException: sealing violation).

All content on this wiki is licensed under a Creative Commons Attribution 3.0 Unported License.