简体   繁体   English

Selenium TestNG:如何使用TestNG从编译的jar运行selenium测试?

[英]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. 我们通常使用TestNG xml runner运行我们的Selenium测试。 Sample XML: 示例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. 现在我想在没有源文件的情况下在IDE之外执行此操作,只需从编译的jar文件中运行所有内容。

I found this SO post and was trying out this solution from the TestNG site. 我找到了这篇 SO帖子,并试用了TestNG网站上的这个解决方案。

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(); 在将项目导出到jar之前,我尝试运行main,但是我在这一行得到了一个java.lang.ClassNotFoundExceptionXmlSuite 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. 值得注意的是,我的项目是一个maven项目,所以主要和测试文件夹是分开的。 在此输入图像描述

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. 我有一个类似的要求,其中我必须运行测试作为testng jar而不是通过maven测试阶段。 Steps documented here . 这里记录的步骤。

HTH 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 确保您没有遗漏任何与TestNG相关的JAR文件,并将它们添加为可执行文件jar
  2. Exception in thread "main" java.lang.NoClassDefFoundError: means, that the class which you are trying to run was not found in the classpath. 线程“main”中的异常java.lang.NoClassDefFoundError:表示在类路径中找不到您尝试运行的类。 Check target/ classes once. 检查目标/类一次。
  3. Use @Test annotation for each class 为每个类使用@Test注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何远程运行TestNG Selenium测试? - How can I run TestNG Selenium tests remotely? 如何使用Testng和Selenium参数化我的搜索测试 - How can I parameterise my search tests using testng and selenium 使用Surefire在jar中执行TestNG / Selenium测试 - Using Surefire to execute TestNG/Selenium tests in a jar 如何以编程方式使用TestNG运行Selenium Java测试? - How to run Selenium Java tests with TestNG programmatically? 如何通过使用 TestNG 和 Selenium 的方法并行运行测试 - How to run tests in parallel by methods with TestNG and Selenium 如何使用TestNG框架和Jenkins运行Selenium测试 - How to run Selenium tests using TestNG framework and Jenkins 在使用TestNG进行硒测试期间,如何关闭打开的驱动程序 - How can I close opened drivers during Selenium tests with TestNG 如何使用 Gradle 为 Selenium TestNG 项目创建具有依赖项的可执行 JAR? - How can I create an executable JAR with dependencies using Gradle for a Selenium TestNG project? 如何使用TestNG并行运行Selenium? - How to run Selenium in parallel using TestNG? 通过 TestNG 使用 Selenium 执行自动化测试时,“未找到测试。没有运行任何测试” - "TestNG No tests found. Nothing was run" while executing automated tests using Selenium through TestNG
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM