我正在尝试学习DataProvider测试注释,其中我正在从excel文件中读取测试数据,然后将其作为测试用例的输入发送。但是我在执行相同操作时遇到错误:
[RemoteTestNG]检测到TestNG版本6.14.2 失败:loginTest org.testng.internal.reflect.MethodMatcherException: 数据提供商不匹配
以下是代码:
public class ExcelUtilites {
public static ArrayList<Object> getDataFromExcel() throws EncryptedDocumentException, InvalidFormatException, IOException
{
ArrayList<Object> al=new ArrayList<Object>();
FileInputStream fis=new FileInputStream("E:\\LoginData.xlsx");
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet("Logins");
Row rows=sh.getRow(0);
Cell cells=rows.getCell(0);
for(int rowCount=0;rowCount<sh.getLastRowNum();rowCount++)
{
String username=sh.getRow(rowCount).getCell(0).getStringCellValue();
String password=sh.getRow(rowCount).getCell(1).getStringCellValue();
Object[] ob={username,password};
al.add(ob);
}
return al;
}
}
public class FacebookTest {
WebDriver driver;
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.gecko.driver",
"E:\\All_JARs\\geckodriver.exe");
driver.get("https://www.facebook.com");
driver.manage().window().maximize();
}
@Test(dataProvider = "fetchDatafromExcel")
public void loginTest(String username, String password) {
driver.findElement(By.xpath("//input[@type='email']")).sendKeys(
username);
driver.findElement(By.xpath("//input[@type='password']")).sendKeys(
password);
driver.findElement(
By.xpath("//input[@data-testid='royal_login_button']")).click();
}
@DataProvider(name = "fetchDatafromExcel")
public Iterator<Object> fetchDatafromExcel()
throws EncryptedDocumentException, InvalidFormatException,
IOException {
ArrayList<Object> testdata = ExcelUtilites.getDataFromExcel();
return testdata.iterator();
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
答案 0 :(得分:0)
数据提供者的返回类型是Iterator<Object[]>
。您缺少数组部分。测试方法需要Object
作为参数。
添加了一些通用代码,在其中实现了excel访问逻辑。
public class Credentials {
private String userName;
private String password;
public Credentials() { }
public Credentials(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
}
@Test(dataProvider = "dpit")
public void f(Credentials cred) {
System.out.println(cred.getUserName());
System.out.println(cred.getPassword());
}
@DataProvider(name = "dpit")
public Iterator<Credentials[]> createData() {
Credentials[] s1arr = {new Credentials("hello", "hellopass")};
Credentials[] s2arr = {new Credentials("bye", "byepass")};
List<Credentials[]> sm = new ArrayList<>();
sm.add(s1arr);
sm.add(s2arr);
return sm.iterator();
}