Reputation: 499
I run my Selenium Testng tests using Maven, but I am trying to create executable jars, so other team members can simply run an automation suite from their desktop with minimal setup.
So I have added a main "runner" method to create an executable jar.
public class LaunchTests {
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = new ArrayList();
suites.add("testng.xml");//path to xml..
testng.setTestSuites(suites);
testng.run();
}
}
My best guess is that something needs to be added to the POM.xml?
When running the testng tests manually from eclipse, or with mvn clean test (Surefire plugin), they run flawlessly. But I get this error when trying to run as a Java program.
Exception in thread "main" org.testng.TestNGException:
Cannot find class in classpath: package.TestOne
at org.testng.xml.XmlClass.loadClass(XmlClass.java:81)
at org.testng.xml.XmlClass.init(XmlClass.java:73)
at org.testng.xml.XmlClass.<init>(XmlClass.java:59)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:548)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.testng.xml.XMLParser.parse(XMLParser.java:38)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:16)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:9)
at org.testng.xml.Parser.parse(Parser.java:172)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:302)
at org.testng.TestNG.run(TestNG.java:991)
at util.LaunchTests.main(LaunchTests.java:17)
Testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" parallel="methods" thread-count="1">
<test name="Chrome Adviser Portal" allow-return-values="true"
thread-count="1">
<parameters>
<parameter name="browser" value="chrome" />
<parameter name="loginType" value="adviser" />
</parameters>
<classes>
<class name="adviserPortal.AdviserLogin" />
</classes>
</test>
</suite>
Upvotes: 3
Views: 46405
Reputation: 955
I just resolved this for one of our engineers. He had updated to the latest testng plugin, but we resolved the issue by also updating his build path to a testng 6.11 jar file (he had 6.9.6 before).
Upvotes: 0
Reputation: 14746
You mentioned that you are able to run the tests from within eclipse via maven surefire plugin and you are not able to run it only when executing your main method.
This can only happen when you class LaunchTests
resides in src/main/java
and your suite xml file testng.xml
is referring to classes within src/test/java
. Java classes within src/main/java
donot have visibility into java classes that reside within src/test/java
. That explains why TestNG throws the exception Cannot find class in classpath
And if your LaunchTests
moves into src/test/java
then by default it wont be included in the uber jar (executable jar) that you are building because test classes are omitted.
So if you really want to be able to do this, you have two options
src/test/java
into src/main/java
and then work with it (Surefire plugin configuration in your pom file may need some tweaks to let it know that your test sources is not src/test/java
but src/main/java
.maven jar plugin
to create a jar of test classes by referring to here.Upvotes: 7
Reputation: 1323
Yes, you have to add extra plugins in pom.xml to create a runnable jar .
<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>StackOverFlow</groupId>
<artifactId>StackOverFlow</artifactId>
<version>1.0.0-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.stack.JarCreation.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.stack.JarCreation.Main</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.13.6</version>
</dependency>
</dependencies>
</project>
Go to your Project =>Right Click => Run As => Maven build... (which has 3 trailing dots) =>In Goals write clean install
It will create two jars in target folder.
The jar which has trailing jar-with-dependencies.jar is the runnable jar.
Place it where your chrome driver is and go to the location of jar and run it , it would work.
Upvotes: 2