user1817396
user1817396

Reputation:

How to get arguments from main method to test class

I'm working on Selenium project. I'm running TestNG programmatically because I need main method because I'm creating .jar file via maven-shade plugin containing all of dependencies.

public class EDITestApp {   
public static void main(String[] args) {        
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    Class[] classes = new Class[]{
            LoginTest.class,
            ContractorsTest.class,
            ActiveContractorsTest.class,
            InvitedContractorsTest.class,
            CancelledContractorsTest.class,
            MainPageTest.class
    };
    testng.setTestClasses(classes);
    testng.addListener(tla);
    testng.run();
}

I want to make my .jar file with all my tests to run with command line arguments containing, eg, argument that sets browser which will run tests. Is it possible to get this argument from my test classes which creating instance of driver with specific browser or I need to create dynamically testng.xml in main method and set properties in .xml file?

Upvotes: 0

Views: 1757

Answers (1)

Akbar
Akbar

Reputation: 1525

You can pass the cmd arguments as parameters your tests. I checked the TestNG class api and it does not provide you such a feature. But there is one class which does - XmlSuite(). You add all the classes to XmlSuite and then add the suite to TestNG using setXmlSuites(). XmlSuite allows parameters to be defined using setParameters().

Sample code-

XmlSuite suite = new XmlSuite();
suite.setParameters(suiteParameters - you can use a map(key,value));
classes = new ArrayList<XmlClass>();
classes.add(new XmlClass(fullClassName));
--here add all the required test class to classes object.
test.setXmlClasses(classes);
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();

Let me know if you need more help on this.

Upvotes: 1

Related Questions