Class TestNGParameterListener

  • All Implemented Interfaces:
    ConfNGListener, org.testng.IInvokedMethodListener, org.testng.ISuiteListener, org.testng.ITestListener, org.testng.ITestNGListener

    public class TestNGParameterListener
    extends Object
    implements org.testng.ITestListener, org.testng.IInvokedMethodListener, org.testng.ISuiteListener, org.testng.ITestNGListener, ConfNGListener
    TestNG listener that automatically captures test parameters and makes them available to ConfNG as a configuration source.

    This listener is automatically loaded via TestNG's service loader mechanism when ConfNG is on the classpath. It captures parameters from:

    • Suite parameters (from testng.xml)
    • Test parameters (from testng.xml)
    • Method parameters (from @Parameters annotation)

    Parameters are made available with the following precedence:

    1. Method-level parameters (highest priority)
    2. Test-level parameters
    3. Suite-level parameters (lowest priority)

    This listener implements ConfNGListener with priority 0, allowing custom listeners to execute before (negative priority) or after (positive priority) the configuration loading process.

    Automatic Configuration Loading

    When the test suite starts, this listener automatically loads configuration files in the following order (first loaded = highest precedence):

    1. Config Files - Auto-loads standard config files (HIGHEST PRECEDENCE):
      • config.properties, config.json, config.yaml, config.toml
      • File type is auto-detected based on extension
      • Files that don't exist are silently skipped
    2. Environment-Specific Configuration - Loads configuration files specific to the current environment (MEDIUM PRECEDENCE):
      • If a suite parameter named "environment" or "env" is present in testng.xml, loads {environment}.properties, {environment}.json, {environment}.yaml, {environment}.toml
      • If no environment parameter is found, attempts auto-detection by checking the APP_ENV, ENVIRONMENT, or ENV environment variables
      • If no environment is detected, defaults to "local"
    3. Global/Common Configuration - Loads configuration files that apply to all environments (LOWEST PRECEDENCE, provides defaults):
      • global.properties, global.json, global.yaml, global.toml
      • common.properties, common.json, common.yaml, common.toml

    This loading order ensures that config.* files have the highest precedence, followed by environment-specific values, and finally global values as defaults. This allows you to define common defaults in global configuration files, override them per environment, and have project-specific overrides in config.* files.

    Example Configuration Structure

    config.json (highest precedence):

     {
       "browser": "chrome",
       "headless": "true"
     }
     

    uat.json (medium precedence):

     {
       "api.url": "https://uat-api.example.com",
       "api.timeout": "60",
       "database.host": "uat-db.example.com"
     }
     

    global.json (lowest precedence, provides defaults):

     {
       "api.timeout": "30",
       "retry.count": "3",
       "log.level": "INFO"
     }
     

    Result for UAT environment: browser=chrome (from config.json), headless=true (from config.json), api.timeout=60 (from uat.json, overrides global), retry.count=3 (from global.json), log.level=INFO (from global.json), api.url and database.host from uat.json

    Example testng.xml

     <suite name="My Test Suite">
       <parameter name="environment" value="uat"/>
       <test name="My Test">
         <classes>
           <class name="com.example.MyTest"/>
         </classes>
       </test>
     </suite>
     
    Since:
    1.0
    Author:
    Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
    See Also:
    TestNGParameterSource, ConfNG.loadGlobalConfig(), ConfNG.loadConfigForEnvironment(String), ConfNG.autoLoadConfig()
    • Constructor Detail

      • TestNGParameterListener

        public TestNGParameterListener()
    • Method Detail

      • getPriority

        public int getPriority()
        Returns the priority of this listener in the listener chain. Priority 0 is the default for TestNGParameterListener, allowing custom listeners to execute before (negative numbers) or after (positive numbers).
        Specified by:
        getPriority in interface ConfNGListener
        Returns:
        the priority value (can be negative, zero, or positive)
      • onStart

        public void onStart​(org.testng.ISuite suite)
        Called when a suite starts. Captures suite-level parameters and automatically loads configuration files with auto-detection of file types.

        Auto-loads the following configuration files (if they exist):

        1. config.properties, config.json, config.yaml, config.toml - HIGHEST PRECEDENCE
        2. Environment-specific configuration files ({environment}.properties, {environment}.json, etc.)
        3. Global/common configuration files (global.properties, global.json, etc.) - LOWER PRECEDENCE (provides defaults)

        This ensures that config.* files have the highest precedence, followed by environment-specific values, and finally global values as defaults.

        Specified by:
        onStart in interface org.testng.ISuiteListener
      • onSuiteStart

        public void onSuiteStart​(org.testng.ISuite suite)
        Implementation of ConfNGListener.onSuiteStart - this is called by the listener chain. This method contains the actual configuration loading logic.
        Specified by:
        onSuiteStart in interface ConfNGListener
        Parameters:
        suite - the test suite
      • onTestStart

        public void onTestStart​(org.testng.ITestResult result)
        Called when a test starts. Captures test-level parameters. This method satisfies both ITestListener.onTestStart and ConfNGListener.onTestStart.

        When called by TestNG, delegates to the listener chain. When called by the chain, executes the actual parameter capture logic.

        Specified by:
        onTestStart in interface ConfNGListener
        Specified by:
        onTestStart in interface org.testng.ITestListener
        Parameters:
        result - the test result
      • beforeInvocation

        public void beforeInvocation​(org.testng.IInvokedMethod method,
                                     org.testng.ITestResult testResult)
        Called before a method is invoked. Captures method-level parameters. This method satisfies both IInvokedMethodListener and ConfNGListener.
        Specified by:
        beforeInvocation in interface ConfNGListener
        Specified by:
        beforeInvocation in interface org.testng.IInvokedMethodListener
        Parameters:
        method - the invoked method
        testResult - the test result
      • afterInvocation

        public void afterInvocation​(org.testng.IInvokedMethod method,
                                    org.testng.ITestResult testResult)
        Called after a method is invoked. Cleanup method-level parameters. This method satisfies both IInvokedMethodListener and ConfNGListener.
        Specified by:
        afterInvocation in interface ConfNGListener
        Specified by:
        afterInvocation in interface org.testng.IInvokedMethodListener
        Parameters:
        method - the invoked method
        testResult - the test result
      • onTestSuccess

        public void onTestSuccess​(org.testng.ITestResult result)
        Called when a test succeeds. Cleanup test-level parameters. This method satisfies both ITestListener.onTestSuccess and ConfNGListener.onTestSuccess.
        Specified by:
        onTestSuccess in interface ConfNGListener
        Specified by:
        onTestSuccess in interface org.testng.ITestListener
        Parameters:
        result - the test result
      • onTestFailure

        public void onTestFailure​(org.testng.ITestResult result)
        Called when a test fails. Cleanup test-level parameters. This method satisfies both ITestListener.onTestFailure and ConfNGListener.onTestFailure.
        Specified by:
        onTestFailure in interface ConfNGListener
        Specified by:
        onTestFailure in interface org.testng.ITestListener
        Parameters:
        result - the test result
      • onTestSkipped

        public void onTestSkipped​(org.testng.ITestResult result)
        Called when a test is skipped. Cleanup test-level parameters. This method satisfies both ITestListener.onTestSkipped and ConfNGListener.onTestSkipped.
        Specified by:
        onTestSkipped in interface ConfNGListener
        Specified by:
        onTestSkipped in interface org.testng.ITestListener
        Parameters:
        result - the test result
      • onTestFailedButWithinSuccessPercentage

        public void onTestFailedButWithinSuccessPercentage​(org.testng.ITestResult result)
        Called when a test fails but is within the success percentage. This method satisfies both ITestListener and ConfNGListener.
        Specified by:
        onTestFailedButWithinSuccessPercentage in interface ConfNGListener
        Specified by:
        onTestFailedButWithinSuccessPercentage in interface org.testng.ITestListener
        Parameters:
        result - the test result
      • onTestFailedWithTimeout

        public void onTestFailedWithTimeout​(org.testng.ITestResult result)
        Called when a test fails with timeout. This method satisfies both ITestListener and ConfNGListener.
        Specified by:
        onTestFailedWithTimeout in interface ConfNGListener
        Specified by:
        onTestFailedWithTimeout in interface org.testng.ITestListener
        Parameters:
        result - the test result
      • onStart

        public void onStart​(org.testng.ITestContext context)
        Called when a test context starts (ITestListener). Delegates to the listener chain.
        Specified by:
        onStart in interface ConfNGListener
        Specified by:
        onStart in interface org.testng.ITestListener
        Parameters:
        context - the test context
      • onFinish

        public void onFinish​(org.testng.ITestContext context)
        Called when a test context finishes (ITestListener). Note: This is different from onFinish(ISuite) which is for ISuiteListener. Delegates to the listener chain.
        Specified by:
        onFinish in interface ConfNGListener
        Specified by:
        onFinish in interface org.testng.ITestListener
        Parameters:
        context - the test context
      • onFinish

        public void onFinish​(org.testng.ISuite suite)
        Called when a suite finishes (TestNG ISuiteListener method). Delegates to the listener chain which will call onSuiteFinish on all listeners.
        Specified by:
        onFinish in interface org.testng.ISuiteListener
      • onSuiteFinish

        public void onSuiteFinish​(org.testng.ISuite suite)
        Implementation of ConfNGListener.onSuiteFinish - called by the listener chain. Cleanup suite-level parameters.
        Specified by:
        onSuiteFinish in interface ConfNGListener
        Parameters:
        suite - the test suite
      • getParameterSource

        public static TestNGParameterSource getParameterSource()
        Gets the current TestNG parameter source. This method is primarily for testing purposes.
        Returns:
        the current parameter source, or null if not initialized