运行 带有 jar 的 testNG 测试套件/没有可运行的方法

Run a testNG test suite with a jar / no runnable method

我需要你的帮助! 在工作中,我需要自动化测试来测试整个应用程序。重点是,我是 Java 和自动化方面的初学者。

我正在尝试不同的教程,效果很好。但是现在,我想要进步。我将需要以 .jar 格式交付我的测试。 而且它不起作用,因为我需要一个主要 class。我正在尝试添加它,但它不起作用。

更新 我的脚本来更正和简化它们,但我仍然遇到一些问题。现在我有:

A main class 启动测试:

package nouguierc.selenium;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;

public class TestRunner {

public static void main(String[] args) {
    System.out.println("Running tests!");
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] { RegistrationTest.class });
    testng.addListener(tla);
    testng.run();
}
}

我收到警告消息:"The method addListener from the type testNG is deprecated"

我也有我想执行的测试(并且也作为 运行nable jar):

package nouguierc.selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import org.testng.annotations.Test;


public class RegistrationTest{

public  ExtentReports extent;
public  ExtentTest test;
public  int a = 1;
WebDriver driver=null;

@Test //This is TestNG annotation

public void testRegister(){

        extent = ExtentManager.Instance();


                    try{

        System.setProperty("webdriver.chrome.driver", "C:\Users\nouguierc\Desktop\Projets\Testing\Airbus\Automatisation\testCélia\WD_Automation\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://newtours.demoaut.com/");

        test = extent.startTest("VerificationReporting", "AlorsAlors");

        driver.findElement(By.linkText("REGISTER")).click();
        driver.findElement(By.name("firstName")).sendKeys("User1");
        driver.findElement(By.name("lastName")).sendKeys("Surname1");
        driver.findElement(By.name("phone")).sendKeys("123456789");
        driver.findElement(By.name("userName")).sendKeys("user1@test.com");
        driver.findElement(By.name("address1")).sendKeys("Test Address");
        driver.findElement(By.name("city")).sendKeys("Test City");
        Select select = new Select(driver.findElement(By.name("country")));
        select.selectByVisibleText("ANGOLA");
        driver.findElement(By.name("email")).sendKeys("user1@test.com");
        driver.findElement(By.name("password")).sendKeys("user1");
        driver.findElement(By.name("confirmPassword")).sendKeys("user1");
        driver.findElement(By.name("register")).click();

        if(a == 1)
            test.log(LogStatus.PASS, "OK" );
        else
            test.log(LogStatus.FAIL, "NOT OK" );

        driver.quit();

        }

        catch (Exception e) {
            System.out.println(e.getMessage());
            driver.quit();
        }

    extent.endTest(test);
    extent.flush();
    extent.close();
}   

}

我的 POM.xml 是:

<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>Whosebug</groupId>
<artifactId>Whosebug</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
    <maven.compiler.version>1.7</maven.compiler.version>
    <maven.compiler.target>1.7</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <sourceDirectory>src/test/java</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>nouguierc.selenium.TestRunner</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>nouguierc.selenium.TestRunner</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>com.relevantcodes</groupId>
        <artifactId>extentreports</artifactId>
        <version>2.41.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.13.6</version>
    </dependency>
</dependencies>
</project>

当我在 Eclipse 上执行测试时,我得到了这个结果:

Running tests!
[TestNG] Running:
  Command line suite


===============================================
Command line suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================

当我使用 Eclipse 导出 jar 时,运行 使用 cmd 导出 jar 时:

no manifest attribute in ExecutableJAR.jar

测试正在打开 google chrome 地址 "data;" 并且什么都不做。然后我有以下失败的结果:

testRegister java.lang.NullPointerException
at nouguierc.selenium.RegistrationTest.testRegister(RegistrationTest.java:61)
at nouguierc.selenium.TestRunner.main(TestRunner.java:13)
... Removed 22 stack frames

当我删除 catch 中的行 "driver.quit();" 时。测试成功通过,但这不正常,因为它在 google chrome 中没有发生任何事情。应该是K.O。

也许有人可以帮助我?

您必须为 chromedriver 设置 属性 并在您的项目级别设置 chromedriver.exe。

为了 运行 将它作为一个 jar,你还必须创建一个主 class :

package com.stack.JarCreation;

import org.testng.TestListenerAdapter;
import org.testng.TestNG;

public class Main {

    public static void main(String[] args) {
        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[] { RunnableJar.class });
        testng.addListener(tla);
        testng.run();
    }
}

package com.stack.JarCreation;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class RunnableJar {
    public int a = 1;

    @Test
    public void testRegister() {
        WebDriver driver=null;
        try {
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            driver.get("http://newtours.demoaut.com/");
            driver.findElement(By.linkText("REGISTER")).click();
            driver.findElement(By.name("firstName")).sendKeys("User1");
            driver.findElement(By.name("lastName")).sendKeys("Surname1");
            driver.findElement(By.name("phone")).sendKeys("123456789");
            driver.findElement(By.name("userName")).sendKeys("user1@test.com");
            driver.findElement(By.name("address1")).sendKeys("Test Address");
            driver.findElement(By.name("city")).sendKeys("Test City");
            Select select = new Select(driver.findElement(By.name("country")));
            select.selectByVisibleText("ANGOLA");
            driver.findElement(By.name("email")).sendKeys("user1@test.com");
            driver.findElement(By.name("password")).sendKeys("user1");
            driver.findElement(By.name("confirmPassword")).sendKeys("user1");
            driver.findElement(By.name("register")).click();

            if (a == 1)
                System.out.println("OK");
            else
                System.out.println("NOT OK");
            driver.quit();
        }

        catch (Exception e) {
            System.out.println(e.getMessage());
            driver.quit();
        }
    }
}

如果它不是 Maven 项目,请右键单击您的项目并将其转换为 Maven 项目,然后转到配置->转换为 Maven 项目。

并将您的 pom.xml 替换为以下文件。

    <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>Whosebug</groupId>
    <artifactId>Whosebug</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>

将用于启动配置的 Jar 创建为 Main-Whosebug,并将 jar 和 chromedriver.exe 放在同一位置。

注册测试class:

package nouguierc.selenium;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class RegistrationTest {
    public int a = 1;

    @Test
    public void testRegister() {
        WebDriver driver = null;
        try {
            System.setProperty("webdriver.chrome.driver", "C:\Users\nouguierc\Desktop\Projets\Testing\Airbus\Automatisation\testCélia\WD_Automation\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            driver.get("http://newtours.demoaut.com/");
            driver.findElement(By.linkText("REGISTER")).click();
            driver.findElement(By.name("firstName")).sendKeys("User1");
            driver.findElement(By.name("lastName")).sendKeys("Surname1");
            driver.findElement(By.name("phone")).sendKeys("123456789");
            driver.findElement(By.name("userName")).sendKeys("user1@test.com");
            driver.findElement(By.name("address1")).sendKeys("Test Address");
            driver.findElement(By.name("city")).sendKeys("Test City");
            Select select = new Select(driver.findElement(By.name("country")));
            select.selectByVisibleText("ANGOLA");
            driver.findElement(By.name("email")).sendKeys("user1@test.com");
            driver.findElement(By.name("password")).sendKeys("user1");
            driver.findElement(By.name("confirmPassword")).sendKeys("user1");
            driver.findElement(By.name("register")).click();

            if (a == 1)
                System.out.println("OK");
            else
                System.out.println("NOT OK");
            driver.quit();
        }

        catch (Exception e) {
            System.out.println(e.getMessage());
            driver.quit();
        }
    }
}

pom.xml 文件:

<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>Whosebug</groupId>
    <artifactId>Whosebug</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>nouguierc.selenium.MainJar</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>nouguierc.selenium.MainJar</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>

请用我刚刚提供的文件替换你的这两个文件,并将主 class 命名为 MainJar