首页 > 解决方案 > 我们应该在 java 测试文件夹还是主文件夹中编写 testng selenium 代码?

问题描述

我正在编写一组硒测试,我想构建一个 jar 并在 docker 上运行,我正在做一个基于 maven 的项目。

在大多数视频教程中,我看到这些测试都写在 test 文件夹中。但在这种情况下,它会构建到 jar 文件中,我是否能够执行这些文件?

测试 selenium 项目的合适文件夹结构是什么?

标签: javaseleniumselenium-webdriver

解决方案


是的,它将基于您的 pom.xml 构建配置。

<build>
<finalName>testProject</finalName>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
            <compilerVersion>1.8</compilerVersion>
            <source>1.8</source>
            <target>1.8</target>
            <testSource>1.8</testSource>
            <testTarget>1.8</testTarget>
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
            <execution>
                <id>copy-resources</id>
                <phase>validate</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <resources>
                        <resource>
                            <directory>src/main/resources</directory>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>
                        ${project.build.directory}/libs
                    </outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
            <execution>
                <goals>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

最后一节特别告诉 maven 包含测试类[您在哪里编写测试脚本]并构建测试 jar。

你可以通过写来执行它,

java -cp testProject.jar:testProject-tests.jar:libs/*<other parameters like testNG suite or host etc>

当我使用页面对象框架时,我更喜欢将Page类src->main和Test类分开编写src-test


推荐阅读