如何使用Selenium / TestNG java文件中的IntelliJ制作可执行jar文件?
我已经谷歌搜索了几天试图弄清楚如何做到这一点,如果有人这样做,我会非常感谢帮助。
我有一个自动化测试项目,我在IntelliJ中创建,自动化用户与Web应用程序交互。
我想将自动化测试(使用Selenium和TestNG在Java中创建)放入可执行jar文件中,其他人可以通过双击jar文件来运行。
每次我尝试通过导航到项目结构 – >工件 – > + – >jar子 – >从具有依赖关系的模块创建一个jar文件时,它最终会创建一个声称它的jar,
"Could not find or load the main class "
当我尝试使用以下命令运行它时:
java -jar MyProject.jar
知道为什么我不断得到这个错误,或者有办法成功地做到这一点?
另外,这是我的pom.xml:
TestAutomation TestAutomation 1.0-SNAPSHOT org.apache.maven.plugins maven-assembly-plugin true com.test.automation.Executable jar-with-dependencies org.seleniumhq.selenium selenium-server 2.39.0 org.seleniumhq.selenium selenium-java 2.40.0 junit junit 4.11 test org.testng testng 6.1.1 test
我终于想到了碰巧遇到这个问题的其他人,这就是我如何创建jar文件并成功运行…
我不得不将我的pom.xml文件更改为以下内容:
TestAutomation TestAutomation jar 1.0-SNAPSHOT http://maven.apache.org org.apache.maven.plugins maven-compiler-plugin org.apache.maven.plugins maven-jar-plugin true com.test.automation.Executable org.apache.maven.plugins maven-surefire-plugin org.seleniumhq.selenium selenium-java 2.40.0 junit junit 4.11 test org.testng testng 6.1.1 test
然后,我不得不调整我的主要方法,不使用任何与TestNG相关的调用。 例如,我不能在我的main方法中使用这样的东西:
TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); testng.setTestClasses(new Class[] {WordProfFonts2Set0.class}); testng.addListener(tla); testng.run();
最后,以下是创建相应jar文件的步骤:
- 从顶部菜单中选择文件>项目结构….
- 选择左侧菜单中的“Artifact”并单击“+”
- 选择Jar> From modules with dependencies …
- 使用浏览按钮选择主类
- 单击“提取到目标jar”旁边的单选按钮,然后单击“确定”
- 单击“+”,然后选择“模块测试输出”
- 在右侧的“可用元素”窗格中,展开项目名称并选择所有Maven文件,然后将它们移动到左窗格中创建的jar目录中
- 点击“确定”
- 从顶部菜单中选择Build> Build Artifacts ….
- 将鼠标hover在创建的jar上,然后单击Actions下的“Build”
笔记:
-
务必将IE或Chrome驱动程序添加到项目资源文件夹中,并通过代码文件夹而不是计算机的硬盘驱动器调用它。 例如,执行以下操作:
文件文件=新文件(“src \ test \ resources \ _binaries \ IEDriverServer.exe”);
不是这个:
File file = new File ("C:\\Users\\\\\\src\\test\\java\\src\\ test\\resources\\binaries\\IEDriverServer.exe");
然后在您的jar保存到计算机上的同一文件夹中创建与其中的驱动程序相同的目录:
src TestAutomation.jar
2。 请确保,如果使用IE,则为所有区域设置保护模式或不设置任何区域(在IE中,转到Internet选项…>安全性(选项卡)>启用保护模式复选框)