简体   繁体   中英

Why I am getting Data Provider MisMatch error when I try to read from excel file?

I am trying to do data driven testing and I am not able to figure it out why I am getting data mismatch. I am trying to automate just usernames and that is in form of string. Can you please review my code and tell me what I am doing wrong? Below are two files. one is my main method file and one is my excel config file. I have also attached screenshot of my error. Any help would be appreciated. Thank you.

File1:

`

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) throws InterruptedException
{
    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("www.abc.com/AdminHome.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_LogInAsUser")).
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\\eclipseworkspace\\Login\\testdata\\testdata.xlsx");

int rows = config.getRowCount(0);
Object[][] credentials = new Object[rows][2];
for(int i = 0; i < rows; i++)

{

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;
 }
 }
 `

Error Screenshot:

在此处输入图像描述

Excel File: 在此处输入图像描述

You are declaring Object[][] credentials = new Object[rows][2]; but you fill only one column keeping other column empty ( null ). Hence the dimension of your array does not match the number of arguments your method accepts.

Fix it with changing that line to:

Object[][] credentials = new Object[rows][1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM