DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Roo, Selenium WebDriver,Failsafe and JUnit - Smores, anyone?

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

Ken Rimple user avatar by
Ken Rimple
·
Jan. 17, 12 · Java Zone · Interview
Like (0)
Save
Tweet
4.65K Views

Join the DZone community and get the full member experience.

Join For Free

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

integration test JUnit

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Portfolio Architecture Examples: Retail Collection
  • Upsert in SQL: What Is an Upsert, and When Should You Use One?
  • A Smarter Redis
  • 11 Reasons To Use Selenium for Automation Testing

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo