December 17, 2012

Run Webdriver script in Google Chrome

This post explains you how to run your webdriver script in google chrome..

Firefox Browser:
driver=new FirefoxDriver();  --it will work and will launch your firefox browser,

Google Chrome:

driver= new Chromedriver() --- it will throw an error.

Error:


FAILED CONFIGURATION: @BeforeClass beforeClass
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

To over come this we need to download the chrome driver.exe and we have to specify the apth of chrome driver in the script

Download path:
http://code.google.com/p/chromedriver/downloads/list

take the latest exe file. see the below image




save the chrome driver in a folder and you need to specify the path of driver in your webdriver script.

Below is the Sample code:


package testng;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ChromedriverTest {

public WebDriver driver;
  @BeforeClass
  public void beforeClass() {
// Create chrome driver instance...
 System.setProperty("webdriver.chrome.driver", "C:\\xxx\\Jar\\chromedriver.exe");
 driver=new ChromeDriver();
  }


  @Test
  public void testChromedriverTest() throws Exception {
 driver.navigate().to("http://bing.com");
 Thread.sleep(5000);
  }

  @AfterClass
  public void afterClass() {
 driver.quit();
  }

}


Use the above code your script will run in google chrome.





2 comments:

  1. Hi Admin,

    can you please help me.why below error is displaying.i am getting below error after downloading the chromedriver.exe and added chromedriver .exe path to the script.


    [TestNG] Running:
    C:\Users\rchennub.ST-IDC\AppData\Local\Temp\testng-eclipse-1376540339\testng-customsuite.xml

    FAILED CONFIGURATION: @BeforeClass beforeClass
    java.lang.NoClassDefFoundError: com/google/common/base/Function
    at testng.ChromedriverTest.beforeClass(ChromedriverTest.java:16)
    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:80)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    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: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 25 more

    SKIPPED CONFIGURATION: @AfterClass afterClass
    SKIPPED: testChromedriverTest

    ===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 1
    ===============================================


    ===============================================
    Default suite
    Total tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 1
    ===============================================

    [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@210a6ae2: 18 ms
    [TestNG] Time taken by org.testng.reporters.EmailableReporter@20c1f10e: 9 ms
    [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@288051: 113 ms
    [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 16 ms
    [TestNG] Time taken by org.testng.reporters.XMLReporter@32bf7190: 15 ms
    [TestNG] Time taken by org.testng.reporters.jq.Main@7290cb03: 39 ms

    ReplyDelete
  2. Please paste your code here...so that i will try to help you...

    ReplyDelete