Over a million developers have joined DZone.

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

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}