简体   繁体   中英

Running compiled Java Selenium tests (with dependencies) using TestNG xml via command line

I'm trying to run java selenium tests using a testng xml file via command line. I moved all my jars (testNg,selenium,jcommander...) in a single folder and add in the classpath by using something like set classpath="singlefolder\\*;" from the command line.

Reference:
http://learn-automation.com/execute-selenium-test-from-command-line/

I managed to do this using a simple test:

Test Class:

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) File:

<suite name="Suite 1" >
    <test name="Test 1" >
        <classes>
            <class name="NBS.testcases.SampleTest" />
        </classes>
    </test>
</suite>


This is not working when I try to run my actual tests.


1. My tests are having some dependencies -- common methods being called from a compiled jar within the project.
2. My test extends other classes from within the same project
3. The 'extended' class [UITestTriggerProject] is not on the same folder with the test classes.
4. The 'extended' classes may vary from each test, and each extended class may or may not be extending further classes that are from the same or other folders.
5. The said compiled jar on item 1 is already added in the classpath.
6. The same test class is running fine when the test.xml is triggered from within eclipse via the plugin.

My test class:

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();
    }
}

Result when test.xml from cmd:

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


1. Why is this happening? Is this a testNG limitation? - the test runs just fine when fired from the eclipse plugin.
2. How do I solve the problem?
3. Apart from putting all the jars in one folder and adding them in the classpath, is there another way to run testng from command line more efficiently? The reason I'm trying to avoid this is my dependent jars are stored on my local maven repo that is already included in the project. Collating them all in one location seems to be redundant and just adds clutter.

Thanks in advance!

============================================================

I tried to accomplish the same by using the maven build + pom file as advised by @Cathal and now I'm getting a different error.

My project was already a maven project to begin with, all dependencies were already added. (surefire, testNg, selenium...). I just had to modify the surefire config with the correct xml name

            <suiteXmlFiles>
                <suiteXmlFile>src/test/resources/testngRunner/test.xml</suiteXmlFile>
            </suiteXmlFiles>


1. Open command prompt
2. Enter the following:

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

This works with SampleTest.java but not with LoginWithBayanUser.java

[TestNGClassFinder] Warning: Can't link and determine methods of class NBS.testcases.LoginWithBayanUser

This error also happens when I try to run the pom.xml as Maven test from within eclipse. Hoping you can help!

============================================================
Managed to resolve the maven issue by adding the custom jar being called by LoginWithBayanUser.java under maven pom.xml dependencies. 通过在maven pom.xml依赖LoginWithBayanUser.java下添加由LoginWithBayanUser.java调用的自定义jar来管理解决maven问题。

I can now run my tests in cmd prompt using the maven command listed above. However, would still want to know why the problem happens with testNG + cmd line. Any information would be appreciated. Thank you.

Have you considered using a build tool like maven for your project? It designed to do exactly what your asking. All of your project configuration is contained within one file. Your tests are more portable and it allows for easy execution of your test scripts anywhere.

Here is a basic example pom.xml configuration file:

<?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>

Executing your script from the command line is as easy as:

mvn -fn clean install -DtestSuite=YOUR_TEST_SUITE.xml

Maven is highly configurable and allows you to setup your projects as you like.

Heres a more detailed explanation: http://toolsqa.com/java/maven/configure-selenium-continuous-integration-maven/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM