Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve Can inject only one of <ITestContext, XmlTest> into a BeforeTest annotated setBrowser error

Tags:

java

testng

I have @BeforeTest and @AfterTest Annotations used. Please find below code

WebDriverFactory wd =  new WebDriverFactory();

    //@Parameters({"browserName","appURL"})
    @BeforeTest
    public void setBrowser(String browserName, String appURL){
        wd.setDriver("chrome", "https:URL");
    }

    @AfterTest
    public void tearDown()
    {
        wd.closeBrowser();
    }

And getting below error

AILED CONFIGURATION: @BeforeTest setBrowser org.testng.TestNGException: Can inject only one of into a BeforeTest annotated setBrowser. 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)

like image 501
Ashu123 Avatar asked Sep 12 '25 10:09

Ashu123


1 Answers

The reason for the error is pretty straight forward:

Your @BeforeTest annotated method is accepting two String parameters, but you aren't injecting those parameters via the @Parameters annotation (I am seeing that in the code that you shared, you have commented it out).

TestNG is basically complaining that it doesn't know what to provide as values for those two String parameters, and TestNG can ONLY natively inject either a

  • ITestContext object for which your @BeforeTest annotated method signature would look like public void setBrowser(ITestContext context){ (or)
  • XmlTest object for which your @BeforeTest annotated method signature would look like public void setBrowser(XmlTest xmlTest){

So you would need to either uncomment your @Parameters annotation on top of the setBrowser() method, or change its signature to one of the above two variants.

like image 108
Krishnan Mahadevan Avatar answered Sep 14 '25 00:09

Krishnan Mahadevan