使用jar / no runnable方法

时间:2017-03-26 15:04:54

标签: java eclipse jar testng main

我需要你的帮助! 在工作中,我需要自动化测试以测试整个应用程序。重点是,我是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):

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>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上执行测试时,我得到了这个结果:

Running tests!
[TestNG] Running:
  Command line suite


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

当我通过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

当我删除“driver.quit();”行时在捕获。测试成功通过,但这不正常,因为它在谷歌浏览器中没有发生任何事情。它应该是K.O.

也许有人可以帮助我?

2 个答案:

答案 0 :(得分:0)

您必须设置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();
    }
}

enter image description here

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项目,然后转到configure-&gt;转换为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>

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

答案 1 :(得分:0)

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>

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