javaxmlselenium-webdrivertestngsuite

Can a Parameterized method be called in a TestNG XML Suite?


I have a parameterized method(readConfig_1(String path)) in a class which I need to run before the tests in a TestNG Suite. Is there a way that I can call this method and define the parameter for the same in the TestNG.xml file?

Here's the Parameterized Method, I have written which actually needs a path to where the XML file is stored.

public static void readConfig_1(String configXmlPath) 
{

            browser = CoreLib.fGetNodeText(configXmlPath, "config",
                    "browser");
             env = CoreLib.fGetNodeText(configXmlPath, "config", "env");
             release = CoreLib.fGetNodeText(configXmlPath, "config", "release");
            serverName = CoreLib.fGetNodeText(configXmlPath, env,
                    "serverName");
             host = CoreLib.fGetNodeText(configXmlPath, env, "host");
            userName = CoreLib.fGetNodeText(configXmlPath, env, "userName");
            passWord = CoreLib.fGetNodeText(configXmlPath, env, "passWord");
            portNumber = CoreLib.fGetNodeText(configXmlPath, env,
                    "portNumber");
            schema = CoreLib.fGetNodeText(configXmlPath, env, "schema");
            url = CoreLib.fGetNodeText(configXmlPath, env, "url");
            screenShotForPass=CoreLib.fGetNodeText(configXmlPath, env, "SCreenShotForPass");

            screenShotForFail=CoreLib.fGetNodeText(configXmlPath, env, "SCreenShotForFail");


            CoreLib.LOGGER.info("****************************************************");
            CoreLib.LOGGER.info("           Configuration Details                    ");
            CoreLib.LOGGER.info("****************************************************");

            CoreLib.LOGGER.info("Browser ::" + browser);
            CoreLib.LOGGER.info("env ::" + env);
            CoreLib.LOGGER.info("serverName ::" + serverName);
            CoreLib.LOGGER.info("host ::" + host);
            CoreLib.LOGGER.info("userName ::" + userName);
            CoreLib.LOGGER.info("passWord ::" + passWord);
            CoreLib.LOGGER.info("portNumber ::" + portNumber);
            CoreLib.LOGGER.info("schema ::" + schema);
            CoreLib.LOGGER.info("url::" + url);
            CoreLib.LOGGER.info("ScreenSnapShotForPass::"+screenShotForPass );
            CoreLib.LOGGER.info("ScreenSnapShotForFail::"+screenShotForFail );

        }

In this TestNG Suite seen below, I need to call the above method passing a parameter to it before it can go ahead and run the tests written in the other classes.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Smoke Suite" parallel="false" preserve-order="true">

    <listeners>
        <listener class-name="com.healthcare.reports.MyListener"></listener>
    </listeners>



    <test name="XYZ Tests">
        <classes>

            <class name="com.healthcare.utility.Config">
                <methods>
                    <include name="readConfig_1"></include>
                </methods>
            </class>

            <class name="com.healthcare.businessLib.xyz.AddUserTests" />

        </classes>

    </test>


</suite> 

By this I intend to restrict a TestNG Suite to read a particular Config.XML file which will have it's own values such as Env, URL, browser etc., set before the tests can be executed. Is there a way I can achieve this?


----Added 11/24/2017-----

---- I thought adding the readConfig_1 to a @BeforeClass annotation would resolve the problem. But there's more to it-----

My Listener Class has **@onStart** annotation which needs the config file to be run on the start of the Suite. As you see below my Listener Class has the variables release_1 coming from Config file.

 public class MyListener implements ITestListener, ISuiteListener {

        // This belongs to ISuiteListener and will execute before the Suite start

        ReportLib report=new ReportLib();

        @Override
        public void onStart(ISuite arg0) {

            Config.readConfig_1(configXlsPath);
            ExportTestResults export = new ExportTestResults();
            export.exportExcelHeader(Config.release_1);
            CoreLib.fCreateLogger(Config.release_1);

        }

But if I put it in @BeforeClass in a TestClass these variables(Config.release_1) are returning null as they would be running before the test class. So I need the readconfig_1 to run before or with the Listener class and unable to add a parameter to the onStart(ISuite arg0).

Tried a few things by :

  1. Running the readConfig_1() in the TestNG.XML as the first method even before the listener class could be called.

  2. putting a @BeforeClass annotation in the Listener class with readConfig_1() method parameterized in it- hoping that the readConfig would be called before the onStart() is executed.

    public class MyListener2 implements ITestListener, ISuiteListener {
    
        ReportLib report=new ReportLib();
    
        @BeforeClass
        @Parameters("configXlsPath")
        public void beforeSuite(String configXlsPath)
        {
            Config.readConfig_1(configXlsPath);
            System.out.println("In Before Class(Listener_2)"+ Config.release_1);
        }
    
    
        @Override
        public void onStart(ISuite arg0) {
    
            ExportTestResults export = new ExportTestResults();
            System.out.println("In onStart(Listener_2)"+ Config.release_1);
            export.exportExcelHeader(Config.release_1);
            CoreLib.fCreateLogger(Config.release_1);
    
        }
    

    }

But none worked. Is there a way around this now?

Thanks in advance.


Solution

  • Yes, you can do this. Firstly, add @Parameters("configXmlPath") annotation to your test class. configXmlPath parameter must also be defined in testng.xml file like <parameter name = "configXmlPath" value="Whateverthevalueis"/> this. Here is an example.

    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;
    
    public class YourTest {
       @Test
       @Parameters("configXmlPath")
       public void test1(String configXmlPath) {
          System.out.println("Parameterized value is : " + configXmlPath);
       }
    }
    

    Then, in your testng.xml define parameter like this:

    <?xml version = "1.0" encoding = "UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name = "Suite1">
       <test name = "test1">
    
          <parameter name = "configXmlPath" value="Whateverthevalueis"/> 
    
         <classes>
             <class name = "YourTest" />
          </classes>
    
        </test>
    </suite>