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>
Problem:
This is not working when I try to run my actual tests.
Some notes:
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
Questions:
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!
============================================================
UPDATE 1:
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>
Execution steps:
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
Error:
[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!
============================================================
UPDATE 2:
Managed to resolve the maven issue by adding the custom jar being called by LoginWithBayanUser.java under maven pom.xml dependencies.
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.