While automating your web-application using webdriver, many may have faced problems in executing your cases on Google-chrome. Whenever you try to use webdriver object for executing your cases on chrome using the following code:
you most probably get the following error:
Method - 1:
Start the selenium server using the following command:
DesiredCapabilities capability = DesiredCapabilities.chrome();
you most probably get the following error:
The path to the chromedriver 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://code.google.com/p/chromium/downloads/list
The solution to this error that has been mentioned in the given URL "http://code.google.com/p/selenium/wiki/ChromeDriver" is to set the chromedriver path to the variable "webdriver.chrome.driver". But how exactly to set the path and use it for your test execution is not given there. So I though to just document it here on my blog.
For setting the "webdriver.chrome.driver" value and for using google-chrome to execute your automated tests, you can use one of the following methods:
Method - 1:
Start the selenium server using the following command:
java -Dwebdriver.chrome.driver=/path/to/chromedriver -jar selenium-server-standalone-2.7.0.jar
And in your Java code you use the chrome driver in the following way:
DesiredCapabilities capability = DesiredCapabilities.chrome();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
The advantage of using this method is that you can execute your test-cases in a remote machine and this is advantageous when you want to run it on grid.
Method - 2:
Set the system property "webdriver.chrome.driver" using System.setProperty() in your code and then try to execute your cases on Chrome.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
This method works well when your tests are getting execute on a local machine and you are not starting any selenium server.
Following are some important links for Chrome driver:
Webdriver Chromedriver page:
http://code.google.com/p/selenium/wiki/ChromeDriver
ChromeDriver download page for downloading the ChromeDriver server:
http://code.google.com/p/chromium/downloads/list
Your blog made my day dude..!! Thanks a million tonne !It was really helpful for me :)
ReplyDeleteThanks @Vasu
DeleteI am getting following error while using chrome browser:
ReplyDelete[TestNG] Running:
C:\Users\Admin\AppData\Local\Temp\testng-eclipse-995334387\testng-customsuite.xml
[5396:5276:14390343:ERROR:extension_error_reporter.cc(56)] Extension error: Expected version "1.8", but version was "5.0.1701".
FAILED: f
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 17:28:14'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_07'
Driver info: driver.version: ChromeDriver
My code is as follows:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class chrome {
@Test
public void f() {
System.setProperty("webdriver.chrome.driver", "C:/Users/Admin/AppData/Local/Google/Chrome/Application/chrome.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/");
}
}
@vikas- you need to use chromedriver and not chrome browser. Both are 2 different things.
Deleteya. with chrome driver now it's working fine. thanks varun
Deletedude, thank you so very much :)
ReplyDeleteWelcome :)
DeleteThanks man!!
ReplyDeleteHi Varun,
ReplyDeleteIam trying to invoke chrome browser using webdriver in my mac.
System.setProperty("webdriver.chrome.driver", "/Users/myname/Documents/New/chromedriver");
WebDriver driver = new ChromeDriver();
but on running gives following error as :
dyld: unknown required load command 0x80000022
FAILED: testGoogleSearch
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.5.8', java.version: '1.6.0_26'
Driver info: driver.version: ChromeDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:493)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:182)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:110)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:114)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:165)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:107)
at com.selenium.training.Firstprograms.testGoogleSearch(Firstprograms.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
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:127)
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:1197)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
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: org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.5.8', java.version: '1.6.0_26'
Driver info: driver.version: ChromeDriver
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:166)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:62)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:472)
... 30 more
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:10837/status] to be available after 20019 ms
can u let me know where am getting wrong.
I think this problem comes when the code is unable to invoke the chrome browser.
DeleteTry adding ChromeDriver.exe also to the path.
Hi Varun,
DeleteI did also added ChromeDriver.exec as in Mac OS shows exec and even den shows the same error. Do i need to place the ChromeDriver in same path as where Chrome is installed and even doing that hasnt resolved the problem.
I have another issue in Firefox and using the link (http://www.deal4loans.com/home-loans.php ) where am not getting count of items from the drop down list (City) . it shows count as 0.
but selection it does selecting any one item , but trying to get count and to display list items from drop down list shows as 0.
can you let me know the java code i need to use to get the ist of counts.
i have used xpath and driver.findelements(By) and used a variable to as a.size() and used forloop but still shows count as 0.
I dont see and .exec extension for ChromeDriver for mac downloaded from here:
Deletehttp://code.google.com/p/chromedriver/downloads/detail?name=chromedriver_mac_23.0.1240.0.zip&can=2&q=
For the drop down check your locator.The following should work:
driver.findelements(By.css("#City > option")).size();
Hi Varun,
ReplyDeleteThanks for posting this.
I'm still getting an issue I cannot understand...
"ChromeDriver cannot be resolved to a type"
Any help would be greatly appreciated.
Thanks
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import java.io.File;
import java.util.Dictionary;
public class testchapter6 {
WebDriver driver;
//System.setProperty("webdriver.chrome.driver", "C:/Users/kmcdonnell/Desktop\\Work\\testing\\Selenium\\chromedriver_win_23.0.1240.0");
@Before
public void setUp(){
System.setProperty("webdriver.chrome.driver", "C:\\Windows\\chromedriver.exe)");
driver = new ChromeDriver(); //we will update this part with each section
driver.get("http://book.theautomatedtester.co.uk/chapter4");
}
@After
public void tearDown(){
driver.quit();
}
@Test
public void testExamples(){
WebElement element = driver.findElement(
By.id("selectLoad"));
String value = element.getAttribute("value");
Assert.assertEquals("Click to load the select below",
value);
}
}
You need to import ChromeDriver class into your code.
DeleteHi Varun,
ReplyDeleteIam trying to chrome browser using webdriver in my windows
System.setProperty("webdriver.chrome.driver","C:\\workspace")
but some error displayed
that is
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://code.google.com/p/chromedriver/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.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:69)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:107)
at test12.test123.setUp(test123.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
can u pl z let me known where am getting wrong.
At what point you are setting the system property. Hopefully its before initialize the driver object.
DeleteIf you are doing the above try putting the complete path of the chromedriver to the system path.
setting the system property does't changes I given complete system path. I initialize the chrome driver but should not lunching to the chrome.
ReplyDeleteHi varun
ReplyDeletecan you tell me how to run our test on multiple browser instance parallel after setting up hub and node
This depends on which execution framework you are using junit2, junit4 or testng. You can refer to my post :
Deletehttp://blog.varunin.com/2011/10/selenium-grid-with-webdriver.html
Let me know if you have further issues.
Dear Mr.Varun,
ReplyDeleteI am able to open the browser and type into the fields but am not able to use the .click() command. I am suspecting a compatibility issue. Can you tell me which version of chrome and which version of chromedriver you are using.
Hi Varun,
ReplyDeleteThanks for the post. I am using your method 1 via giving the chromedrive exe path while starting the hub and node on remote machine and m using thos code for calling chrome
public void initialize(final Browser browser) {
LOGGER.debug("Browser choice is [{}]", browser.name());
final RunMode runMode = (RunMode)properties.get(DefaultProperties.RUN_MODE);
final String remoteSeleniumHubUrl = properties.getProperty(DefaultProperties.REMOTE_SELENIUM_HUB_URL);
if (RunMode.REMOTE.equals(runMode) && !StringUtils.isEmpty(remoteSeleniumHubUrl)) {
LOGGER.debug("Creating remote driver. Browser choice is [{}], Hub Url is [{}]", browser, remoteSeleniumHubUrl);
delegate = createRemoteDriver(browser, remoteSeleniumHubUrl);
} else if (RunMode.LOCAL.equals(runMode)) {
LOGGER.debug("Creating local driver. Browser choice is [{}]", browser);
delegate = createLocalDriver(browser);
} else {
LOGGER.debug("Creating local driver. Browser choice is [{}]", browser);
delegate = createLocalDriver(browser);
}
}
private ThreadLocal createRemoteDriver(final Browser browser, final String remoteSeleniumHubUrl) {
final ThreadLocal webDriver = new ThreadLocal();
final DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setJavascriptEnabled(true);
switch (browser) {
case CHROME:
desiredCapabilities.setBrowserName("chrome");
desiredCapabilities.setCapability("chrome.switches", Arrays.asList("--no-sandbox"));
break;
try {
final URL url = new URL(remoteSeleniumHubUrl);
webDriver.set(new RemoteWebDriver(url, desiredCapabilities));
} catch (final MalformedURLException e) {
LOGGER.error("", e);
}
return webDriver;
}
But still I am getting the same error
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://code.google.com/p/chromedriver/downloads/list
Can you please help me
can you paste the command line that you are using to start the server.
DeleteHi Varun,
DeleteI am using these comments for hub and nodes on remote machine -
java -DwebDriver.chrome.driver=c:/Users/tia/AppData/Local/Google/Chrome/Application/chromedriver.exe -jar selenium-server-standalone-2.31.0.jar -role hub
and for nodes
java -DwebDriver.chrome.driver=c:/Users/tia/AppData/Local/Google/Chrome/Application/chromedriver.exe -jar selenium-server-standalone-2.31.0.jar -role webDriver -hub http://localhost:4444
Change the path specified to :
Deletec:\\Users\\tia\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe
Hi Varun,
DeleteI did try this (c:\\Users\\tia\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe), but i am getting the same error.
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://code.google.com/p/chromedriver/downloads/list
Please let me whts wrong here.Thanks
Try putting it in the PATH variable and check. There may be some other problem also.
DeleteIts already there in the path variable (both ob my local and vm). locally its working fine but giving the above error when i am trying to run my test on remote vm
DeleteHi,
DeleteI am able to execute on vm, below is command is used.
java -Dwebdriver.chrome.driver=C:/chromedriver.exe -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=chrome
Hi Varun,
ReplyDeleteWhile running my test in Chrome I am getting below error. My Chrome is installed in the following path: C:\Program Files (x86)\Google\Chrome\
My code is
@Test
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
driver = new ChromeDriver();
/*Script*/
}
"org.openqa.selenium.WebDriverException: java.io.IOException: Cannot run program "C:\Users\jkgupta\Local Settings\Application Data\Google\Chrome\Application\chrome.exe": CreateProcess error=2, The system cannot find the file specified
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0'
Driver info: driver.version: chrome
at org.openqa.selenium.chrome.ChromeBinary.start(ChromeBinary.java:55)
at org.openqa.selenium.chrome.ChromeDriver.startClient(ChromeDriver.java:75)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:50)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:60)
at creatematter.Add_Matter.setUp(Add_Matter.java:33)
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:84)
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:127)
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:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
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.io.IOException: Cannot run program "C:\Users\jkgupta\Local Settings\Application Data\Google\Chrome\Application\chrome.exe": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at org.openqa.selenium.chrome.ChromeBinary.start(ChromeBinary.java:41)
... 28 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 30 more"
Pl. help me on this.
Hi jay,
DeleteTRY THIS. Give the actual path of chrome driver in the test
@Test
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/chromedriver.exe");