Ken Rimple heads Chariot Solutions' training and mentoring programs, and has developed and/or delivered courseware and seminars in a variety of technologies such as Maven, OSGi, Groovy, Grails and Spring. Throughout his career, Ken has always made it a priority to teach others what he has learned. Ken has served as the technical co-chair of both the Fall Forecast 2008 Cloud Computing Conference and the 2009 - 2012 Emerging Technologies for the Enterprise conferences. He hosts a popular podcast, the Chariot TechCast, and has led or participated in projects written in Java since Java 1.0.2. Ken taught the first Philadelphia-area Sun Introduction to Java course in the late 1990s. He is the co-author (along with Srini Penchikala) of Spring Roo in Action for Manning Publications. He is also an avid photographer and jazz drummer. Ken is a DZone MVB and is not an employee of DZone and has posted 35 posts at DZone. You can read more from them at their website. View Full User Profile

Roo, Selenium WebDriver,Failsafe and JUnit - Smores, anyone?

01.17.2012
| 3756 views |
  • submit to reddit

S'Mores are about the perfect mix of sugar, sugar and chocolate. So, the programming equivalent would be getting your Selenium tests separated from your unit tests, and running in an API that just needs a web server, not a separate Selenium server, right?

(Ok, I'm stretching it).

First, here is an integration test, ITCourseSelenium.java that uses the Selenium WebDriver API. Look how simple the code is:

package org.rooinaction.coursemanager.web;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ITCourseSelenium {

  private WebDriver webDriver;

  @Before
  public void setUp() throws Exception {
    webDriver = new FirefoxDriver();
  }

  @Test

  public void testCourseCreate() throws Exception {
    webDriver.get("http://localhost:8080/coursemanager/courses?form");
    // implicitly wait 10 seconds for anything needed
    // to appear...
    webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    webDriver.findElement(By.id("_name_id")).sendKeys("someName1");    
    webDriver.findElement(By.id("_description_id")).sendKeys("someDescription1");
    webDriver.findElement(By.id("_maxiumumCapacity_id")).sendKeys("1");
    webDriver.findElement(By.id("_createdDate_id")).sendKeys("5/1/11");
    webDriver.findElement(By.id("proceed")).click();
    Assert.assertEquals(true, 0 < webDriver.getPageSource().indexOf("Show Course"));
  }

}

Next up, we have the Maven configuration for the various plugins to install Selenium (forget the current Selenium Roo add-on for this, it only sets up HTML tests and doesn't include the Java API, plus doesn't install WebDriver and needs to run on a port, which is tricky):

Selenium Configuration

Add these elements to your pom.xml file to set up the configuration:


<dependencies>
  ...
    <!-- install the WebDriver API -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.16.1</version>
      <scope>test</scope>
    </dependency>
...
</dependencies>

<build>

...
    <plugin>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>8.1.0.RC2</version>
      <configuration>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <stopKey>stop</stopKey>
        <stopPort>9999</stopPort>
        <webAppConfig>
          <contextPath>/${project.name}</contextPath>
        </webAppConfig>
      </configuration>
      <executions>
        <execution>
          <id>start-jetty</id>
          <goals>
            <goal>run</goal>
          </goals>
          <phase>pre-integration-test</phase>
          <configuration>
            <scanIntervalSeconds>0</scanIntervalSeconds>
            <daemon>true</daemon>
          </configuration>
        </execution>
        <execution>
          <id>stop-jetty</id>
          <goals>
            <goal>stop</goal>
          </goals>
          <phase>post-integration-test</phase>
          <configuration>
            <stopKey>stop</stopKey>
            <stopPort>9999</stopPort>
          </configuration>
        </execution>
      </executions>
    </plugin>
...

    <!-- failsafe runs integration tests, ending or 
         starting with IT. -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>2.8.1</version>
      <executions>
        <execution>
          <id>integration-test</id>
          <goals>
            <goal>integration-test</goal>
          </goals>
          <configuration>
            <includes>
              <!-- add other patterns not included with
                   standard tests. -->
              <include>**/IT*</include>
            </includes>
          </configuration>
        </execution>
        <execution>
          <id>verify</id>
          <goals>
            <goal>verify</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
...
  </plugins>
</build>

These fragments configure a jetty web server, which starts up before integration testing. They also configure the maven-failsafe-plugin, which is attached to the integration-test and verify Maven lifecycle phases.

Running the tests

To run your tests, just issue the mvn verify command. All tests starting or ending with IT will run after the web application starts, and will be recorded as test results in target/failsafe-reports.

Enjoy.

 

From http://www.rimple.com/tech/2012/1/8/roo-selenium-webdriverfailsafe-and-junit-smores-anyone.html

Published at DZone with permission of Ken Rimple, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Justin Forder replied on Tue, 2012/01/17 - 5:19am

The original article, http://www.rimple.com/tech/2012/1/8/roo-selenium-webdriverfailsafe-and-junit-smores-anyone.html , goes on to show the Maven configuration needed to build and run integration tests using Failsafe and Jetty.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.