本文介绍了如何使用Selenium / TestNG java文件中的IntelliJ制作可执行jar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经谷歌搜索了几天试图弄清楚如何做到这一点,如果有人这样做之前我会非常感谢你的帮助。

I've been Googling for days trying to figure out how to do this, if anybody has done this before I would greatly appreciate the help.

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

I have an automation test project I've created in IntelliJ that automates a user interacting with a Web Application.

我想把自动化测试(用Java创建,使用Selenium和TestNG)放到一个可执行的jar文件中,其他人可以通过双击jar文件来运行。

I'd like to put that automated test (created in Java, using Selenium and TestNG) into an executable jar file that others can run by double-clicking the jar file.

每当我尝试通过导航到项目结构 - >工件 - > + - >罐子 - >从具有依赖关系的模块创建一个jar文件时,它最终会创建声称它的jar,

Every time I attempt to create a jar file by navigating to Project Structure -> Artifact -> + -> Jar -> From modules with dependencies, it ends up creating a jar that claims it,

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

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

when I attempt to run it with the following command:

java -jar MyProject.jar <Manifest Path>

我知道为什么我不断得到这个错误,或者有办法成功地做到这一点?

Any idea why I continually get this error, or have a way to do this successfully?

此外,这是我的pom.xml:

Also, here is my 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>

推荐答案

我终于想到了碰巧遇到这个问题的其他人,这就是我如何创建jar文件并成功运行...

I finally figured it out for anyone else who happens to run into this problem, this is how I got the jar file to be created and run successfully...

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

I had to change my pom.xml file to the following:

<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相关的调用。例如,我不能在我的main方法中使用类似的东西:

Then, I had to adjust my main method to not use any TestNG-related calls. For example, I could not use something like this for my main method:

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

最后,以下是创建相应jar文件的步骤:

Finally, here are the steps to get the appropriate jar file created:


  1. 从顶部菜单中选择文件>项目结构...

  2. 在左侧菜单中选择Artifact并单击' +'

  3. 选择Jar>来自具有依赖关系的模块...

  4. 使用浏览按钮选择主类

  5. 单击提取到目标jar旁边的单选按钮,然后单击确定。

  6. 单击+,然后选择模块测试输出

  7. 在右侧的可用元素窗格中,展开项目名称并选择所有Maven文件,然后将它们移动到左窗格中创建的jar目录

  8. 单击确定

  9. 从顶部菜单中选择构建>构建工件...

  10. 将鼠标悬停在创建的jar上,然后单击操作下的构建

  1. Select File > Project Structure... from the top menu
  2. Select "Artifact" on the left menu and click the '+'
  3. Select Jar > From modules with dependencies...
  4. Select your main class using the browse button
  5. Click the radio button next to, "extract to the target jar" and click "OK"
  6. Click the '+' then select "Module Test Output"
  7. In the Available Elements pane to the right, expand the project name and select all the Maven files, then move them to the jar directory being created in the left pane
  8. Click "OK"
  9. Select Build > Build Artifacts... from the top menu
  10. Hover over the jar created and click "Build" under Actions

注意:


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

  1. Be sure to add the IE or Chrome driver to your projects resources folder, and call it via the code folder, rather than the computer's hard drive. For example, do this:

文件文件=新文件(src\test\resources\binaries\IEDriverServer.exe);

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保存到的同一文件夹中创建与其中的驱动程序相同的目录电脑:

Then create the same directory with the driver in it in the same folder your jar is saved to on your computer:

src
TestAutomation.jar

2。请确保,如果使用IE,则为所有区域设置保护模式或不设置任何区域(在IE中,转到Internet选项...>安全性(选项卡)>启用保护模式复选框)

2 . Be sure, if using IE, that Protected Mode is set for either all zones or none of them (in IE, go to Internet Options... > Security (tab) > Enable Protected Mode check box)

这篇关于如何使用Selenium / TestNG java文件中的IntelliJ制作可执行jar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:05