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:
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:
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); } }
TestNG 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 -->
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
My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning
Hello Folks, As part of our API Testing series, we will see "Sending first GET request in Postman". First of all…
Hello Guys, DataProvider method in TestNG is a way to provide test data to Test annotated methods in a TestNG…
Hello Folks, As part of our API Testing series, we will see "Introduction of Postman Tool" in this post. Postman is…
Hello Folks, As part of our API Testing series, we will see "Tools to test SOAP and REST APIs manually and…
Hello Folks, As part of our API Testing series, we will see "Difference between SOAP and REST web services" in…
Hello Folks, As part of our API Testing series, we will see “Introduction of SOAP” in this post. SOAP stands…