TestNG Tutorials 44: Constructor with @Parameter Annotation

Hello Folks,

We have seen previously that how can we parameterize methods in a TestNG class.

The approach of using @Parameters annotations with each methods is not so good. Some demerits of this approach are as below:

  1. You need to include @Parameters annotation with each parameterized method.
  2. You need to accept as many as attributes in method signature passed in @Parameters annotation.
  3. If same parameter is used by multiple methods, you need to explicitly write that parameter name with @Parameters annotation for all methods consuming same parameter and in method signature.
  4. If you need to remove or add new parameters from methods, you need to edit both @Parameters annotation for desired methods and their method signature.

 

Can’t we do something so that all parameters will be initialized at once with values provided at run time which will be used by methods instead of individual Parameter annotation for each method ? 

Yes, we can. We can achieve that using Constructor concept of Java. 

We needs to create a constructor in TestNG class and use @Parameter annotation on that. Initialize all parameters in constructor. See an example below:

[java]
package Parameters;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParametersExampleAtClonstructor {

// List of parameters
String p1;
String p2;
String p3;
String p4;

// Accept all parameters in Constructor from testng.xml
@Parameters({"confBeforeParameter","testParameters1","testParameters2","confAfterParameter"})
public ParametersExampleAtClonstructor(String s1, String s2, String s3, String s4)
{
// Initialize all parameters
System.out.println("in constructor");
p1=s1;
p2=s2;
p3=s3;
p4=s4;
}

// Use whatever parameters are needed in methods
// Note here I am not using any parameter in method signature and Parameters annotation
@BeforeMethod
public void configurationBeforeMethod()
{
System.out.println("Paramters for before configuration method: "+p1);
}

@Test
public void testMethod()
{
System.out.println("Paramters one for test method: "+p2);
System.out.println("Paramters two for test method: "+p3);
}

@AfterMethod
public void configurationAfterMethod()
{
System.out.println("Paramters for after configuration method: "+p4);
}
}

[/java]

TestNG xml:

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<parameter name="confBeforeParameter" value="confBeforeParameter" />
<parameter name="testParameters1" value="testParameters1" />
<parameter name="testParameters2" value="testParameters2" />
<parameter name="confAfterParameter" value="confAfterParameter" />
<classes>
<class name="Parameters.ParametersExampleAtClonstructor" />
</classes>
</test> <!– Test –>
</suite> <!– Suite –>

[/xml]

Output:

If you want to add or remove parameters, you can do easily by modification in constructor instead of multiple places. Hope this concept will be very much useful when you need to deal with multiple parameters.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

 

Leave a Reply

Your email address will not be published. Required fields are marked *