本文介绍了需要使用(TestNG.xml + Maven)创建Bat文件的正确步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建正确的 Bat文件

我正在使用 Maven + Selenium(TestNG)

我尝试了多种解决异常的方法,但是它是最常见的异常之一:无法找到或加载主类/java.lang.ClassNotFoundException

I have tried many different ways to Resolve exception, But its one of the most common exception: Could not find or load main class / java.lang.ClassNotFoundException

在我的Maven项目中,没有bin或lib文件夹,我想知道这一点.是否从.m2(Maven)安装中调用库.

In my Maven project there is no bin or lib folder, And I am wondering for it. Whether to call library from .m2(Maven) installation.

要调用类文件,请在我的maven项目.class文件中找到类似于E:\testBatchDemo\target\classes\mytest

To call class file, In my maven project .class file located something like : E:\testBatchDemo\target\classes\mytest

这里是我到目前为止尝试过的示例,

Here is sample of What I have tried so far,

set projectLocation=E:\testBatchDemo
cd %projectLocation%   
set classpath=E:\testBatchDemo\target\classes\mytest; 
java -cp %classpath% C:\Users\Desktop-pc\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar; C:\Users\Desktop-pc\.m2\repository\org\seleniumhq\selenium\selenium-java\3.12.0\selenium-java-3.12.0.jar; org.testng.TestNG %projectLocation%\testng.xml
pause

任何人都可以提供帮助,需要调用哪个库,或者如何获得正确的.class路径.

Anyone can please assist, Which library need to call or How can I achieve correct .class path.

推荐答案

根据您的问题描述,我认为我们需要解决某些灰色区域,以免进入解决方案.

As per your question description I think there are certain grey areas which we need to address beofre jumping into the solution.

  • 我想创建正确的batch(即.bat)文件:从根本上说, Maven 是一个构建工具,可以广泛地与pom.xml结合使用,并且不需要任何 Windows批处理文件.
  • 如果您的用例是要将 Windows批处理文件传递给 Jenkins 进行持续集成,那么如果 Windows批处理文件能够启动执行,则需要验证此步骤您的 Test Suite
  • .
  • 不过, Jenkins 也可以通过pom.xml Maven 启动您的 Test Suite 的执行.
  • I am wondering about to create correct batch (i.e. .bat) file: Fundamentally, Maven is a build tool which extensively works in-conjunction with pom.xml and doesn't requires any Windows batch file.
  • Moving forward if your usecase is to pass the Windows batch file to Jenkins for Continous Integration then you need to verify this step if the Windows batch file is able to kick-start the execution of your Test Suite.
  • However Jenkins can also kick-start the execution of your Test Suite through the pom.xml and Maven.

现在,您的用例必须与上述要求之一匹配,如下所述:

Now, your usecase must match to one of the above mentioned requirement as mentioned below:

  • Windows批处理文件 + TestNG
  • Jenkins + TestNG
  • 詹金斯 + Maven
  • Windows Batch file + TestNG
  • Jenkins + TestNG
  • Jenkins + Maven

您已经广泛讨论了 Windows批处理文件,因此,这里是第一个选项 Windows批处理文件 + TestNG 的相关详细信息. :

As you have extensively spoken about the Windows batch file so here are the relevant details as per the first option Windows Batch file + TestNG:

  • TestNG @Test注释编写一个简单的代码块,如下所示:

  • Write a simple code block with @Test annotation of TestNG as follows:

package demoJenkins;

import org.testng.annotations.Test;

public class DemoJenkinsJob 
{

    @Test
    public void testJenkins()
    {

        System.out.println("Welcome to Jenkins World");
    }
}

  • 运行方式 TestNG Test

    testng.xml如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite">
      <test name="Test">
        <classes>
          <class name="demoJenkins.DemoJenkinsJob"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->
    

  • 从您的 IDE (即 Eclipse )获取绝对的项目位置,浏览至子目录,创建目录 lib

  • Get the absolute Project Location from your IDE (i.e. Eclipse), browse to the sub-directory, create a directory lib

    将所有相关的jar和库( Selenium TestNG jars)复制到 lib 目录中.

    Copy all the relevant jars and libraries (Selenium and TestNG jars) in the lib directory.

    • selenium-server-standalone-3.13.0.jar
    • org.testng_6.14.2.r201802161450.jar
    • com.beust.jcommander_1.72.0.jar
    • org.apache-extras.beanshell.bsh_2.0.0.b6.jar
    • org.yaml.snakeyaml_1.17.0.jar
    • selenium-server-standalone-3.13.0.jar
    • org.testng_6.14.2.r201802161450.jar
    • com.beust.jcommander_1.72.0.jar
    • org.apache-extras.beanshell.bsh_2.0.0.b6.jar
    • org.yaml.snakeyaml_1.17.0.jar

    通过 CLI 浏览到项目目录并提供以下 classpath

    Through CLI browse to the Project Directory and provide the following classpath:

    >set classpath=<Project Directory>\bin;<Project Directory>\lib\*;
    

  • 通过 CLI 如下执行testng.xml:

  • Through CLI execute testng.xml as follows:

    Project_Directory>java org.testng.TestNG testng.xml
    

  • 成功执行后,在 Project Directory 中创建一个新的文本文档并添加以下代码:

  • On successful execution, within the Project Directory create a new text document and add the following code:

    java -cp bin;lib/* org.testng.TestNG testng.xml
    

  • 将文件另存为 run.bat

    根据您的问题和后续评论,

    As per your question and subsequent comment,

    • 当您使用 TestNG 和/或 Maven 时,这些框架/构建工具会将所有相关的 jars 保留在各自的位置.但是,当您打算手动配置 buildpath 时,必须手动创建 lib 目录,并保留相关的 jars 的副本.

    • When you use TestNG and/or Maven, these framework/buildtool keeps all the relevant jars in their respective location. But when you intend the configure the buildpath manually you have to create the lib directory manually and keep a copy of the relevant jars.

    也许您不需要手动创建 bin 目录,就像成功编译 Java程序一样,您的程序会自动创建 bin 并在执行之前保留 class 文件.

    Perhaps you don't need to create the bin directory manually as on successful compilation of your Java program your program will automatically create the bin and keeps the class file before execution.

    这篇关于需要使用(TestNG.xml + Maven)创建Bat文件的正确步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-24 18:11