【问题标题】:How to run selenium test using junit and maven如何使用 junit 和 maven 运行 selenium 测试
【发布时间】:2019-06-02 18:48:28
【问题描述】:

我想为 Jenkins 创建简单的 selenium 项目。 当我使用 Runner.java 运行测试时,输出会显示 0 个场景。 当我使用 mvn 运行测试时,例如 mvn clean verify -Dcucumber.options="--tags @smoke" 输出显示我构建成功

我所有的课程都在 src/test/java/package 中

跑步者: src/test/java/runner/Runner.java

胶水: src/test/java/glue/login/LoginDef.java src/test/java/glue/dashboard/DashboardDef.java

黄瓜的文件 src/test/resources/login/login.feature src/test/resources/smoke/page.feature

以下是我的文件夹结构截图 https://prnt.sc/nwov73

我不知道问题出在哪里;/我在询问之前使用了 google,但我没有找到解决方案...

@RunWith(Cucumber.class)
@CucumberOptions(
        strict = true,
        features = {"src/test/resources/features"},
        plugin = {"json:build/reports/cucumberRunner.json", "html:build/reports/html", "pretty"},
        monochrome = true,
        tags = {"not @ignore", "not @wip", "@login", "@smoke"},
        glue = {"src/test/java/glue", "hooks"}
)
public class Runner {
}
    <properties>
        <io.cucumber.gherkin>5.1.0</io.cucumber.gherkin>
        <io.cucumber.common>4.3.1</io.cucumber.common>
        <selenide>5.2.3</selenide>
        <lombok>1.18.4</lombok>
        <junit>4.12</junit>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
            <version>${io.cucumber.gherkin}</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>${io.cucumber.common}</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${io.cucumber.common}</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>io.cucumber</groupId>-->
            <!--<artifactId>cucumber-jvm</artifactId>-->
            <!--<version>${io.cucumber.common}</version>-->
        <!--</dependency>-->

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit}</version>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok}</version>
            <scope>provided</scope>
        </dependency>

        <!-- selenide -->
        <dependency>
            <groupId>com.codeborne</groupId>
            <artifactId>selenide</artifactId>
            <version>${selenide}</version>
        <!-- <scope>test</scope>-->
     </dependency>
    </dependencies>

    <build>
        <!--<testSourceDirectory>src/test/resources/features/</testSourceDirectory>-->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
            </plugin>
        </plugins>
    </build>
</project>

编辑:我将 Runner.java 更改为 RunnerTest.java。 Mvn 执行测试,但所有测试都失败。所有步骤都有“您可以使用下面的 sn-ps 实现缺少的步骤:”;/

Edit2:我从 RunnerTest.java 的标签中删除了“@”,目前我一直 "0 场景 0 步 0m0.093s"

编辑 3 - 问题已解决。 我没有通往我有驱动程序配置的类的路径。实际上我的胶水 - 胶水 = {“胶水”,“配置/浏览器”}。在我只有 {"glue"} 之前。

【问题讨论】:

  • 黄瓜找不到您的步骤定义实现。将胶水选项更改为胶水 =“胶水”。没有看到任何钩子包,为什么要添加它。不需要 gherkin 和 junit 依赖项,它们将被自动拉入。对于通过surefire自动包含测试类检查这个 - maven.apache.org/surefire/maven-surefire-plugin/examples/…
  • 我将胶水选项更改为胶水 = {"glue"},我删除了 gherkinjunit 仍然有 0 个场景 :(跨度>
  • 在标签选项中使用这个 -- tags = {"@login or @smoke"}
  • 不起作用,仍然 [INFO] 测试运行:0,失败:0,错误:0,跳过:0,经过的时间:0.568 秒 - 在 runner.RunnerTest 中

标签: java maven selenium junit cucumber


【解决方案1】:

正确更新 POM 依赖项。 gherkin、junit 等是传递依赖项,当您添加直接依赖项时,maven 会处理这些依赖项。

关键点:我们不会混合直接依赖和传递依赖,特别是它们的版本!这样做可能会导致不可预测的结果。

您可能更喜欢下面一组正确的 io.cucumber 依赖项,并根据您的框架需要更新 cucumber v。

 <dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.2.6</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.2.6</version>
</dependency>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-01-04
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 2015-08-19
  • 1970-01-01
相关资源
最近更新 更多