简体   繁体   中英

Selenium TestNG: How can I run selenium tests from a compiled jar using TestNG?

We usually fire our Selenium tests using the TestNG xml runner. Sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="P1_jobSeeker" parallel="classes" preserve-order="true" thread-count="1">
  <test name="P1_jobSeeker-1">
    <classes>
      <class name="pamr.testCases.jobSeeker.PAMRAutoJS12Part1"/>
      <class name="pamr.testCases.jobSeeker.PAMRAutoJS12Part2"/>
      <class name="pamr.testCases.jobSeeker.PAMRAutoJS12Part3"/>
    </classes>
  </test>
</suite>

Now I'd like to do this without the source files, outside the IDE and just run everything from a complied jar file.

I found this SO post and was trying out this solution from the TestNG site.

I have created my main class and indicated the classes to run.

import java.util.ArrayList;
import java.util.List;

import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class Main {
    public static void main(String[] args) {
        XmlSuite suite = new XmlSuite();
        suite.setName("P1_jobSeeker");

        XmlTest test = new XmlTest(suite);
        test.setName("P1_jobSeeker-1");
        List<XmlClass> classes = new ArrayList<XmlClass>();
        classes.add(new XmlClass("pamr.testCases.jobSeeker.PAMRAutoJS12Part1"));
        classes.add(new XmlClass("pamr.testCases.jobSeeker.PAMRAutoJS12Part2"));
        classes.add(new XmlClass("pamr.testCases.jobSeeker.PAMRAutoJS12Part3"));
        test.setXmlClasses(classes);

        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        suites.add(suite);
        TestNG tng = new TestNG();
        tng.setXmlSuites(suites);
        tng.run();
    }
}

Before exporting the project to a jar, I tried running main but I'm getting a java.lang.ClassNotFoundException at this line: XmlSuite suite = new XmlSuite();

full console stack:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/xml/XmlSuite
    at Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException: org.testng.xml.XmlSuite
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [util.c:840]

It's probably worth noting that my project is a maven project so main and test folders are separated. 在此输入图像描述

Thanks in advance!

I had a similar requirement wherein I had to run the tests as testng jar and not through the maven test phase. Steps documented here .

HTH

Below checkpoints may resolve your issue:

  1. Make sure that you're not missing any JAR files related to TestNG and add them as Executable jars
  2. Exception in thread "main" java.lang.NoClassDefFoundError: means, that the class which you are trying to run was not found in the classpath. Check target/ classes once.
  3. Use @Test annotation for each class

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