spring - Maven Spring Selenium 集成测试 - 在 Selenium 之前运行 Tomcat

标签 spring maven selenium maven-cargo maven-tomcat-plugin

我正在学习如何在 Maven 中运行 Selenium 集成测试。我已经使用 Cargo 配置了 Selenium 和 Tomcat Maven 插件。 这是我的 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/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 正在启动并且我的应用程序正在运行。但是集成测试尚未完成。

最佳答案

要运行集成测试,您必须告诉 maven 运行 integration-test 阶段。您可以运行 mvn Integration-testmvc install (install 可以工作,因为它在 Maven 中位于 integration-test 之后阶段)

@参见Maven Build Lifecycle

顺便说一句:你应该看看 maven-failsave-plugin 。它是一个类似于 Surefire 的测试插件,但旨在运行集成测试。 - 所以你可以在你的pom中有两个插件(surefire用于单元测试,failave用于集成测试),所以你不需要对surefire进行太多的重新配置。

关于spring - Maven Spring Selenium 集成测试 - 在 Selenium 之前运行 Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15715349/

相关文章:

java - 配置 Junit + spring + hibernate 4 + maven : Null pointer getting session

java - 在同一个 Selenium 网格节点上运行 PhantomJS 和 Chrome 浏览器

java - 抽象类中的 Selenium FindBy 注释

java - Spring静态资源映射: controller vs addResourceHandler

java - 在类上使用 @DirtiesContext 注释究竟意味着什么?

java - 我可以创建 spring mvc 自定义验证注释而不链接到 validator 实现吗

Java Selenium 无法登录GMail

spring - JSF,如何启动一个单独的线程来处理pdf生成并直接渲染 View

java - 如何在 Selenium 中为跨度文本使用 @FindBy 注释?

Android Studio 在磁盘上添加外部 maven 项目作为依赖项