英文:
Why I am getting Data provider mismatch error?
问题
package loginAdmin;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class PersonateUser {
@Test(dataProvider="testdata")
public void login(String username) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\abc\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(200, TimeUnit.SECONDS);
driver.get("abc.com/Home.aspx");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_txtLoginName")).sendKeys("admin");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_txtPassword")).sendKeys("Password");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_cmdContinue")).click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_lblUserName")).click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_txtUserName")).sendKeys(username);
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_btnSearch")).click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_resultsGrid_ctl02_LogIn")).click();
System.out.println("User is able to login successfully");
driver.findElement(By.xpath("/html/body/form/div[3]/div[3]/div[1]/div[1]/div/div[5]/ul/li[6]/a")).click();
}
@DataProvider(name="testdata")
public Object[][] TestDataFeed() {
ReadExcelFile config = new ReadExcelFile("C:\\Users\\abc\\eclipse-workspace\\Login\\testdata\\username.xlsx");
int rows = config.getRowCount(0);
Object[][] credentials = new Object[rows][1]; // Changed from [rows][2] to [rows][1]
for(int i=0; i<rows; i++) { // Removed semicolon
credentials[i][0] = config.getData(0, i, 0);
}
return credentials;
}
}
package loginAdmin;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelFile {
XSSFWorkbook wb;
XSSFSheet sheet;
public ReadExcelFile(String excelPath) {
try {
File src = new File(excelPath);
FileInputStream fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public String getData(int sheetnumber, int row, int column) {
sheet = wb.getSheetAt(sheetnumber);
String data = sheet.getRow(row).getCell(column).getStringCellValue();
return data;
}
public int getRowCount(int sheetIndex) {
int row = wb.getSheetAt(sheetIndex).getLastRowNum();
row = row + 1;
return row;
}
}
Please note that I've made some adjustments to your code to fix the issues mentioned in your original description. Be sure to replace your existing code with these corrected parts.
英文:
I am trying to read excel file using Data Provider Annotation and I am getting Data provider mismatch error. My script is simple. I have one column in excel and that is username. I am trying to read that data from row 0, column0 from my .xlsx file and it will load it into my program. Below are two files and their corresponding screenshots. File 1 is my main program and File 2 is my ExcelConfig. Also I have attach screenshot of console window. My code works fine if I hard code username just erroring out when I use through excel file.
File 1
package loginAdmin;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class PersonateUser {
@Test(dataProvider="testdata")
public void login(String username)
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\abc\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(200, TimeUnit.SECONDS);
driver.get("abc.com/Home.aspx");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_txtLoginName")).sendKeys("admin");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_txtPassword")).sendKeys("Password");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_cmdContinue")).click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_lblUserName")).click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_txtUserName")).sendKeys(username);
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_btnSearch")).click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_resultsGrid_ctl02_LogIn")).click();
System.out.println("User is able to login successfully");
driver.findElement(By.xpath("/html/body/form/div[3]/div[3]/div[1]/div[1]/div/div[5]/ul/li[6]/a")).click();
}
@DataProvider(name="testdata")
public Object[][] TestDataFeed()
{
ReadExcelFile config = new ReadExcelFile("C:\\Users\\abc\\eclipse-workspace\\Login\\testdata\\username.xlsx");
int rows = config.getRowCount(0);
Object[][] credentials = new Object[rows][2];
for(int i=0;i<rows;i++);
{
int i = 0;
credentials[i][0] = config.getData(0, i, 0);
}
return credentials;
}
}
File 2:
package loginAdmin;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelFile
{
XSSFWorkbook wb;
XSSFSheet sheet;
public ReadExcelFile(String excelPath)
{
try
{
File src = new File(excelPath);
FileInputStream fis = new FileInputStream(src);
wb =new XSSFWorkbook(fis);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public String getData(int sheetnumber, int row, int column)
{
sheet= wb.getSheetAt(sheetnumber);
String data = sheet.getRow(row).getCell(column).getStringCellValue();
return data;
}
public int getRowCount(int sheetIndex)
{
int row = wb.getSheetAt(sheetIndex).getLastRowNum();
row = row + 1;
return row;
}
}
Screenshot 2:
Screenshot:
答案1
得分: 1
在方法`TestDataFeed()`中,有两个句子不正确。
```java
for(int i=0;i<rows;i++); // <-- 分号符号很奇怪
{
int i = 0; // <-- 与for循环中的变量i重复
credentials[i][0] = config.getData(0, i, 0);
}
改为
for (int i = 0; i < rows; i++)
{
credentials[i][0] = config.getData(0, i, 0);
}
英文:
In method TestDataFeed()
, there are two sentence not correct.
for(int i=0;i<rows;i++); // <-- the ; symbol is weird
{
int i = 0; // <-- Duplicate local variable i with the one in for-loop
credentials[i][0] = config.getData(0, i, 0);
}
change to
for (int i = 0; i < rows; i++)
{
credentials[i][0] = config.getData(0, i, 0);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论