Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Understanding Parameters in TestNG tests

Parameters can be passed to a method from testNG.xml file. In this article, we will discuss on how to Pass parameter value in the methods.


Focus Area of this article:

  • How to pass a parameter from Testng.xml file to the test method.
  • How to define default value of a parameter in a test method.
  • How to pass multiple parameters through TestNG,xml

How to pass a parameter from Testng.xml file to the test method: 


In the below code, we have defined parameter with name as "Param1". Method mailval has argument test which will accept the value as defined in TestNG.xml file.

package testNG;

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

public class NewTestoptionalParameter {
@Test
@Parameters("Param1")

  public void mailval(String Test)
  {
   System.out.println(Test);
  }
}

In the testNG.xml file, we need to provide the parameter with name and a value assigned to the parameter as seen below. So if we execute this test from testNG.xml file, we will get the output as Parameter has name as param 1.

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <parameter name="Param1" value="Parameter has name as param 1" />  
 <test name="Test1">  
 <classes>  
   <class name="testNG.NewTestoptionalParameter"/>  
 </classes>  
 </test>  
 </suite>  

Let us assume scenario where we have added a parameter in the Test method, but does not define and provide the value of the parameter in the testNG.xml file as shown in the below xml file code.In this case, on execution of the test, we will get the output as blank.

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
 <classes>  
   <class name="testNG.NewTestoptionalParameter"/>  
 </classes>  
 </test>  
 </suite>  

We can assign default value to a parameter using Optional as shown in the code below. Now in case we do not define the parameter in the TestNG.xml file, the argument will work with the assigned default value and will output in console as "testing".

package testNG;

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

public class NewTestoptionalParameter {
@Test
@Parameters("Param1")

  public void mailval(@Optional("testing") String Test)
  {
   System.out.println(Test);
  }
}

Next thing, we can define multiple parameters for a method as shown in the code below:

package testNG;

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

public class NewTestoptionalParameter {
@Test
@Parameters({"Param1","Param2"})

  public void mailval(@Optional("resting") String Test, String Test1)
  {
   System.out.println(Test);
  }
}

And finally , we can define the test parameter at test level as well as suite level. If a parameter is defined at both test as well as suite level, Parameter value defined at test level is considered. 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1">
<parameter name="Param1" value="Parameter has name as param 1s" /> 
<test name="Test1">
 <parameter name="Param1" value="Parameter has name as param 1t" /> 
 <parameter name="Param2" value="Parameter has name as param 2t" /> 
<classes>
    <class name="testNG.NewTestoptionalParameter"/>
</classes>
</test>
</suite>

No comments:

Post a Comment