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

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

Why I am getting Data provider mismatch error?

问题

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][1];  // Changed from [rows][2] to [rows][1]
        for(int i=0; i<rows; i++) {  // Removed semicolon
            credentials[i][0] = config.getData(0, i, 0);
        }
        return credentials;
    }
}
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;
    }
}

Please note that I've made some adjustments to your code to fix the issues mentioned in your original description. Be sure to replace your existing code with these corrected parts.

英文:

I am trying to read excel file using Data Provider Annotation and I am getting Data provider mismatch error. My script is simple. I have one column in excel and that is username. I am trying to read that data from row 0, column0 from my .xlsx file and it will load it into my program. Below are two files and their corresponding screenshots. File 1 is my main program and File 2 is my 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.

File 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=&quot;testdata&quot;) 
public void login(String username)
{
System.setProperty(&quot;webdriver.chrome.driver&quot;, &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;abc.com/Home.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_LogIn&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\\eclipse-workspace\\Login\\testdata\\username.xlsx&quot;);
int rows = config.getRowCount(0);
Object[][] credentials = new Object[rows][2];
for(int i=0;i&lt;rows;i++);
{
int i = 0;
credentials[i][0] = config.getData(0, i, 0);
}
return credentials;
}
}

Screenshot:
为什么我会收到数据提供程序不匹配错误?

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

Screenshot 2:

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

Screenshot:

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

答案1

得分: 1

在方法`TestDataFeed()`有两个句子不正确
```java
for(int i=0;i&lt;rows;i++); // &lt;-- 分号符号很奇怪
{
    int i = 0;           // &lt;-- 与for循环中的变量i重复
    credentials[i][0] = config.getData(0, i, 0);
    
}

改为

for (int i = 0; i &lt; rows; i++)
{
    credentials[i][0] = config.getData(0, i, 0);
}
英文:

In method TestDataFeed(), there are two sentence not correct.

for(int i=0;i&lt;rows;i++); // &lt;-- the ; symbol is weird
{
    int i = 0;           // &lt;-- 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 &lt; rows; i++)
{
    credentials[i][0] = config.getData(0, i, 0);
}

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

发表评论

匿名网友

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

确定