Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jar/no可运行方法运行testNG测试套件_Java_Eclipse_Jar_Testng_Main - Fatal编程技术网

Java 使用jar/no可运行方法运行testNG测试套件

Java 使用jar/no可运行方法运行testNG测试套件,java,eclipse,jar,testng,main,Java,Eclipse,Jar,Testng,Main,我需要你的帮助! 在工作中,我需要自动化测试来测试整个应用程序。关键是,我是Java和自动化的初学者 我正在尝试不同的教程,效果很好。但是现在,我想要进步。我需要以.jar的形式交付测试。 它不起作用,因为我需要一门主课。我试图添加它,但它不起作用 我更新了我的脚本以更正和简化它们,但我仍然有一些问题。现在我有: 启动测试的主类: package nouguierc.selenium; import org.testng.TestListenerAdapter; import org.testn

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

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

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

启动测试的主类:

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();
}
}
我收到一条警告消息:“testNG类型的addListener方法已弃用”

我还有一个要执行的测试(并将其作为可运行的jar):

我的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>StackOverFlow</groupId>
<artifactId>StackOverFlow</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导出jar并使用cmd运行它时,我有:

no manifest attribute in ExecutableJAR.jar
测试是打开谷歌浏览器的地址“数据”,什么也不做。然后我有以下失败的结果:

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();”时。测试成功通过,但这并不正常,因为在谷歌chrome中什么都没有发生。应该可以


也许有人可以帮助我?

您必须设置chromedriver的属性,并将chromedriver.exe置于项目级别

要将其作为jar运行,还必须创建一个主类:

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();
    }
}

如果它不是maven项目,请右键单击您的项目并转到configure->convert to maven project,将其转换为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>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>

4.0.0
堆栈溢出
堆栈溢出
1.0.0-SNAPSHOT
src
maven编译器插件
3.5.1
1.8
1.8
maven汇编插件
com.stack.JarCreation.Main
带有依赖项的jar
org.apache.maven.plugins
maven汇编插件
附属的
包裹
带有依赖项的jar
com.stack.JarCreation.Main
org.seleniumhq.selenium
硒爪哇
3.3.1
org.testng
testng
6.9.13.6

创建用于启动配置的Jar作为主StackOverflow,并将Jar和chromedriver.exe放在同一位置。

RegistrationTest类:

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>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>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>

4.0.0
堆栈溢出
堆栈溢出
1.0.0-SNAPSHOT
src
maven编译器插件
3.5.1
1.8
1.8
maven汇编插件
nouguierc.selenium.MainJar
带有依赖项的jar
org.apache.maven.plugins
maven汇编插件
附属的
包裹
带有依赖项的jar
nouguierc.selenium.MainJar
org.seleniumhq.selenium
硒爪哇
3.3.1
org.testng
testng
6.9.13.6

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

谢谢您的回答,我将测试它。但什么是壁虎河?我想它应该默认启动firefox。我测试了它。我有一个错误:“Jar导出已完成,但存在问题。请参阅详细信息,无法从给定的启动配置中找到主方法”。MANIFEST.MF不包含主类(TestRunner)。我不明白为什么。在jar中,没有我的main方法,我的TestSuite和RegistrationTest也没有。如果您使用的是Selenium 3,那么要使用Firefox浏览器,您需要使用单独的驱动程序,该驱动程序将与Firefox浏览器交互。如果你注意到了,那么我们在Chrome和IE浏览器上也做了同样的事情。你想创建一个jar并运行它,还是在eclipse上运行它?你能告诉我实际的问题是什么吗?你能用上面的代码附上失败报告吗?因为它对我来说工作得很好。当我在Eclipse上运行TestRunner时,它工作得很好。但是当我在cmd中导出runnable jar和whrite“java-jar”ExecutableJar.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>
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();
        }
    }
}
<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>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>