1

我是 Selenium 和 Java 的新手。关于我的代码的基本思想是,我在我的 java 项目下创建了两个类,即第一个和第二个。我的第一个类包含所有方法(打开浏览器、登录、注销),我的第二个类包含从第一个继承所有方法的主程序。

我创建了一个 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 org.testng.reporters.SuiteHTMLReporter@9fa12f: 16 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1cc0a0f: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@e43b44: 0 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@109062e: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1d7e4d6: 31 ms

恳请您对此提供帮助。

Excel工作表的屏幕截图:

TD    TCname        TCDescription          Execute  

       TC01                login                 Y  

       TC02                logout                N  

                                                               TD

添加 IE 路径后的新错误报告

[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 org.testng.reporters.SuiteHTMLReporter@9fa12f: 78 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1cc0a0f: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@e43b44: 47 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@109062e: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1d7e4d6: 110 ms
4

2 回答 2

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();

希望有帮助。

于 2013-04-05T20:42:44.617 回答
1

试试下面的代码,也许能解决问题 -

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 ...
于 2013-08-16T14:03:39.817 回答