1

I want to print the Parameters passed to this method using Listners.

@Test
@Parameters({"browsername","URL", "ebrowse","eURL"})
public static void LaunchApplication(String browsername, String URL, String ebrowse, String eURL ) throws InterruptedException, IOException{

I have used the following code:

public void onTestSuccess(ITestResult result) {
System.out.println("\nThe method is in " +result.getTestClass().getName());
        String paramet = null;
        for(Object parameter : result.getParameters()){ 
        paramet += parameter.toString() +",";
        }
        System.out.println("The parameters of the method are: " +paramet);
}

Output:

The parameters of the method are: nullch32,http://localhost:90/fintech/login.html,ch32,http://localhost:90/fintech/login.html,

The output I get is the values that are passed to the parameter. Also it prints the null that I have used to initialize the String. How do I eliminate the null and print the parameters only and not values?

Learner_me
  • 29
  • 7

2 Answers2

0

To get rid of the null, just initialize the string with "" (empty string).

String paramet = "";

I don't think there's a way to print the parameter names... only the values since that's what TestNG provides. I'm not sure why you would want to do that anyway because the parameter names don't change, only the values.

JeffC
  • 22,180
  • 5
  • 32
  • 55
0

You are trying to get only parameter values, which what @Parameters annotation generates. What you actually need is to access the annotation itself using reflection and extract the configuration properties passed to it.

To get the parameter names, you should have something similar to the following code:

String[] parameters = testResult.getMethod().getMethod().getAnnotation(Parameters.class).value();
System.out.println("Parameter 1: " + parameters[0]); //will print browsername
System.out.println("Parameter 2: " + parameters[1]); //will print URL
System.out.println("Parameter 3: " + parameters[2]); //will print ebrowse
System.out.println("Parameter 4: " + parameters[3]); //will print eURL
Kristijan Rusu
  • 567
  • 4
  • 14