问题描述
我面临一个特殊问题,当我在 Eclipse 中将项目作为 maven 测试运行时,它运行良好。但是当我尝试在命令行中执行相同的操作时,构建失败并出现如下错误 -
//src/main/java/com/qa/util/TestUtil.java:[10,18] 包org.testng 不存在 [错误] //src/main/java/com/qa/util/TestUtil.java:[11,30] 包 org.testng.annotations 不存在 [错误] //src/main/java/com/qa/util/TestUtil.java:[64,35] 找不到符号 [ERROR] 符号:类 ITestContext [ERROR] 位置:类 com.eBusiness.qa.util.TestUtil
出于好奇,我还尝试将整个 /java 文件夹移动到 test 文件夹下,但问题仍然存在。我的配置有什么问题?
我的 TestUtil.java 文件:
package com.qa.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.testng.annotations.DataProvider;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import com.eBusiness.qa.base.TestBase;
public class TestUtil extends TestBase{
public static long PAGE_LOAD_TIMEOUT = 60;
public static long IMPLICIT_WAIT = 20;
static Workbook book;
static Sheet sheet;
@DataProvider(name="test")
public static Object[][] getTestData2() throws IOException {
FileInputStream file = new FileInputStream("C:\\Users\\Teju\\Downloads\\payrollDps.xlsx");
sheet = book.getSheet("allowance");
Object[][] data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];
int noOfColumns = sheet.getRow(0).getLastCellNum();
//System.out.println(noOfColumns);
String[] Headers = new String[noOfColumns];
for (int j=0;j<noOfColumns;j++){
Headers[j] = sheet.getRow(0).getCell(j).getStringCellValue();
}
for (int a=0;a<noOfColumns;a++){
if(Headers[a].equals("Allowance Name")){
for (int i = 1; i < sheet.getLastRowNum(); i++) {
data[i][a] = sheet.getRow(i).getCell(a).toString();
}
}
}
try {book.close();} catch (IOException e) { e.printstacktrace();}
return data;
}
@DataProvider(name="Data")
public Object[][] getDataFromDataprovider(){
return new Object[][]
{
{ "ATitle","Adesc"},};
}
public static void refreshPage(){
driver.navigate().refresh();
}
public static void takeScreenshotAtEndOftest() throws IOException {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String currentDir = System.getProperty("user.dir");
FileUtils.copyFile(scrFile,new File(currentDir + "/screenshots/" + System.currentTimeMillis() + ".png"));
}
}
如果需要,还有我的 pom:
4.0.0
<groupId>HRMS</groupId>
<artifactId>eBusinessAutomation</artifactId>
<version>1.0-SNAPSHOT</version>
<name>project</name>
<!-- FIXME change it to the project's website -->
<url>https://www.github.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>jcenter</id>
<name>bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>openxml4j</artifactId>
<version>1.0-beta</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.paulhammant</groupId>
<artifactId>ngwebdriver</artifactId>
<version>1.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven
defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle,see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle,jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle,see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- This is added to create java project -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<!-- Attach the shade into the package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.mkyong.hashing.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<inherited>true</inherited>
<configuration>
<useSystemClassLoader>true</useSystemClassLoader>
<suiteXmlFiles>code_test_run</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
解决方法
改变依赖范围
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>runtime</scope>
</dependency>
或者您可以尝试删除范围
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
</dependency>
您是否将测试类放在默认测试包中,然后需要将其更改为src(如果是自动化项目)