![](/img/trans.png)
[英]Run TestNG XML suite from runnable jar with test classes in specific folder
[英]Run a testNG test suite with a jar / no runnable method
我需要你的幫助 ! 在工作中,我需要自動執行測試以測試整個應用程序。 重點是,我是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
該測試正在將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
當我刪除“ driver.quit();”行時 在抓住。 測試成功通過,但是不正常,因為它在谷歌瀏覽器中什么都沒有發生。 應該是KO
也許有人可以幫助我?
您必須為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();
}
}
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-> convert to 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放在同一位置。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.