0

I am automating a web page which runs in multi-threading environment, so I am exporting every test result into a file system and I wanted to maintain every test result uniquely for the future reference. So is there a way to pass file name as parameter to a test method dynamically while calling it from TestNG class.

I know we can pass parameters from .xml file but if I do that the values will more like static and can be seen by all the thread running parallel.

Test class will be called from main method as bellow

public class Test  {

    public static void main(String[] args) throws ParseException {

        try
        {
            TestNG testng = new TestNG();
            testng.setTestClasses(new Class[] { Testing.class });
            testng.run(); 
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}

Bellow code is my test method

public class Testing {

@Test
@Parameters("filename")
public void testMethod(String fileName){

    System.out.println("filename is: "+fileName);

   // ---- remaining test logic -----
}

}

Or can we use TestListenerAdapter onStart() method to inject parameter values...?.

Rajesh Hatwar
  • 1,843
  • 6
  • 39
  • 58

3 Answers3

1

If you want unique file name you can just add it a time stamp

Date date = new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String timeStamp = formatter.format(date);

String fileName = "TestResults-" + timeStamp;
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Yes I could have done, but even running test cases are scheduled and automated! :-). After execution of test cases I should have a track of generated reports with respect to the scheduled time. If I depend on time stamp it cannot be a error free code. If possible I want to create a listener which feed the required data at the time of test execution. – Rajesh Hatwar Jul 05 '17 at 06:01
1

You can store your values into ITestContext which will be available for all tests.

You can set up the values in a configuration method (@BeforeSuite for example) or a listener.

juherr
  • 5,640
  • 1
  • 21
  • 63
  • yes you are correct, I was trying the same way useing `TestListenerAdapter` listener but it was not happening. If possible can you please post some example..! – Rajesh Hatwar Jul 05 '17 at 04:24
0

Pass Dynamic Parameters to TestNG suite during runtime

What the below code does: I want to add a list of parameters to each test during runtime. These parameters are passed as maven runtime arguments. They are read using System.getProperty() method as shown below. Then these parameters are added to the test inside suite and testng is ran successfully. This can be really useful in other scenarios as well.

The below code reads the testng.xml file and adds parameter to

List<String> parameters = new ArrayList<>();
parameters = Arrays.asList(System.getProperty("parameters").split(",");

TestNG tng = new TestNG();
File initialFile = new File("testng.xml");
InputStream inputStream = FileUtils.openInputStream(initialFile);
Parser p = new Parser(inputStream);
List<XmlSuite> suites = p.parseToList();
for(XmlSuite suite:suites){
    List<XmlTest> tests = suite.getTests();
    for (XmlTest test : tests) {
         for (int i = 0; i < parameters.size(); i++) {
            HashMap<String, String> parametersMap = new HashMap<>();
            parametersMap.put("parameter",parameters.get(i));
            test.setParameters(parametersMap);
        }
    }
}
tng.setXmlSuites(suites);
tng.run();
Kishore Mohanavelu
  • 439
  • 1
  • 6
  • 17