如何使用Selenium/TestNG java文件中的IntelliJ制作可执行jar文件?

urb*_*aut 10 java selenium jar intellij-idea executable-jar

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

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

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

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

"Could not find or load the main class <package.MainClass> "
Run Code Online (Sandbox Code Playgroud)

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

java -jar MyProject.jar <Manifest Path>
Run Code Online (Sandbox Code Playgroud)

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

另外,这是我的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>
Run Code Online (Sandbox Code Playgroud)

urb*_*aut 8

我终于想到了碰巧遇到这个问题的其他人,这就是我如何创建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>
Run Code Online (Sandbox Code Playgroud)

然后,我不得不调整我的主要方法,不使用任何与TestNG相关的调用.例如,我不能在我的main方法中使用这样的东西:

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

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

  1. 从顶部菜单中选择文件>项目结构....
  2. 选择左侧菜单中的"Artifact"并单击"+"
  3. 选择Jar>来自具有依赖关系的模块...
  4. 使用浏览按钮选择主类
  5. 单击"提取到目标jar"旁边的单选按钮,然后单击"确定"
  6. 单击"+",然后选择"模块测试输出"
  7. 在右侧的"可用元素"窗格中,展开项目名称并选择所有Maven文件,然后将它们移动到左窗格中创建的jar目录中
  8. 点击"确定"
  9. 从顶部菜单中选择Build> Build Artifacts ....
  10. 将鼠标悬停在创建的jar上,然后单击Actions下的"Build"

笔记:

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

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

不是这个:

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

然后在您的jar保存到计算机上的同一文件夹中创建与其中的驱动程序相同的目录:

src
TestAutomation.jar
Run Code Online (Sandbox Code Playgroud)

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