我终于找到了碰巧遇到这个问题的其他人的方法,这就是我如何创建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文件的步骤:
- 从顶部菜单中选择文件>项目结构…
- 在左侧菜单上选择“工件”,然后单击“ +”
- 选择Jar>来自具有依赖性的模块…
- 使用浏览按钮选择您的主要班级
- 单击“提取到目标jar”旁边的单选按钮,然后单击“确定”。
- 单击“ +”,然后选择“模块测试输出”
- 在右侧的“可用元素”窗格中,展开项目名称并选择所有Maven文件,然后将它们移动到在左窗格中创建的jar目录中。
- 点击“确定”
- 从顶部菜单中选择Build> Build Artifacts …
- 将鼠标悬停在创建的jar上,然后在“操作”下单击“构建”
笔记:
- 确保将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保存到计算机的同一文件夹中,创建具有驱动程序的相同目录:
srcTestAutomation.jar
2。如果使用IE,请确保为所有区域或全部区域都未设置保护模式(在IE中,转到“ Internet选项…”>“安全性”(选项卡)>“启用保护模式”复选框)



