2013-01-17 57 views
-2

傳遞數據我開始使用Eclipse和TestNG學習Selenium2(webdriver的)。我有一個關於DataProvider的問題。例如,我有一個登錄頁面,其中包含用戶,密碼和登錄按鈕。我在TestNG中寫了一個測試。我已經在另一個類中使用了UI對象(具有單獨的類)和實際測試的pageobject。的DataProvider TestNG中從Excel

這裏glogin是一個類和登錄在這裏找到元素和發送鍵完成的功能與這被稱爲在另一個類中GTEST(這是主要的測試),其具有TestNG的註釋。

我訪問將於值主腳本類。

我有以下excel表

user  pass  
John  Smith  
Carson Black  
Carla  ck23 
test  test4 

當我使用數據提供器,並從Excel工作表作爲陣列獲取數據,並用它在試驗則顯示以下錯誤:

org.testng.TestNGException: 

The data provider is trying to pass 4 parameters but the method plus.gmail#glogin takes 2 
    at org.testng.internal.Invoker.injectParameters(Invoker.java:1337) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1225) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
    at org.testng.TestRunner.privateRun(TestRunner.java:767) 
    at org.testng.TestRunner.run(TestRunner.java:617) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128) 
    at org.testng.TestNG.run(TestNG.java:1036) 
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 

任何幫助真的很感激。

這裏是方法的代碼與數據提供器和註釋

@DataProvider(name="test") 
     public Object[][] createdata1()throws Exception 
     { 
      Object[][] retobj = getexcel(); 
      return retobj;    
     } 

     private String[][] getexcel() throws Exception 
     { 
      // TODO Auto-generated method stub 

      String[][] tabarray = null; 
      try { 
       Workbook wb1 = Workbook.getWorkbook(new 

File("F:/testdata.xls")); 

       Sheet sheet = wb1.getSheet("userlogin"); 

       Cell tablestart = sheet.findCell("login"); 

       int startrow = tablestart.getRow(); 
       int startcol = tablestart.getColumn(); 

       Cell tableend = sheet.findCell("login",startcol+1,startrow+1, 

100, 64000, false); 

       int endrow = tableend.getRow(); 
       int endcol = tableend.getColumn(); 

       System.out.println("startRow="+startrow+", endRow="+endrow+", 

" + "startCol="+startcol+", endCol="+endcol); 

       tabarray = new String[endrow - startrow + 1][endcol - 

startcol + 1]; 

       int ci = 0; 
       for(int i = startrow +1 ;i<endrow;i++,ci++) 
       { 

        int cj = 0; 
        for(int j = startcol + 1;j<endcol;j++,cj++) 
        { 
         tabarray[ci][cj] = sheet.getCell(j, 

i).getContents(); 
         System.out.println(tabarray[ci][cj]); 

        } 
       } 

      } catch (BiffException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return tabarray; 
     } 

test(Dataprovider = "test") 
public void glogins(String user, String pass) 
{ 
    glogin log1 = new glogin(driver); 
    log1.login(user,pass); 
} 

當我執行的測試中,我從Excel接收到的數據作爲

john 
smith 
carson 
Black 
carla 
ck23 
test 
test4 

作爲輸出

+0

你能給與的DataProvider(這樣一個閱讀批註的方法的代碼excel)?我猜這個問題肯定會在那裏。 –

+1

您應該檢查是否tabarray [X]。長度爲4對任何在您的Excel行。 –

回答

0

是不是錯誤消息不言自明?

The data provider is trying to pass 4 parameters but the method plus.gmail#glogin takes 2 

使用調試器和弄清楚爲什麼你的數據提供者返回4個參數,而不是僅僅2

0

使用

tabarray = new String[endrow - 1][endcol - 1]; instead of //tabarray = new String[endrow - startrow + 1][endcol - startcol + 1]; 

因爲你打算返回只有2 * 2陣列

-1

我同意發表格利揚的反應;原因是數據提供者excel表的第一列和最後一列也被函數計入參數/參數;因此請使用tabArray=new String[endRow-startRow-1][endCol-startCol-1];希望這有助於幸福的測試...

+1

這聽起來更像是評論而不是答案。 – Math

0

替換行

tabarray = new String[endrow - startrow + 1][endcol - startcol + 1]; 

tabarray = new String[endrow - startrow - 1][endcol - startcol - 1];