简体   繁体   English

为什么我在尝试读取 excel 文件时收到数据提供程序不匹配错误?

[英]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.一个是我的主要方法文件,一个是我的 excel 配置文件。 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:文件 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: Excel 档案: 在此处输入图像描述

You are declaring Object[][] credentials = new Object[rows][2];您正在声明Object[][] credentials = new Object[rows][2]; but you fill only one column keeping other column empty ( null ).但您只填写一列,其他列为空( null )。 Hence the dimension of your array does not match the number of arguments your method accepts.因此,您的数组维度与您的方法接受的 arguments 的数量不匹配。

Fix it with changing that line to:通过将该行更改为以下内容来修复它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到数据提供者不匹配错误? - Why I am getting Data provider mismatch error? 尝试从文件中读取文件时为什么会出现错误? - Why am I getting an error when it tries to read from the file? 尝试从文件中读取某些字节时,为什么会超出索引范围? - Why am I getting an index out of bounds when I try to read certain bytes from a file? 为什么在尝试读取.DOCX文件时出现异常? - Why am I getting exception as I try to read .DOCX file? 为什么会出现类型不匹配错误? - Why am I getting Type mismatch error? 为什么我收到错误“类型不匹配”? - Why i am getting error “Type Mismatch”? 当我尝试从getter获取数据时,为什么会出现NullPointerException? - Why am I getting a NullPointerException when I try to get data from a getter? 当我尝试从Jframe中的表中获取字符串时,为什么会出现错误? - Why am I getting an error when I try to get a string from my table in a Jframe? 当我尝试从excel文件读取时出现ClassNotFoundException - i get ClassNotFoundException when i try to read from excel file 使用Scanner读取文件:为什么在Java中使用Scanner读取文件时出现错误? - read a file using Scanner: Why am I getting an error when using Scanner for read files in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM