org.testng.TestNGException:无法实例化类

时间:2017-08-17 08:06:59

标签: java selenium

我编写了以下代码,使用selenium驱动程序调用Google浏览器:

package automation_se;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;


public class InvokeChrome {

    public WebDriver driver; 


public InvokeChrome() {

        System.setProperty("webdriver.chrome.driver", "D:\\downloads\\chromedriver_win32\\chromedriver");
        driver = new ChromeDriver(); 
        driver.manage().window().maximize();

    }

    @Test
    public void chromeProgram() {

    }

}

在运行程序和TestNG测试时,它会抛出以下异常:

org.testng.TestNGException: 
Cannot instantiate class automation_se.InvokeChrome
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:382)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:295)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:118)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:183)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:128)
    at org.testng.TestRunner.initMethods(TestRunner.java:416)
    at org.testng.TestRunner.init(TestRunner.java:242)
    at org.testng.TestRunner.init(TestRunner.java:212)
    at org.testng.TestRunner.<init>(TestRunner.java:166)
    at org.testng.remote.support.RemoteTestNG6_9_7$1.newTestRunner(RemoteTestNG6_9_7.java:27)
    at org.testng.remote.support.RemoteTestNG6_9_7$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_9_7.java:63)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:587)
    at org.testng.SuiteRunner.init(SuiteRunner.java:161)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:114)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1290)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1277)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1131)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 21 more
Caused by: java.lang.IllegalStateException: The driver executable does not exist: D:\downloads\chromedriver_win32\chromedriver
    at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:121)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:116)
    at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
    at automation_se.InvokeChrome.<init>(InvokeChrome.java:17)
    ... 26 more

任何人都可以帮我解决这个例外吗?将不胜感激。

我已经阅读了类似的问题,但他们似乎都有特定于程序的解决方案。

1 个答案:

答案 0 :(得分:0)

异常原因IllegalStateException明确说明

  

驱动程序可执行文件不存在:D:\ downloads \ chromedriver_win32 \ chromedriver

您应该验证路径是否正确并添加扩展名,修改如下:

System.setProperty("webdriver.chrome.driver",
    "D:\\downloads\\chromedriver_win32\\chromedriver.exe");