如何在maven项目中的Cucumber测试下运行GlassFish和Selenium?

时间:2013-04-16 16:43:03

标签: maven netbeans selenium glassfish cucumber

如何在POM文件中一起映射所有内容?

1 个答案:

答案 0 :(得分:1)

pom:

 <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>pl.cucumber.test</groupId>
            <artifactId>cucumber-with-glassfish</artifactId>
            <version>1.0.0</version>

            <properties>
                <jdk.version>1.7</jdk.version>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <cucumber.version>1.1.3</cucumber.version>
                <junit.version>4.11</junit.version>
                <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
                <maven-surefire-plugin.version>2.14.1</maven-surefire-plugin.version>
                <cargo-maven2-plugin.version>1.4.0</cargo-maven2-plugin.version>
                <glasfish.port>8080</glasfish.port>
                <glasfish.home>${project.build.directory}/glasfish</glasfish.home>
                <selenium.version>2.32.0</selenium.version>
            </properties>

            <dependencies>
                <dependency>
                    <groupId>info.cukes</groupId>
                    <artifactId>cucumber-java</artifactId>
                    <version>${cucumber.version}</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>info.cukes</groupId>
                    <artifactId>cucumber-junit</artifactId>
                    <version>${cucumber.version}</version>
                    <scope>test</scope>
                </dependency>

                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-java</artifactId>
                    <scope>test</scope>
                    <version>${selenium.version}</version>
                </dependency>
            </dependencies>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>${maven-compiler-plugin.version}</version>
                        <configuration>
                            <source>${jdk.version}</source>
                            <target>${jdk.version}</target>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>${maven-surefire-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>integration-test</id>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.cargo</groupId>
                        <artifactId>cargo-maven2-plugin</artifactId>
                        <version>${cargo-maven2-plugin.version}</version>
                        <configuration>
                            <container>
                                <containerId>glassfish3x</containerId>
                                <zipUrlInstaller>
                                    <url>http://dlc.sun.com.edgesuite.net/glassfish/3.1.2.2/release/glassfish-3.1.2.2.zip</url>
                                    <downloadDir>${basedir}/downloads</downloadDir>
                                </zipUrlInstaller>
                                <output>${project.build.directory}/container.log</output>
                                <append>false</append>
                                <log>${project.build.directory}/cargo.log</log>
                                <systemProperties>
                                    <mySystemProperty>value</mySystemProperty>
                                </systemProperties>
                            </container>
                            <configuration>
                                <home>${glasfish.home}</home>
                                <properties>
                                    <cargo.servlet.port>${glasfish.port}</cargo.servlet.port>
                                    <cargo.logging>medium</cargo.logging>
                                </properties>
                            </configuration>
                            <!--
                            <deployables>
                                 <deployable>
                                                      <groupId>pl.cucumber.warproject</groupId>
                                                      <artifactId>warproject</artifactId>
                                                      <version>1.0</version>
                          <type>war</type>
                                   <properties>
                                     <context>mycontext</context>
                                  </properties>
                                </deployable>
                            </deployables>
                             -->
                        </configuration>
                        <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>
                    </plugin>
                </plugins>
            </build>
        </project>

RunIT:

@RunWith(Cucumber.class)
public class RunIT {
    //
}

TestTitlePages:

public class TestTitlePages {

    private WebDriver driver;

    @Given("^Connect to the given address$")
    public void a_User_has_no_money_in_their_current_account() {
        // Create a new instance of the html unit driver
        driver = new HtmlUnitDriver();

    }

    @When("^Address is (.*)$")
    public void connectTo(String address) {
        //Navigate to desired web page
        driver.get(address);
    }

    @Then("^Title is (.*)$")
    public void titleIs(String expectedTitle) {
        //get the title of the page
        String actualTitle = driver.getTitle();
        // verify title
        Assert.assertEquals(actualTitle,expectedTitle);
    }

}

test.feature:

Feature: Check the title

Scenario: Connect to the given address and check the title
Given Connect to the given address
When Address is http://www.google.com
Then Title is Google

项目结构:

src/test/
    java/
        pl/cucumber/test/
            RunIT.java
            TestTitlePages.java
    resources
        pl/cucumber/test
            test.feature

致电mvn verify