bugfix> testng-dataprovider > 投稿

コードの下で試してみましたが、データプロバイダーの不一致エラーを受け取りました。誰もこれを手伝ってもらえますか?

package appModules;
import org.testng.annotations.Test;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeTest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
public class NewTest {
public WebDriver driver;
public WebDriverWait wait;
String appURL = 
"https://dev.agencyport.rsagroup.ca:8443/agencyportal/ProcessLogoff";
//Locators
private By username = By.id("USERID");
private By password = By.id("PASSWORD");
@BeforeClass
public void testSetup() {
    System.setProperty("webdriver.firefox.marionette", 
"C:\\Automation\\geckodriver.exe");
    driver=new FirefoxDriver();
    driver.manage().window().maximize();
    wait = new WebDriverWait(driver, 5);

}
@Test(dataProvider = "login")
public void Login(String Username, String Password) {
  driver.findElement(username).sendKeys(Username);
  driver.findElement(password).sendKeys(Password);
}
@DataProvider (name="login")
public Object[][] dp() throws Exception{
  Object[][] arrayObject = 
getExcelData("C:\\Automation\\testData.xls","New");
  return arrayObject;
}
public String[][] getExcelData(String fileName, String sheetName) throws 
 Exception {
  String[][] arrayExcelData = null;
  try {
      FileInputStream fs = new FileInputStream(fileName);
      Workbook wb = Workbook.getWorkbook(fs);
      Sheet sh = wb.getSheet(sheetName);
      int totalNoOfCols = sh.getColumns();
      System.out.println(totalNoOfCols);
      int totalNoOfRows = sh.getRows();
      System.out.println(totalNoOfRows);
      arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];
      for (int i=1 ; i <totalNoOfRows; i++) {
          for (int j=0; j <totalNoOfCols; j++) {
              arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
              System.out.println(arrayExcelData[i-1][j]);
          }
      }
  } catch (FileNotFoundException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
      e.printStackTrace();
  } catch (BiffException e) {
      e.printStackTrace();
  }
  return arrayExcelData;
}
@Test
public void tearDown() {
  driver.quit();
}
}

以下のエラーを受け取りました-

org.testng.internal.reflect.MethodMatcherException: 
Data provider mismatch
Method: Login([Parameter{index=0, type=java.lang.String, 
declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, 
declaredAnnotations=[]}])
Arguments: [(java.lang.String) agent,(java.lang.String) password,
(java.lang.String) ]
atorg.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)`

回答 1 件
  • arrayObject の戻り値を渡している  不正なデータタイプ データプロバイダーメソッドでExcelファイルから文字列値を取得する必要があり、 Object で渡す必要があること 。

    参考のため、以下の例を参照してください。

       @DataProvider
        public Iterator<Object[]> getTestData()
        {
            ArrayList<Object[]> testdata = new ArrayList<Object[]>();
            try {
                reader = new excelUtility(excelTestDataFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
            sheetName = className;
            for (int rowNumber = 2; rowNumber <= reader.getRowCount(sheetName); rowNumber++) {
                String caseNo = reader.getCellData(sheetName, "Case", rowNumber);
                String emailid = reader.getCellData(sheetName, "Email ID", rowNumber);
                String password = reader.getCellData(sheetName, "Password", rowNumber);
                String message = reader.getCellData(sheetName, "Expected Result", rowNumber);
                Object ob[] =
                { caseNo, emailid, password, message };
                testdata.add(ob);
            }
            return testdata.iterator();
        }
    
    

    そして、これは @Test です  データプロバイダーの受信者:

       @Test(dataProvider = "getTestData")
        public void calllogin(String caseNO, String emailid, String password, String expectedResult) throws Exception
        {
         ******
        }
    
    

あなたの答え