Loading...
середу, 6 листопада 2013 р.

Integration tests with Selenium due Maven Build (JSP site)

How to run jetty server during integration testing with Maven  (on Jenkins machine), deploy and test web application there with Selenium?
What we need to do?
1. Compile sources and assemble WAR file
2. Start jetty maven plugin and deploy WAR contents there
3. Start Selenium and browse to site
4. After tests are finished shutdown jetty


Example for  test code

TestSeleniumExample.java
package com.wordpress.ykyuen;

import junit.framework.TestCase;

import com.thoughtworks.selenium.DefaultSelenium;

public class TestSeleniumExample {
 
 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("/");
  //Do your test stuff here
  selenium.stop();
 }
}

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
   
Done =)
Reference:

0 коментарі:

 
TOP