【问题标题】:Running compiled Java Selenium tests (with dependencies) using TestNG xml via command line通过命令行使用 TestNG xml 运行已编译的 Java Selenium 测试(具有依赖项)
【发布时间】:2017-05-28 15:36:12
【问题描述】:

我正在尝试通过命令行使用 testng xml 文件运行 java selenium 测试。我将所有 jars (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已经添加到classpath中。
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();
    }
}

test.xml 来自 cmd 时的结果:

[TestNG] [ERROR]
Cannot instantiate class NBS.testcases.LoginWithBayanUser

问题:
1. 为什么会这样?这是 testNG 的限制吗? - 从 Eclipse 插件触发时,测试运行良好。
2. 我该如何解决这个问题?
3. 除了把所有的jar包放在一个文件夹里,并把它们添加到classpath中,有没有另一种方法可以更有效地从命令行运行testng?我试图避免这种情况的原因是我的依赖 jar 存储在我的本地 maven 存储库中,该存储库已经包含在项目中。将它们全部整理在一个位置似乎是多余的,只会增加混乱。

提前致谢!

================================================ =============
更新 1:

我尝试按照@Cathal 的建议使用 maven build + pom 文件来完成相同的操作,但现在我遇到了另一个错误。

我的项目一开始就已经是一个 maven 项目,所有依赖项都已添加。 (万无一失,testNg,硒......)。我只需要使用正确的 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 行会出现问题。任何信息,将不胜感激。谢谢。

【问题讨论】:

    标签: java eclipse selenium command-line testng


    【解决方案1】:

    您是否考虑过为您的项目使用像 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/

    【讨论】:

    • 通过 Maven 运行 testNG 绝对是我的选择之一。我打算考虑这样做,但我想知道为什么我的问题首先发生以及如何解决它。谢谢!
    • 按照建议,我现在尝试使用 maven。请参阅更新 1。希望您能提供帮助。谢谢!
    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多