简体   繁体   English

如何使用junit和maven运行硒测试

[英]How to run selenium test using junit and maven

I want to create simply selenium project for Jenkins. 我想为詹金斯创建一个简单的硒项目。 When I run my test with Runner.java then output show me 0 scenarios. 当我使用Runner.java运行测试时,输出显示0个场景。 When I run my test with mvn, egmvn clean verify -Dcucumber.options="--tags @smoke" output show me BUILD SUCCESS 当我使用mvn运行测试时,egmvn clean验证-Dcucumber.options =“-tags @smoke”输出向我显示构建成功

All my classes is in src/test/java/package 我所有的课程都在src / test / java / package中

Runner: src/test/java/runner/Runner.java 亚军:src / test / java / runner / Runner.java

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

cucumber's file src/test/resources/login/login.feature src/test/resources/smoke/page.feature 黄瓜的文件src / test / resources / login / login.feature src / test / resources / smoke / page.feature

Below is screenshot my folders structure https://prnt.sc/nwov73 下面是我的文件夹结构的截图https://prnt.sc/nwov73

I have no idea where is problem ;/ I used google before I asked and I not found solution... 我不知道哪里出了问题; /我在问之前没有找到解决方案...

@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>

Edit: I change Runner.java to RunnerTest.java. 编辑:我将Runner.java更改为RunnerTest.java。 Mvn execute tests but all tests fail. MVN执行测试,但所有测试均失败。 All steps has "You can implement missing steps with the snippets below:" ;/ 所有步骤均带有“您可以使用以下代码片段实现缺少的步骤:”; /

Edit2: I remove "@" from tags in RunnerTest.java and currently I have always "0 Scenarios 0 Steps 0m0.093s" Edit2:我从RunnerTest.java中的标签中删除了“ @”,并且目前我总是“ 0个场景0个步骤0m0.093s”

Edit 3 - problem solved. 编辑3-问题已解决。 I don't have path in glue to class where I have driver config. 我没有胶水的路径到我有驱动程序配置的类。 actually my glue - glue = {"glue", "config/browser"}. 实际上是我的胶水-胶水= {“胶水”,“配置/浏览器”}。 Before I had only {"glue"}. 在我只有{“ glue”}之前。

Please update the POM dependencies correctly. 正确更新POM依赖关系。 gherkin, junit etc are transitive dependencies which would be taken care by maven when you add direct dependencies. gherkin,junit等是可传递依赖项,当您添加直接依赖项时,maven会小心处理。

Key Point : We shall not mix direct & transitive dependencies specially their versions! 关键点:我们将不能混用直接和传递依赖,特别是它们的版本! Doing so can cause unpredictable outcome. 这样做可能导致不可预测的结果。

You may prefer below correct set of io.cucumber dependencies and update cucumber v as per your framework need. 您可能更喜欢下面的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM