org.testng.TestNGException: Cannot instantiate class

I have written the following code to invoke the Google browser using selenium driver:

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

    }

}

On running the program as as TestNG test, it throws the following exception:

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:downloadschromedriver_win32chromedriver
    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

Can anyone please help me with this exception? Will be grateful.

I have read similar questions but they all seem to have program specific solutions.

Answer

The exception cause IllegalStateException clearly states

The driver executable does not exist: D:downloadschromedriver_win32chromedriver

You should probably verify that the path is correct and add the extension, modify like this:

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

Leave a Reply

Your email address will not be published. Required fields are marked *