首页 文章

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

提问于
浏览
9

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

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

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

每次我尝试通过导航到项目结构 - >工件 - > - > jar - >从具有依赖关系的模块创建一个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 回答

  • 7

    我终于想到了碰巧遇到这个问题的其他人,这就是我如何创建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相关的调用 . 例如,我不能在我的main方法中使用这样的东西:

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

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

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

    • 在左侧菜单中选择"Artifact"并单击'+'

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

    • 使用浏览按钮选择主类

    • 单击"extract to the target jar"旁边的单选按钮,然后单击"OK"

    • 单击'+'然后选择"Module Test Output"

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

    • 点击"OK"

    • 从顶部菜单中选择Build> Build Artifacts ....

    • 将鼠标悬停在创建的jar上,然后单击“操作”下的"Build"

    笔记:

    • 务必将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");
    

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

    src
    TestAutomation.jar
    

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

相关问题