2013-04-05 43 views
1

我是Selenium和Java的新手。我的代碼的基本思想是,我創建了兩個類,即我的Java項目下的第一個和第二個。我的第一個類包含所有的方法(打開瀏覽器,登錄,註銷),而我的第二個類包含繼承所有方法的主程序。使用Java調用Selenium Webdriver從類到另一個方法

我已經創建了一個excel,說明哪個測試用例(登錄,註銷)應該基於Excel工作表運行。測試用例將根據Excel工作表執行列下的「Y」或「N」執行

當我試圖運行第二課時,它顯示一個錯誤。請在下面找到我的代碼,並將錯誤和Excel表單作爲測試案例應該運行的圖像。

這是我第二類(主程序)

package asdf; 

import java.io.File; 

import jxl.Cell; 
import jxl.Sheet; 
import jxl.Workbook; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.SkipException; 
import org.testng.annotations.DataProvider; 
import org.testng.annotations.Test; 

public class second {     

    @DataProvider(name = "DP1") 
    public Object[][] createData1() throws Exception { 
     Object[][]retObjArr=getTableArray("Resource\\Testdata.xls","credentials","TD"); 
     return(retObjArr); 
    } 

    @Test(dataProvider = "DP1") 
    public void execution(String TCname, String TCDescription, String Execute) { 
     first r=new first(); 
     if(Execute.equalsIgnoreCase("Y")) { 
      if(TCname.equalsIgnoreCase("TC01")) { 
       r.login(); 
      } else if(TCname.equalsIgnoreCase("TC02")) { 
       r.login(); 
      r.logout(); 
     } 
     } else { 
     throw new SkipException("Skipping this test case: "+TCname); 
     } 
    }  

    public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception { 
     String[][] tabArray=null; 
     Workbook workbook = Workbook.getWorkbook(new File(xlFilePath)); 
     Sheet sheet = workbook.getSheet(sheetName); 
     int startRow,startCol, endRow, endCol, ci, cj; 
     Cell tableStart=sheet.findCell(tableName); 
     startRow = tableStart.getRow(); 
     startCol = tableStart.getColumn(); 
     Cell tableEnd = sheet.findCell(tableName, startCol+1, startRow+1, 100, 64000, false); 
     endRow = tableEnd.getRow(); 
     endCol=tableEnd.getColumn(); 

     System.out.println("startRow="+startRow+", endRow="+endRow+", " + "startCol="+startCol+", endCol="+endCol); 
     tabArray = new String[endRow-startRow-1][endCol-startCol-1]; 
     ci=0; 

     for (int i=startRow+1;i<endRow;i++,ci++) { 
      cj=0; 
      for (int j=startCol+1;j<endCol;j++,cj++) { 
       tabArray[ci][cj]=sheet.getCell(j,i).getContents(); 
      } 
     } 
     return(tabArray); 
    } 
}   

**This is my First class (which contains all the methods and should return these methods to Second class)** 
package asdf; 

import java.io.File;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
import org.openqa.selenium.ie.InternetExplorerDriver;  
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.testng.annotations.BeforeMethod;  
import org.testng.annotations.Test; 

public class first { 

    public static WebDriver driver; 
    public String baseUrl; 
    static{ 
    driver =new InternetExplorerDriver(); 
    String baseUrl = "http://newtours.demoaut.com/"; 

    } 

public void login() 

{ 

    driver.get(baseUrl); 

    driver.findElement(By.name("Email")).sendKeys("mercury"); 

    driver.findElement(By.name("Passwd")).sendKeys("mercury"); 

    driver.findElement(By.name("signIn")).click(); 
    } 

public void logout() 

{ 

    driver.findElement(By.xpath(".//*[@id='gb_71']")); 

} 

} 

它拋出異常如下

> [TestNG] Running: 
    C:\Users\SRI\AppData\Local\Temp\testng-eclipse--2072559575\testng-customsuite.xml 

