這里有一些問題,請你指導我如何解決?
所以這里是我讀取Excel的代碼
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell。
import org.apache.poi.xssf.usermodel.XSSFRow。
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public String unicode;
public String[][] readExcel() throws IOException {
XSSFWorkbook excelObject = new XSSFWorkbook(" 。 /data/unicode1.xlsx")。)
XSSFSheet excelSheet = excelObject.getSheet("Sheet1")。
int rows = excelSheet.getLastRowNum()。
int columns = excelSheet.getRow(0).getLastCellNum();
String[][] data = new String [rows][columns] 。
for (int i = 1; i <= rows; i ) {
XSSFRow row = excelSheet.getRow(i)。
for (int j = 0; j < columns; j ) {
XSSFCell cell = row.getCell(j);
String cellValue = cell.getStringCellValue()。
data[i-1] [j] = cellValue。
}
}
return data。
}
}
我的測驗案例的代碼
import java.io.IOException;
import org.testng.annotations.DataProvider。
import org.testng.annotations.Test。
import com.github.javafaker.Faker。
import commonFeatures.CommonThings;
import commonFeatures.ReadExcel;
public class SignUpWithUnicode extends CommonThings{
Faker faker = new Faker()。
public void signUpWithUnicode(String unicode) throws InterruptedException {
invokeBrowser("chrome"/span>, "https://master.signup"/span>)。
type(locateEle("xpath","//input[@id='first_name-id']"),faker.name().firstName())。)
type(locateEle("xpath","//input[@id='last_name-id']"),faker.name() .lastName())
type(locateEle("xpath","//input[@id='last_name-id']"),faker.name() .lastName())。
type(locateEle("xpath","//input[@id='client_user_email-id']"),faker.Internet().emailAddress()
type(locateEle("xpath", "//input[@id='password-id']"), "12345") 。
type(locateEle("xpath", "//input[@id='degree-id']"), unicode)。)
click(locateEle("xpath", "//span[contains(text(),'SUBMIT')]")。
關閉瀏覽器()。
}
(name = "fetchData"/span>)
public String[][] getData() throws IOException {
ReadExcel excel = new ReadExcel()。
return excel.readExcel()。
}
這里是錯誤的地方
[RemoteTestNG] 檢測到TestNG版本7.0.0
FAILED: signUpWithUnicode
org.testng.internal.reflect.MethodMatcherException。
[public void testCases.SignUpWithUnicode.signUpWithUnicode(java.lang.String) throws java.lang. InterruptedException]沒有定義引數,但被發現使用了一個資料提供者 (明確指定或從類級注釋繼承)。
資料提供者不匹配
我在Excel表格中使用Unicode字符,所以我嘗試使用Object而不是String,但仍然出現了這個錯誤。然后我試著在我的Excel中使用字串,只是為了找到這個錯誤,但仍然沒有成功。
uj5u.com熱心網友回復:
TestNG資料提供者以一種不同的方式作業。如果你要回傳 示例:如果你要回傳 這里的輸出將是Object[][],那么測驗方法應該有一個引數數,這個引數數等于2d陣列中的列的數量。行的數量代表測驗案例的數量。
String[2][3],那么測驗方法將被呼叫2次,測驗方法中應該有3個引數。
@DataProvider (name = "fetchData")
public String[][] getData() {
return new String[] [] { { "a", "b"/span>, "c"/span> }, { "x"/span>, "y"/span>, "z"/span> }. };
}
@Test(dataProvider = "fetchData")/span>
public void testMethod(String one, String two, String three) {
System.out.println(one two three)。
}
abc
xyz
在你的案例中,資料的長度是無法預先確定的,因為資料是從excel中加載的。因此,在測驗方法中設定一組特定的引數是行不通的。所以,你可以做的是把excel讀取方法回傳的2D陣列放在另一個2D陣列中:
在你的案例中,由于資料是從excel中加載的,所以無法預測其長度。
@DataProvider (name = "fetchData")
public String[][] getData() throws IOException {
ReadExcel excel = new ReadExcel()。
//我們回傳1行和1列。
return new String[][] { { excel.readExcel() }; new String[] { { excel.readExcel() };
}
@Test(dataProvider = "fetchData")
public void signUpWithUnicode(String[][] data) throws InterruptedException {
//在這里你可以回圈資料并進行處理...
}
uj5u.com熱心網友回復:
錯誤是說資料提供者不匹配
在ReadExcel類中,你將資料作為多維陣列回傳,但在你的測驗signUpWithUnicode中,你將資料作為字串傳遞,因此要改變引數型別。
@Test(dataProvider = "fetchData")
public void signUpWithUnicode(String[][] unicode) throws InterruptedException {
//code
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/318676.html
標籤:
