1

My Code :

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

 public class NewTest {

public WebDriver driver;
@Test
public void f(WebDriver driver) {


  System.setProperty("webdriver.chrome.driver", "C:\\Users\\Cheitrav\\Desktop\\ISTQB\\chromedriver.exe"); 
  driver = new ChromeDriver();

  driver.get("http://www.guru99.com");


}
}



[TestNG] Running:
  C:\Users\Cheitrav\AppData\Local\Temp\testng-eclipse--373686985\testng-customsuite.xml

[Error] org.testng.TestNGException: 
Cannot inject @Test annotated Method [f] with [interface org.openqa.selenium.WebDriver].
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection
    at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:244)
    at org.testng.internal.Parameters.createParameters(Parameters.java:172)
    at org.testng.internal.Parameters.createParameters(Parameters.java:458)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:568)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1293)
    at org.testng.internal.Invoker.createParameters(Invoker.java:1020)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1110)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:756)
    at org.testng.TestRunner.run(TestRunner.java:610)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    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)

FAILED: f
org.testng.TestNGException: 
Cannot inject @Test annotated Method [f] with [interface org.openqa.selenium.WebDriver].
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection
    at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:244)
    at org.testng.internal.Parameters.createParameters(Parameters.java:172)
    at org.testng.internal.Parameters.createParameters(Parameters.java:458)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:568)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1293)
    at org.testng.internal.Invoker.createParameters(Invoker.java:1020)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1110)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:756)
    at org.testng.TestRunner.run(TestRunner.java:610)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    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)


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


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

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@5f2108b5: 16 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@72d818d1: 4 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@cb644e: 4 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 3 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@5649fd9b: 4 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@4b4523f8: 24 ms
2

Methods annotated with @Test cannot have parameters. Try something like this:

public class NewTest {
    @Test
    public void f() {
       WebDriver driver;
       System.setProperty("webdriver.chrome.driver", "C:\\Users\\Cheitrav\\Desktop\\ISTQB\\chromedriver.exe"); 
       driver = new ChromeDriver();
       driver.get("http://www.guru99.com");
    }
}

There are problems with your code but my suggestion above should get you past the problem you asked about.

Edit: this answer was based on outdated information and is wrong.

2

Unlike what @user246 said, it is possible to have parameters in @Test methods.

2 options exist:

Any @Before method or @Test method can declare a parameter of type ITestContext.

Any @AfterMethod method can declare a parameter of type ITestResult, which will reflect the result of the test method that was just run.

Any @Before and @After methods can declare a parameter of type XmlTest, which contain the current tag.

Any @BeforeMethod (and @AfterMethod) can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called once this @BeforeMethod finishes (or after the method as run for @AfterMethod).

Any @BeforeMethod can declare a parameter of type Object[]. This parameter will receive the list of parameters that are about to be fed to the upcoming test method, which could be either injected by TestNG, such as java.lang.reflect.Method or come from a @DataProvider.

Any @DataProvider can declare a parameter of type ITestContext or java.lang.reflect.Method. The latter parameter will receive the test method that is about to be invoked.

  • Can you complete your answer with an example of how to fix the Op's problem? – user246 Feb 12 '17 at 18:33
  • No, I can't. The objective behind the question is not clear enough and there are too many ways to do it. The accepted question is a good way to solve the issue and I've just pointed the wrong statement about parameters. – juherr Feb 13 '17 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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