2013-03-30 71 views
2

我正在学习如何在Maven中运行Selenium集成测试。我已经使用Cargo配置了Selenium和Tomcat Maven Plugin。 这是我的pom.xml文件:Maven Spring Selenium集成测试 - 在Selenium之前运行Tomcat

<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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.mycompany</groupId> 
    <artifactId>SeleniumTest</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>SeleniumTest</name> 

    <properties> 
     <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>tomcat-maven-plugin</artifactId> 
      <version>1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc-struts</artifactId> 
      <version>2.5.6</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.14</version> 
     </dependency> 
     <dependency> 
      <groupId>org.testng</groupId> 
      <artifactId>testng</artifactId> 
      <version>6.8</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.codehaus.cargo</groupId> 
      <artifactId>cargo-maven2-plugin</artifactId> 
      <version>1.3.3</version> 
     </dependency> 
     <dependency> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>selenium-maven-plugin</artifactId> 
      <version>2.3</version> 
     </dependency> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-firefox-driver</artifactId> 
      <version>2.31.0</version> 
     </dependency> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>6.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>${project.groupId}</groupId> 
      <artifactId>SeleniumTest</artifactId> 
      <version>${project.version}</version> 
      <type>war</type> 
      <scope>provided</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <testSourceDirectory>src/main/java/test/integrationTests</testSourceDirectory> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <executions> 
        <execution> 
         <goals> 
          <goal>testCompile</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <compilerArguments> 
         <endorseddirs>${endorsed.dir}</endorseddirs> 
        </compilerArguments> 
        <suiteXmlFiles> 
         <suiteXmlFile>src/main/java/test/integrationTests/testng.xml</suiteXmlFile> 
        </suiteXmlFiles> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.1.1</version> 
       <configuration> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.1</version> 
       <executions> 
        <execution> 
         <phase>validate</phase> 
         <goals> 
          <goal>copy</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${endorsed.dir}</outputDirectory> 
          <silent>true</silent> 
          <artifactItems> 
           <artifactItem> 
            <groupId>javax</groupId> 
            <artifactId>javaee-endorsed-api</artifactId> 
            <version>6.0</version> 
            <type>jar</type> 
           </artifactItem> 
          </artifactItems> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.14</version> 
       <executions> 
        <execution> 
         <id>default-test</id>         
         <configuration> 
          <skipTests>true</skipTests> 
         </configuration> 
        </execution> 

        <execution> 
         <id>surefire-it</id> 
         <phase>integration-test</phase> 
         <goals> 
          <goal>test</goal> 
         </goals> 
         <configuration> 
          <skipTests>false</skipTests> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.cargo</groupId> 
       <artifactId>cargo-maven2-plugin</artifactId> 
       <version>1.3.3</version> 
       <configuration> 
        <wait>true</wait> 
        <container> 
         <containerId>tomcat7x</containerId> 
         <type>installed</type> 
         <home>D:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27</home> 
        </container> 
        <configuration> 
         <type>existing</type> 
         <home>D:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27</home> 
        </configuration> 

        <deployables> 
         <deployable> 
          <pingURL>http://localhost:8080/SeleniumTest</pingURL> 
          <pingTimeout>300000</pingTimeout> 
         </deployable> 
        </deployables> 
        <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> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

这是NetBeans的日志,当我说构建

------------------------------------------------------------------------ 
Building SeleniumTest 1.0-SNAPSHOT 
------------------------------------------------------------------------ 

[dependency:copy] 

[resources:resources] 
[debug] execute contextualize 
Using 'UTF-8' encoding to copy filtered resources. 
Copying 0 resource 

[compiler:compile] 
Nothing to compile - all classes are up to date 

[resources:testResources] 
[debug] execute contextualize 
Using 'UTF-8' encoding to copy filtered resources. 
skip non existing resourceDirectory C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\src\test\resources 

[compiler:testCompile] 
Compiling 1 source file to C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\test-classes 

[compiler:testCompile] 
Compiling 1 source file to C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\test-classes 

[surefire:test] 
Tests are skipped. 

[war:war] 
Packaging webapp 
Assembling webapp [SeleniumTest] in [C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\SeleniumTest-1.0-SNAPSHOT] 
Processing war project 
Copying webapp resources [C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\src\main\webapp] 
Webapp assembled in [454 msecs] 
Building war: C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\SeleniumTest-1.0-SNAPSHOT.war 

[surefire:test] 
Surefire report directory: C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\surefire-reports 

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Running test.integrationTests.SimpleTest 
Configuring TestNG with: TestNG652Configurator 

正如你所看到的,它只是建立.war文件和不要启动我的tomcat容器,也不要在其上部署.war文件。 如果我说在Netbeans中运行运行,我可以看到Tomcat启动并运行我的应用程序。然而,集成测试没有完成。

回答

4

要运行集成测试,您必须告诉maven运行integration-test阶段。要么你运行mvn integration-testmvc installinstall的作品,因为它在内行阶段integration-test来到后)

@see Maven Build Lifecycle

BTW:你应该看看maven-failsave-plugin。它是一个像surefire一样的测试插件,但是用于运行集成测试。 - 所以你可以在你的pom中有两个插件(surefire for unit tests和failave用于集成测试),所以你不需要重新配置surefire。

+0

谢谢,这对我有点帮助。 –

相关问题