我正在尝试通过命令行使用 testng xml 文件运行 java selenium 测试。我将所有 jar(testNg、selenium、jcommander...)移动到一个文件夹中,并通过使用类似 set classpath="singlefolder\*;"
从命令行添加到类路径中。
引用:
http://learn-automation.com/execute-selenium-test-from-command-line/
我设法用一个简单的测试做到了这一点:
测试类:
public class SampleTest {
@Test
public void testCase1() {
// Create webDriver reference
FirefoxDriver driver;
// Launch FirefoxDriver
driver = new FirefoxDriver();
// Close the driver
driver.quit();
}
@Test
public void testCase2() {
Assert.assertEquals("1", "1");
}
}
TestNG XML (test.xml) 文件:
<suite name="Suite 1" >
<test name="Test 1" >
<classes>
<class name="NBS.testcases.SampleTest" />
</classes>
</test>
</suite>
问题:
当我尝试运行我的实际测试时,这不起作用。
一些注意事项:
1. 我的测试有一些依赖性——从项目中编译的 jar 调用的通用方法。
2. 我的测试扩展了同一项目中的其他类
3. '扩展'类 [UITestTriggerProject] 与测试类不在同一个文件夹中。
4.“扩展”类可能因每次测试而异,并且每个扩展类可能会或可能不会扩展来自相同或其他文件夹的更多类。
5. 项目1中所述编译的jar已经添加到类路径中。
6. 当通过插件从 eclipse 中触发 test.xml 时,相同的测试类运行良好。
我的测试类:
package NBS.testcases;
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import Automation.framework.dataModel.CaseInfo;
import NBS.config.*;
@Listeners({ WebTestListener.class })
public class LoginWithBayanUser extends UITestTriggerProject{
String xlsDataFilePath = "/excel_data/moduleA/LoginWithBayanUser.xlsx";
@DataProvider(name = "dataInfo1")
public Object[][] dataInfo1(Method method) {
Object[][] myObj = setTestData(xlsDataFilePath, method.getName());
return myObj;
}
@Test(dataProvider="dataInfo1")
public void tcLoginWithUser1(CaseInfo tc) throws Exception {
logTestCaseID(tc);
loginHomePage().tsLoginWithBayanUser(tc);
loginHomePage().tsSelectLogicType(tc);
loginHomePage().tsLogoutWithBayanUser();
}
}
cmd 中的 test.xml 结果:
[TestNG] [ERROR]
Cannot instantiate class NBS.testcases.LoginWithBayanUser
问题:
1. 为什么会这样?这是 testNG 的限制吗? - 当从 eclipse 插件中触发时,测试运行得很好。
2.如何解决问题?
3. 除了将所有的jar 放在一个文件夹中并将它们添加到类路径中之外,还有另一种方法可以更有效地从命令行运行testng 吗?我试图避免这种情况的原因是我的依赖 jar 存储在我已经包含在项目中的本地 Maven 存储库中。将它们全部整理在一个位置似乎是多余的,只会增加困惑。
提前致谢!
============================================= =============
更新 1:
我尝试按照@Cathal 的建议使用 maven 构建 + pom 文件来完成相同的操作,但现在我遇到了不同的错误。
我的项目本来就是一个maven项目,所有的依赖都已经添加好了。 (surefire,testNg, Selenium ......)。我只需要用正确的 xml 名称修改 surefire 配置
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testngRunner/test.xml</suiteXmlFile>
</suiteXmlFiles>
执行步骤:
1. 打开命令提示符
2. 输入以下内容:
set projectLocation=C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS
cd %projectLocation%
set classpath=%projectLocation%\target\test-classes\;%projectLocation%\lib\*;
mvn surefire:test -DtestSuite=test.xml
这适用于 SampleTest.java
但不适用于 LoginWithBayanUser.java
错误:
[TestNGClassFinder] Warning: Can't link and determine methods of class NBS.testcases.LoginWithBayanUser
当我尝试从 eclipse 中运行 pom.xml 作为 Maven 测试时,也会发生此错误。希望对您有所帮助!
============================================= =============
更新 2:
通过在 maven pom.xml 依赖项下添加 LoginWithBayanUser.java
调用的自定义 jar,设法解决了 maven 问题。
我现在可以使用上面列出的 maven 命令在 cmd 提示符下运行我的测试。但是,仍然想知道为什么 testNG + cmd 行会出现问题。任何信息,将不胜感激。谢谢。
最佳答案
您是否考虑过为您的项目使用像 maven 这样的构建工具?它旨在完全满足您的要求。您的所有项目配置都包含在一个文件中。您的测试更具可移植性,并且可以在任何地方轻松执行测试脚本。
这是一个基本的示例 pom.xml 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupName</groupId>
<artifactId>artificatId</artifactId>
<version>0.0.2</version>
<name>Selenium Tests</name>
<properties>
<!-- compiler settings -->
<maven.compiler.sources>1.8</maven.compiler.sources>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- build info -->
<build.timestamp>${maven.build.timestamp}</build.timestamp>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<testng.congig>${testSuite}</testng.congig>
<aspectj.version>1.8.9</aspectj.version>
<allure.version>1.4.14</allure.version>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-testng-adaptor</artifactId>
<version>${allure.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${maven.compiler.sources}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
<suiteXmlFiles>
<suiteXmlFile>target\test-classes\${testng.congig}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/test/resources</directory>
<includes>
<include>*.xml</include>
</includes>
</resource>
</resources>
</build>
<reporting>
<excludeDefaults>true</excludeDefaults>
<plugins>
<plugin>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</reporting>
</project>
从命令行执行脚本非常简单:
mvn -fn clean install -DtestSuite=YOUR_TEST_SUITE.xml
Maven 是高度可配置的,允许您根据需要设置项目。
这里有更详细的解释: http://toolsqa.com/java/maven/configure-selenium-continuous-integration-maven/
关于java - 通过命令行使用 TestNG xml 运行已编译的 Java Selenium 测试(带有依赖项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41631640/