Happy
Happy

Reputation: 61

Why I am getting Data provider mismatch error?

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

Screenshot: File1

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:

File 2

Screenshot:

Console Error window

Upvotes: 0

Views: 246

Answers (1)

Peter Quan
Peter Quan

Reputation: 808

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

Upvotes: 1

Related Questions