如何使用IntelliJ从Selenium/TestNG Java文件创建可执行的jar文件?

11

我已经谷歌搜索了几天,试图找出如何完成这个任务,如果有人以前做过这个,我会非常感谢帮助。

我在IntelliJ中创建了一个自动化测试项目,用于模拟用户与Web应用程序的交互。

我想将该自动化测试(使用Selenium和TestNG创建的Java程序)放入可执行的jar文件中,让其他人可以通过双击jar文件来运行它。

每次我尝试通过导航到“Project Structure -> Artifact -> + -> Jar -> From modules with dependencies”创建jar文件时,它最终创建了一个声称是它的jar文件。

"Could not find or load the main class <package.MainClass> "

当我尝试使用以下命令运行它:

java -jar MyProject.jar <Manifest Path>

你知道我为什么会一直收到这个错误吗?或者有没有成功完成这个操作的方法?

此外,这是我的pom.xml文件:

<groupId>TestAutomation</groupId>
<artifactId>TestAutomation</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.test.automation.Executable</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.39.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.40.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
1个回答

10

我最终搞清楚了,如果有人遇到这个问题,这就是我成功创建和运行jar文件的方法...

我必须将我的pom.xml文件更改为以下内容:

<groupId>TestAutomation</groupId>
<artifactId>TestAutomation</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.test.automation.Executable</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.40.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

接着,我不得不调整我的主方法,以免使用任何与 TestNG 相关的调用。例如,我不能在我的主方法中使用像这样的内容:

    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] {WordProfFonts2Set0.class});
    testng.addListener(tla);
    testng.run();

最后,这里是创建适当的jar文件的步骤:

  1. 从顶部菜单选择“文件”>“Project Structure…”
  2. 在左侧菜单上选择“Artifact”,然后点击“+”
  3. 选择“Jar” > “From modules with dependencies…”
  4. 使用浏览按钮选择您的主类
  5. 选中“提取到目标jar”旁边的单选按钮,然后点击“确定”
  6. 点击“+”,然后选择“Module Test Output”
  7. 在右侧的可用元素窗格中展开项目名称,并选择所有Maven文件,然后将它们移动到在左侧窗格中创建的jar目录中
  8. 点击“确定”
  9. 从顶部菜单中选择“构建”>“构建工件…”
  10. 将鼠标悬停在创建的jar上,然后在操作下面点击“构建”

注意:

  1. 请确保将IE或Chrome驱动程序添加到您的项目资源文件夹中,并通过代码文件夹调用它,而不是计算机的硬盘驱动器。例如,做以下操作:

    File file = new File("src\test\resources\binaries\IEDriverServer.exe");

而不是这样:

File file = new File
("C:\\Users\\<Username>\\<Proj Name>\\src\\test\\java\\src\\
  test\\resources\\binaries\\IEDriverServer.exe");

然后在你电脑上存放jar文件的同一个文件夹中,创建一个含有该驱动程序的相同目录:

src
TestAutomation.jar

2. 如果使用IE浏览器,请确保启用了保护模式,并且将其设置为所有区域或不设置(在IE中,转到Internet选项... > 安全(选项卡)> 启用保护模式复选框)


1
希望这对处于同样情况的任何人都有所帮助! :-) - urbanaut
1
这正是我尝试做的事情。在这里我还缺少一些细节。你能发布你的Manifest.mf吗?以及你项目的骨架?我试图弄清楚的是,你是否将测试移动到主要源代码中,还是将其留在测试下?你的 public static void main(String[] args) 是什么样子的?谢谢! - Brad Rhoads
1
谢谢urbanaut,我也在寻找类似的解决方案,我是新手,不确定我应该在主方法中放置什么,在以前,我在主方法中使用了你提到的相同代码,但是当我创建可执行的jar文件时,它无法工作,抱怨“找不到或加载主类”,我不知道该如何解决。从您的步骤中,我有几个问题:1. 我应该在主方法中放置什么来运行我的测试?2. 在您的第6和第7步中,对我来说并不清楚,您可以详细说明一下吗?如果您可以提供截图,那就太好了。3. 如何运行此jar文件,以便调用我的testng测试?请提供您宝贵的意见。 - user790049

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接