为什么会出现数据提供程序不匹配错误?

时间:2020-09-20 16:37:24

标签: java selenium selenium-webdriver

我正在尝试使用数据提供程序注释读取excel文件,并且出现数据提供程序不匹配错误。我的脚本很简单。我在excel中有一栏,那就是用户名。我正在尝试从.xlsx文件的第0行,第0列读取该数据,并将其加载到我的程序中。以下是两个文件及其对应的屏幕截图。文件1是我的主程序,文件2是我的ExcelConfig。另外,我还附有控制台窗口的屏幕截图。如果我通过硬编码对用户名进行硬编码而导致错误,那么我的代码就可以正常工作。

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

截屏: File1

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

截屏2:

File 2

截屏:

Console Error window

1 个答案:

答案 0 :(得分:1)

在方法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);
    
}

更改为

for (int i = 0; i < rows; i++)
{
    credentials[i][0] = config.getData(0, i, 0);
}