startRow=0, endRow=3, startCol=0, endCol=4 
FAILED: execution("TC01", "login", "Y") 
java.lang.ExceptionInInitializerError 
    at asdf.second.execution(second.java:28) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
    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) 
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver. The latest version can be downloaded from http://code.google.com/p/selenium/downloads/list 
    at com.google.common.base.Preconditions.checkState(Preconditions.java:176) 
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105) 
    at org.openqa.selenium.ie.InternetExplorerDriverService.access$1(InternetExplorerDriverService.java:1) 
    at org.openqa.selenium.ie.InternetExplorerDriverService$Builder.build(InternetExplorerDriverService.java:177) 
    at org.openqa.selenium.ie.InternetExplorerDriver.setupService(InternetExplorerDriver.java:111) 
    at org.openqa.selenium.ie.InternetExplorerDriver.setup(InternetExplorerDriver.java:104) 
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:51) 
    at asdf.first.<clinit>(first.java:19) 
    ... 25 more 

FAILED: execution("TC02", "logout", "N") 
java.lang.NoClassDefFoundError: Could not initialize class asdf.first 
    at asdf.second.execution(second.java:28) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
    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) 


=============================================== 
    Default test 
    Tests run: 2, Failures: 2, Skips: 0 
=============================================== 


=============================================== 
Default suite 
Total tests run: 2, Failures: 2, Skips: 0 
=============================================== 

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms 
[TestNG] Time taken by [email protected]: 16 ms 
[TestNG] Time taken by [email protected]: 0 ms 
[TestNG] Time taken by [email protected]: 0 ms 
[TestNG] Time taken by [email protected]: 0 ms 
[TestNG] Time taken by [email protected]: 31 ms 

請給出您的請求在這個幫助。 Excel工作表的

屏幕截圖:

TD TCname  TCDescription   Execute 

     TC01    login     Y 

     TC02    logout    N 

                   TD 

新的錯誤報告後,加入IE PATH

[TestNG] Running: 
    C:\Users\SRI\AppData\Local\Temp\testng-eclipse--824275806\testng-customsuite.xml 

startRow=0, endRow=3, startCol=0, endCol=4 
FAILED: execution("TC01", "login", "Y") 
java.lang.ExceptionInInitializerError 
    at asdf.second.execution(second.java:28) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
    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) 
Caused by: java.lang.IllegalStateException: The driver executable does not exist: C:\Selenium\iexploredriver.exe 
    at com.google.common.base.Preconditions.checkState(Preconditions.java:176) 
    at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:117) 
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:112) 
    at org.openqa.selenium.ie.InternetExplorerDriverService.access$1(InternetExplorerDriverService.java:1) 
    at org.openqa.selenium.ie.InternetExplorerDriverService$Builder.build(InternetExplorerDriverService.java:177) 
    at org.openqa.selenium.ie.InternetExplorerDriver.setupService(InternetExplorerDriver.java:111) 
    at org.openqa.selenium.ie.InternetExplorerDriver.setup(InternetExplorerDriver.java:104) 
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:51) 
    at asdf.first.<clinit>(first.java:21) 
    ... 25 more 

FAILED: execution("TC02", "logout", "N") 
java.lang.NoClassDefFoundError: Could not initialize class asdf.first 
    at asdf.second.execution(second.java:28) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
    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) 


=============================================== 
    Default test 
    Tests run: 2, Failures: 2, Skips: 0 
=============================================== 


=============================================== 
Default suite 
Total tests run: 2, Failures: 2, Skips: 0 
=============================================== 

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 15 ms 
[TestNG] Time taken by [email protected]: 78 ms 
[TestNG] Time taken by [email protected]: 0 ms 
[TestNG] Time taken by [email protected]: 47 ms 
[TestNG] Time taken by [email protected]: 0 ms 
[TestNG] Time taken by [email protected]: 110 ms 
+0

答案是錯誤日誌。您需要爲webdriver設置InternetExplorer驅動程序的路徑。 – Ioan 2013-04-05 19:44:00

回答

1

你缺少的路徑,可執行IE的驅動程序。試試這個:

File file = new File("C:/Selenium/iexploredriver.exe"); // your location of driver 
System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); 
WebDriver driver = new InternetExplorerDriver(); 

希望有所幫助。

+0

即使在添加IE路徑後,它也會引發錯誤..我在問題中添加了新的錯誤報告 – Sriram 2013-04-07 07:09:26

1

嘗試下面的代碼,可以B檢查解決問題 -

public class first { 

{ 

    System.setProperty("webdriver.ie.driver", "D:\\Pradeep\\pradeep\\IEDriverServer.exe"); 
     // your location of driver 


} 

WebDriver driver = new InternetExplorerDriver(); 

public String baseUrl = "http://newtours.demoaut.com/"; 

......那麼你的代碼

@test ... 
相關問題