0

If I don't use TestNG and just plain Java Selenium, all good. But if I use TestNG with Java Selenium , I get this error.

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

I already put the path to the driver executeable and the compiler still complains. Any suggestion? Thanks.

package testSuite;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Driver;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.junit.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class testNG 
{
	
	@Test
	public void login() throws IOException {
		System.setProperty("WebDriver.Chrome.Driver", "C:\\Users\\Desktop\\chromedriver_win32\\chromedriver.exe");
		WebDriver driver = null; 
		Properties prop = new Properties(); 
		FileInputStream file = new FileInputStream("C:\\Users\\workspace\\Selenium\\src\\testSuite\\config.properties");
		prop.load(file);
		
		System.out.println(prop.getProperty("username"));
		
		if(prop.getProperty("browser").equals("chrome")) {
			System.out.println("OKOK");
			driver = new ChromeDriver(); 
			
		}
		driver.get(prop.getProperty("url"));
		
	}
  }

This is my properties file

username = 56987
password = 1234
url = www.google.com
browser = chrome
1
  • I cannot check now, but off the top of my head that's not how you set the path to the driver. No idea why without TestNG that works. You must create a ChromeOptions instance, set the path on it, and set that instance into the driver instance, I don't remember if as an arg of the constructor or through a setter. Commented Mar 25, 2017 at 7:59

3 Answers 3

2

I found the issue. Just a tiny small mistake.

It should be lower case webdriver.chrome.driver

System.setProperty("webdriver.chrome.driver", "C:\\Users\\Desktop\\chromedriver_win32\\chromedriver.exe");

instead of

System.setProperty("WebDriver.Chrome.Driver", "C:\\Users\\Desktop\\chromedriver_win32\\chromedriver.exe");

Need to be careful with the lower or upper case. Thanks.

Sign up to request clarification or add additional context in comments.

Comments

1

Try not to hardcode the path.

You can directly use System.getProperty("user.dir") to go to your working directory.

And Yes use webdriver.chrome.driver in lower case.

System.setProperty("webdriver.chrome.driver",
                System.getProperty("user.dir")+ "/chromedriver.exe");

Comments

1

If you are running the testng script from command line, you might need to add the following switch-

-Dwebdriver.chrome.driver=<path to chromedriver.exe>\chromedriver.exe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.