简体   繁体   English

为什么我收到数据提供者不匹配错误?

[英]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.我正在尝试使用数据提供程序注释读取 excel 文件,但出现数据提供程序不匹配错误。 My script is simple.我的脚本很简单。 I have one column in excel and that is username.我在excel中有一个列,那就是用户名。 I am trying to read that data from row 0, column0 from my .xlsx file and it will load it into my program.我正在尝试从我的 .xlsx 文件中的第 0 行、第 0 列读取该数据,并将其加载到我的程序中。 Below are two files and their corresponding screenshots.下面是两个文件及其对应的屏幕截图。 File 1 is my main program and File 2 is my ExcelConfig.文件 1 是我的主程序,文件 2 是我的 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.如果我硬编码用户名只是在通过 excel 文件使用时出错,我的代码就可以正常工作。

File 1文件 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:截屏: 文件 1

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

Screenshot 2:截图2:

档案 2

Screenshot:截屏:

控制台错误窗口

In method TestDataFeed() , there are two sentence not correct.在方法TestDataFeed() ,有两句话不正确。

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

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

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