0

I am getting following error when run my selenium webdriver program using testNG (Using Eclipse Kepler service release 2)

Cannot instantiate class TestNG.TestNGClass
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:373)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:285)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
    at org.testng.TestClass.getInstances(TestClass.java:104)
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:90)
    at org.testng.TestClass.init(TestClass.java:82)
    at org.testng.TestClass.<init>(TestClass.java:45)
    at org.testng.TestRunner.initMethods(TestRunner.java:422)
    at org.testng.TestRunner.init(TestRunner.java:252)
    at org.testng.TestRunner.init(TestRunner.java:222)
    at org.testng.TestRunner.<init>(TestRunner.java:171)
    at org.testng.remote.support.RemoteTestNG6_9_10$1.newTestRunner(RemoteTestNG6_9_10.java:28)
    at org.testng.remote.support.RemoteTestNG6_9_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_9_10.java:61)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:604)
    at org.testng.SuiteRunner.init(SuiteRunner.java:170)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:117)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1359)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1346)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
    at org.testng.TestNG.runSuites(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1096)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 25 more
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
    at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:37)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:95)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:277)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:247)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:242)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:238)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:127)
    at TestNG.TestNGClass.<init>(TestNGClass.java:15)
    ... 30 more

Here is the Java program

package TestNG;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNGClass
{

    WebDriver driver = new FirefoxDriver();

   @BeforeTest
   public void launchapp()
   {
       System.out.println("Before test");
       // Puts an Implicit wait, Will wait for 10 seconds before throwing exception

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      // Launch website
      driver.navigate().to("http://www.calculator.net");
      driver.manage().window().maximize();
   }
   @Test
   public void calculatepercent()
   {
       System.out.println("Starting calculator percent");
       // Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();

      // Click on Percent Calculators
      driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();

      // Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");

      // Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");

      // Click Calculate Button
      driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();

      // Get the Result Text based on its xpath
      String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();

      // Print a Log In message to the screen
      System.out.println(" The Result is " + result);

      if(result.equals("5"))
      {
         System.out.println(" The Result is Pass");
      }
      else
      {
         System.out.println(" The Result is Fail");
      }
   }
   @AfterTest
   public void terminatetest()
   {
      driver.close();
   }
}

And here is the XML file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="TestNG.TestNGClass"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
5
  • Here is the XML file
    – Wayne
    Commented Oct 26, 2016 at 12:02
  • <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <class name="TestNG.TestNGClass"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
    – Wayne
    Commented Oct 26, 2016 at 12:02
  • 1
    The "Caused by" line is probably your issue: your firefox driver executable isn't defined. That probably means you either don't have it in your PATH variables, or you have to explicitly set it - use the link in the exception stack (github.com/mozilla/geckodriver) and see if there's more information there
    – Kate Paulk
    Commented Oct 26, 2016 at 12:35
  • Have you tried Kate's solution? Commented Oct 27, 2016 at 10:22
  • 1
    The title is misleading, look at the root cause of your stacktrace: "Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see github.com/mozilla/geckodriver. The latest version can be downloaded from github.com/mozilla/geckodriver/releases"
    – dzieciou
    Commented Oct 27, 2016 at 12:42

2 Answers 2

1

It can't find the driver for Firefox. Downloading the GeckoDriver and putting it into your Bin folder or setting a System Property for webdriver.gecko.driver should fix the problem.

-1

Exception: "java.lang.IllegalStateException"

  • This exception is especially useful when your class has some state (state machine?) that changes over time, making some methods irrelevant

  • This is generally the exception to throw if the invocation is illegal because of the state of the receiving object. For example, this would be the exception to throw if the caller attempted to use some object before it had been properly initialized.

1
  • I highly doubt if definition of IllegalStateException really answers the question
    – olyv
    Commented Nov 27, 2016 at 15:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.