How to resolve "Native Injection is NOT supported for @BeforeSuite annotated launchbrowser"

311 Views Asked by At

I facing error to launch the chrome driver with selenium, TestNG framework. Tried multiple method but it fails the testcase and skips it all the time . It is my first project so i am clueless.The Error log

[RemoteTestNG] detected TestNG version 7.9.0
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 8 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 14 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 1 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 1 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 1 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.collectMethodsByGroup() took 1 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] MethodGroupsHelper.sortMethods() took 0 ms.
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Running:
  C:\Users\prabh\AppData\Local\Temp\testng-eclipse-705038558\testng-customsuite.xml

Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [Utils] DynamicGraphHelper.createDynamicGraph() took 5 ms.
FAILED CONFIGURATION: @BeforeSuite commonfunctionalities.commonfunction.launchbrowser
org.testng.TestNGException: 
Native Injection is NOT supported for @BeforeSuite annotated launchbrowser.
For more information on native dependency injection please refer to https://testng.org/doc/documentation-main.html#native-dependency-injection
    at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:445)
    at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:350)
    at org.testng.internal.Parameters.createParameters(Parameters.java:702)
    at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:166)
    at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:306)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:382)
    at org.testng.SuiteRunner.run(SuiteRunner.java:336)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1228)
    at org.testng.TestNG.runSuites(TestNG.java:1134)
    at org.testng.TestNG.run(TestNG.java:1101)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

SKIPPED: testcases.pending_leave_request.verifypendingleaverequest
org.testng.TestNGException: 
Native Injection is NOT supported for @BeforeSuite annotated launchbrowser.
For more information on native dependency injection please refer to https://testng.org/doc/documentation-main.html#native-dependency-injection
    at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:445)
    at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:350)
    at org.testng.internal.Parameters.createParameters(Parameters.java:702)
    at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:166)
    at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:306)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:382)
    at org.testng.SuiteRunner.run(SuiteRunner.java:336)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1228)
    at org.testng.TestNG.runSuites(TestNG.java:1134)
    at org.testng.TestNG.run(TestNG.java:1101)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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


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

Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.jq.Main@6d07a63d: 48 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.FailedReporter@59cba5a: 12 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.XMLReporter@45a37759: 11 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.EmailableReporter2@31add175: 7 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@457c9034: 5 ms
Feb 14, 2024 3:38:12 PM org.testng.log4testng.Logger info
INFO: [TestNG] Time taken by org.testng.internal.ExitCodeListener@345f69f3: 1 ms

Here is the common function program Selenium version: 4.11.0 TestNG version: 7.9.0 Eclipse version:: 2022-06 (4.24.0)

package commonfunctionalities;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class commonfunction {
    public static WebDriver driver=null;
    public static Properties properties = null;
    public void loadpropertyfile() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\prabh\\IdeaProjects\\OpenHrmProjects\\extras\\config.properties");
        properties = new Properties();
        properties.load(fileInputStream);
    }
    @SuppressWarnings("deprecation")
    @BeforeSuite
    public void launchbrowser(String driverlocation) throws IOException {
        loadpropertyfile();
        String browser = properties.getProperty("browser");
        String url = properties.getProperty("url");
        String Driverlocation = properties.getProperty("Driverlocation");

        if (browser.equalsIgnoreCase("firefox")) {
            System.setProperty("webdriver.gecko.driver",Driverlocation);
            driver = new FirefoxDriver();
        } else if (browser.equalsIgnoreCase("chrome")) {
            System.setProperty("webdriver.chrome.driver",Driverlocation);
            driver = new ChromeDriver();}
        driver.manage().window().maximize();
        driver.get(url);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterSuite
    public void teardown() {
         driver.quit();
    }
}
1

There are 1 best solutions below

0
Krishnan Mahadevan On

This is the root cause of the problem in your code

@SuppressWarnings("deprecation")
@BeforeSuite
public void launchbrowser(String driverlocation) throws IOException { }

The method launchbrowser() has a parameter.

TestNG does not know how to pass a value for the parameter driverlocation when it tries to invoke the @BeforeSuite annotated launchbrowser()

You should remove this method parameter.

If you are expecting to retrieve the value of driverlocation from your suite file, then you can do something like below:

import org.testng.ISuite;
import org.testng.Reporter;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.util.Optional;

public class SampleTestCase {

    @BeforeSuite
    @Parameters({"driver"})
    public void beforeSuiteVariant1() {
        ISuite suite = Reporter.getCurrentTestResult().getTestContext().getSuite();
        Optional.ofNullable(suite.getParameter("driver"))
                .ifPresent(it ->
                        System.err.println("Driver retrieved programmatically: " + it));
    }

    @BeforeSuite
    @Parameters({"driver"})
    public void beforeSuiteVariant2(String driver) {
        System.err.println("Driver retrieved via mandatory parameter in suite file: " + driver);
    }

    @BeforeSuite
    @Parameters({"driver"})
    public void beforeSuiteVariant3(@org.testng.annotations.Optional String driver) {
        System.err.println("Driver retrieved via optional parameter in suite file: " + driver);
    }

    @Test
    public void testMethod() {}
}

Now when you run this above test class from within the IDE (without using a suite file) you will notice that TestNG will error out on beforeSuiteVariant2 because this method is expecting a parameter to be mandatorily injected into it from a suite file. When you run a test from within the IDE, the TestNG IDE plugin (both IntelliJ and Eclipse for that matter) will create a suite file dynamically which will not contain the driver parameterand thusbeforeSuiteVariant2` will fail.

But if you create a suite file that looks like below:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="my_suite" verbose="2">
    <parameter name="driver" value="chromedriver"/>
    <test name="my_test">
        <classes>
            <class name="com.rationaleemotions.so.qn77995590.SampleTestCase"/>
        </classes>
    </test>
</suite>

You will see an output that looks like below:

...
... TestNG 7.9.0 by Cédric Beust ([email protected])
...

Driver retrieved programmatically: chromedriver
Driver retrieved via mandatory parameter in suite file: chromedriver
Driver retrieved via optional parameter in suite file: chromedriver
PASSED: com.rationaleemotions.so.qn77995590.SampleTestCase.testMethod

===============================================
    my_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

Points to remember:

  • @BeforeSuite annotated methods can have a parameter, provided you use the @Parameters annotation from TestNG. This way TestNG knows that it should read the suite file's <parameters> tag at the <suite> level ( NOT within the <test> level ) and use that value to pass in as parameter to the method.
  • Native injection refers to TestNG figuring out what to pass as method parameters on it's own without you telling it how to get those values. Please refer to the official documentation here for more details on what can and what cannot be passed.
  • If you would like to define a default behaviour to your @BeforeSuite method's parameters i.e., you want to tell TestNG, please use the parameter value from the suite file if it's present. If it's not present, please don't complain to me, but instead use a default value, then you should one of the variants as shown in beforeSuiteVariant1() (or) beforeSuiteVariant3()

I think you no longer need to be managing selenium webdriver locations. If you are on the latest selenium 4.x series (I think the latest released version as of today is 4.17.0) then the Selenium Manager automatically does this for you. You just need to ensure that you do the following:

  • Search all the locations that are part of the PATH variable and ensure that you delete all the chromedriver|edgedriver|geckodriver binaries from these locations.
  • Scan through you codebase and get rid of all code snippets that does stuff like this System.setProperty("webdriver.gecko.driver",Driverlocation);