Home > database >  Data Provider Mismatch in Selenium with TestNG and Java
Data Provider Mismatch in Selenium with TestNG and Java

Time:09-17

Got some problem here, could you please navigate me how to resolve it?

So here is my code to read 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;
    }
}

My code for the test case

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

    @Test(dataProvider = "fetchData")
    public void signUpWithUnicode(String unicode) throws InterruptedException {
        invokeBrowser("chrome", "https://master.signup");
        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')]"));
        closeBrowser();
    }
        
    @DataProvider (name = "fetchData")
    public String[][] getData() throws IOException {
        ReadExcel excel = new ReadExcel();
        return excel.readExcel();   
    }
}

And here is the error

[RemoteTestNG] detected TestNG version 7.0.0
FAILED: signUpWithUnicode
org.testng.internal.reflect.MethodMatcherException: 
[public void testCases.SignUpWithUnicode.signUpWithUnicode(java.lang.String) throws java.lang.InterruptedException] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation).
Data provider mismatch

I am using Unicode characters in my Excel sheet, so I tried to use Object instead of String, but still having this error. Then I tried to use the strings in my Excel just to find the error and still no success.

CodePudding user response:

TestNG Data provider works in a different way. If you are returning Object[][], then the test method should have a number of arguments which is equal to the number of columns in the 2d array. The number of rows represents the number of test cases.

Example : If you are returning String[2][3], then the test method would be invoked 2 times and 3 parameters should be present in the test method.

@DataProvider (name = "fetchData")
public String[][] getData()  {   
    return new String[][] { { "a", "b", "c" }, { "x", "y", "z" } };   
}


@Test(dataProvider = "fetchData")
public void testMethod(String one, String two, String three) {
    System.out.println(one   two   three);
}

Here the output would be

abc
xyz

In your case, the length of the data cannot be predetermined as the data is loaded from excel. So having a specific set of parameters in the test method will not work. So, instead what you may do is put the 2d array returned by the excel reading method inside another 2d-array:

@DataProvider (name = "fetchData")
public String[][] getData() throws IOException {
    ReadExcel excel = new ReadExcel();
    // we return 1 row with 1 column
    return new String[][] { { excel.readExcel() } };   
}


@Test(dataProvider = "fetchData")
public void signUpWithUnicode(String[][] data) throws InterruptedException {
      // here you could loop the data and work on it...
}

CodePudding user response:

The error is saying Data provider mismatch

You are returning the data as Multi dimensional array in ReadExcel class but you are passing the data as String in your test signUpWithUnicode so change the argument type.

@Test(dataProvider = "fetchData")
    public void signUpWithUnicode(String[][] unicode) throws InterruptedException {
   //code
  }
  • Related