Selenium, Selenium Errors, Testing, Uncategorized, Web Automation, Web Testing

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:177)

at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:89)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:106)

Problem statement:  Look at the below code and analyze the issue with the snippet.
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
<style="text-align: left;">public class UserInterfaceTest {
WebDriver driver = new ChromeDriver();
<style="text-align: left;">@Before
public void startDriver() {
System.setProperty("webdriver.chrome.driver",
"~Path to your chrome drivero~//chromedriver_win32//chromedriver.exe");
<style="text-align: left;">driver.get("http://www.google.in");
}
<style="text-align: left;">@Test
public void getWebSite() {
}
<style="text-align: left;">@After
public void killSession() {
driver.close();
}}

What this code is doing is it is instantiating the driver first before any function is called WebDriver driver = new ChromeDriver(); as an instance variable  ,what it will do is it will make an instance of selenium web driver and keep it in driver.

Now when you define the path of the webdriver in @Before method then it already has a definition of driver and that does not have any path of chrome driver in it. So during the execution of this code , above error will come and no driver shall get open.

Resolution:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class UserInterfaceTest {
WebDriver driver = null;// you need to instantiate driver first and give it some values like null .
@Before
public void startDriver() {
System.setProperty("webdriver.chrome.driver",
"F://Selenium//chromedriver_win32//chromedriver.exe"); // Here mention the path to run chromedriver as property
driver = new ChromeDriver(); //  Here you need to tell which driver you want to use
driver.get("http://www.google.in");
}
@Test
public void getWebSite() {
System.out.println("Test 1");
}
@After
public void killSession() {
driver.close();
}

This code is a fix for the above error ,What this will do is it will take null value first will instantiation of Web driver . And further when you want to use it in any method then only it will take the value of driver i.e. either it is Fire fox or Chrome or any other browser you want to run your tests on.

And then it will take the path of the driver once you will define which driver you want to use. So for cases where you want to use web browser other than Fire Fox you need to define driver for the same and also need to set property for the same web browser.

And this is how you can get rid of this issue.

Advertisements

1 thought on “java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s