为什么我尝试从Excel文件中读取数据时会出现数据提供程序不匹配错误?

huangapple go评论75阅读模式
英文:

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

问题

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

File2:

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:
Error Screenshot

Excel File:
Excel File Screenshot

英文:

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=&quot;testdata&quot;) 
public void login(String username) throws InterruptedException
{
System.setProperty(&quot;webdriver.chrome.driver&quot;,
C:\\Users\\abc\\Downloads\\chromedriver_win32\\chromedriver.exe&quot;);
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(&quot;www.abc.com/AdminHome.aspx&quot;);
driver.findElement(By.id(&quot;M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_txtLoginName&quot;)).
sendKeys(&quot;admin&quot;);   
driver.findElement(By.id(&quot;M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_txtPassword&quot;)).
sendKeys(&quot;Password&quot;);    
driver.findElement(By.id(&quot;M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_cmdContinue&quot;)).
click();
driver.findElement(By.id(&quot;M_layout_content_PCDZ_M5QH8YG_ctl00_lblUserName&quot;)).
click();
driver.findElement(By.id(&quot;M_layout_content_PCDZ_M5QH8YG_ctl00_txtUserName&quot;)).
sendKeys(username);
driver.findElement(By.id(&quot;M_layout_content_PCDZ_M5QH8YG_ctl00_btnSearch&quot;)).click();
driver.findElement(By.id(&quot;M_layout_content_PCDZ_M5QH8YG_ctl00_resultsGrid_ctl02_LogInAsUser&quot;)).
click(); 			
System.out.println(&quot;User is able to login successfully&quot;);
driver.findElement(By.xpath(&quot;/html/body/form/div[3]/div[3]/div[1]/div[1]/div/div[5]/ul/li[6]/a&quot;)).
click();	      
@DataProvider(name=&quot;testdata&quot;)
public Object[][] TestDataFeed()
{
ReadExcelFile config = new 
ReadExcelFile(&quot;C:\\Users\\abc\\eclipseworkspace\\Login\\testdata\\testdata.xlsx&quot;);
int rows = config.getRowCount(0);
Object[][] credentials = new Object[rows][2];
for(int i = 0; i &lt; 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文件中读取数据时会出现数据提供程序不匹配错误?

Excel File:
为什么我尝试从Excel文件中读取数据时会出现数据提供程序不匹配错误?

答案1

得分: 1

你正在声明 Object[][] credentials = new Object[rows][2];,但你只填充了一列,另一列保持为空(null)。因此,你的数组维数与你的方法接受的参数数量不匹配。

通过将该行更改为以下内容来修复:

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

英文:

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

huangapple
  • 本文由 发表于 2020年9月21日 23:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定