Make Selenium Easy

TestNG Tutorials 48: How to Pass Parameters of Different Datatypes in TestNG

Hello Folks,

Most of us know that, we can pass parameter as a String to methods in TestNG class. Is it really like that? No. Its not.

We can pass parameters in whatever data types we required. TestNG will convert them automatically to required data type. We will see an example below:

package Parameters;

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

public class ParametersWithDifferentDatatypes {

	/* TestNG will convert automatically passed parameter value sin desired data types.*/
	@Parameters({"StringParam", "intParam", "booleanParam"})
	@Test
	/*
	 * A test method accepting argument of different datatypes
	 */
	public void sampleTest(String stringParaValue, int intParamValue, boolean booleanParamValue)
	{
		System.out.println("String parameter: "+stringParaValue);
		System.out.println("Int parameter: "+intParamValue);
		System.out.println("Boolean parameter: "+booleanParamValue);
	}
	
	
}

Output:

[RemoteTestNG] detected TestNG version 6.14.2
String parameter: Amod
Int parameter: 28
Boolean parameter: true

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Note:
1. You need to pass correct values as per desired data types in testng xml. If you are using an integer datatype in test method and passing an alphabet string from testng xml, you will get NumberFormatException.

Example:
Modify above testng.xml as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="StringParam" value="Amod" />
<parameter name="intParam" value="dfgfdg" />
<parameter name="booleanParam" value="true" />
  <test thread-count="5" name="Test">
  <classes>
			<class name="Parameters.ParametersWithDifferentDatatypes">
			
			</class>
		</classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Output:

[RemoteTestNG] detected TestNG version 6.14.2
[Utils] [ERROR] [Error] java.lang.NumberFormatException: For input string: &quot;dfgfdg&quot;
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.valueOf(Integer.java:766)
	at org.testng.internal.Parameters.convertType(Parameters.java:466)
	at org.testng.internal.Parameters.createParams(Parameters.java:293)
	at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:359)
	at org.testng.internal.Parameters.createParameters(Parameters.java:620)
	at org.testng.internal.Parameters.handleParameters(Parameters.java:769)
	at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
	at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:923)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

2. TestNG is intelligent in case of boolean. If you pass anything true, it will take true and false in case of false. If you pass anything other than this, it will take false by default.

Java Code:

package Parameters;

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

public class ParametersWithDifferentDatatypes {

	/* TestNG will convert automatically passed parameter value sin desired data types.*/
	@Parameters({"StringParam", "intParam", "wrongParam","trueParam","falseParam"})
	@Test
	/*
	 * A test method accepting argument of different data types
	 */
	public void sampleTest(String stringParaValue, int intParamValue, boolean wrongParamValue, boolean trueParamValue, boolean falseParamValue)
	{
		System.out.println("String parameter: "+stringParaValue);
		System.out.println("Int parameter: "+intParamValue);
		System.out.println("Wrong Boolean parameter: "+wrongParamValue);
		System.out.println("True Boolean parameter: "+trueParamValue);
		System.out.println("False Boolean parameter: "+falseParamValue);
	}
	
	
}

Testng xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="StringParam" value="Amod" />
<parameter name="intParam" value="123" />
<parameter name="wrongParam" value="NoTrueNoFalse" />
<parameter name="trueParam" value="true" />
<parameter name="falseParam" value="false" />
  <test thread-count="5" name="Test">
  <classes>
			<class name="Parameters.ParametersWithDifferentDatatypes">
			
			</class>
		</classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Output:

[RemoteTestNG] detected TestNG version 6.14.2
String parameter: Amod
Int parameter: 123
Wrong Boolean parameter: false
True Boolean parameter: true
False Boolean parameter: false

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

3. You can not pass user defined data types in parameters. See an example below:

package Parameters;

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

class someClass {

}

public class ParametersWithDifferentDatatypes {

	/*
	 * TestNG will convert automatically passed parameter value sin desired data
	 * types.
	 */
	@Parameters({ "StringParam" })
	@Test
	/*
	 * A test method accepting argument of different data types
	 */
	public void sampleTest(someClass stringParaValue) {
		System.out.println("String parameter: " + stringParaValue);

	}

}


Testng xml:


Output:
[RemoteTestNG] detected TestNG version 6.14.2
[Utils] [ERROR] [Error] org.testng.TestNGException: 
Unsupported type parameter : class Parameters.someClass
	at org.testng.internal.Parameters.convertType(Parameters.java:495)
	at org.testng.internal.Parameters.createParams(Parameters.java:293)
	at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:359)
	at org.testng.internal.Parameters.createParameters(Parameters.java:620)
	at org.testng.internal.Parameters.handleParameters(Parameters.java:769)
	at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
	at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:923)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

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

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

5 thoughts on “TestNG Tutorials 48: How to Pass Parameters of Different Datatypes in TestNG

  1. why don’t you increase the width of “code section”, scrolling right even for small codes is very very trouble.

  2. Hi Amod , The issue which we are facing is that the feedback window will at appear at any time. so we are not sure how to handle it . could you please explain a little bit more on this . i

  3. Hi Amod, Could you please let me know How we can handle the random Feedback window in Ecomm application. The problem is that feed back window is not appearing everytime and it is appearing randomly and blocking the focus and stopping the execution, is there anyother way to handle it .

    1. Hello, you need to handle this while locating element and you get visibility as false. You should add a check if feedback window appeared. If yes, handle and relocate element.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

TestNG Tutorials 48: How to Pass Parameters of Different Datatypes in TestNG

by Amod Mahajan time to read: 5 min
5
%d bloggers like this: