Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Parameters in TestNG - Testingpool

Parameters in TestNG

In the previous post, we have learnt about assertions and reporting in TestNG. In this post, we will learn about parameters in TestNG.

As we know, parameterization plays very important role in automation testing. It is required when we don’t want to hard code the value in our code and it is not a good practice as well. Then parameterization comes into picture, so we will not have to hard code any value and it will be easy to handle the new data also.

Suppose, any scenario needs to be run with different set of data. This can be achieved by parameterization. Selenium itself is not sufficient to handle parameterization , that’s why it takes help from TestNg Framework. TestNG provides one more good feature called Parameters.

TestNG lets you pass parameters directly to your test methods with your testng.xml.

Let’s understand with an example:

  1. Adding parameters into testng.xml file. Added 2 parameters named as ‘browser’ and ‘app’.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ExampleTestSuite" >
  
  <test name="Parameters functionality">
  <parameter name="browser" value="Firefox"/>
  <parameter name="app" value="Gmail"/>
    <classes>
      <class name="com.testsuite.example.ParametersExample"/>
    </classes>
  </test>
</suite>

2. Pass both the parameters to the method “Test” by using @Parameters annotation. The same no. of arguments has to be passed in the method.

package com.testsuite.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParametersExample {
	private WebDriver driver;
	
	
	@Test
	@Parameters({"browser","app"})
	public void Test(String browser,String Application){

		//With browser parameter
		if(browser.equalsIgnoreCase("Firefox")){
			driver = new FirefoxDriver();
			
		}else if(browser.equalsIgnoreCase("chrome")){
			//Code for opening chrome
		}
		
		if(Application.equalsIgnoreCase("Gmail")){
			driver.get("http://www.gmail.com");
		}else if (Application.equalsIgnoreCase("Gmail")){
			driver.get("http://www.gmail.com");
		}
	}
}

Output: This method will open a Firefox browser with Gmail appliacation.


 

Define Parameters at different scope:

Parameters can be defined at different scopes based on the need. It can be defined at suite level, test level, classes level or at the class level. If you define a parameter at suite level then all the classes defined under the suite can access it without the need of re-defining. You can also override a parameter at a lower scope. For example, in the below configuration file, we define Suite2 at suite level and then override it at class level.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="ExampleTestSuite" >
  <parameter name="suiteParam" value="Truck"></parameter>
  <parameter name="suiteCommon" value="Vehicle"></parameter>
  <test name="Parameters functionality">
      <parameter name="browser" value="Firefox"/>
       <parameter name="app" value="Gmail"/>
    <classes>
           <parameter name="ClassesParam" value="All Classes"></parameter>
      <class name="com.testing.Class1">
     		 <parameter name="ClassParam" value="ScooterClass"></parameter>
      </class>
      <class name="com.testing.Class2"></class>
    </classes>
  </test>
</suite>

Class1:

package com.testing;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Class1 {

	
	
	@Test
	@Parameters({"browser"})
	public void Test3(String browser){
		System.out.println("Test3 opens "+ browser);
	}
	
	@Test
	@Parameters({"ClassesParam"})
	public void Test4(String allClasses){
		System.out.println("Test4  "+ allClasses);
	}
	
	@BeforeSuite
	@Parameters({"suiteParam"})
	public void Test1(String suiteParam1){
		System.out.println("Test1 Suite Parameter" +suiteParam1);
	}
	
	@BeforeTest	
	@Parameters({"suiteCommon"})
	public void Test2(String suiteCommon){

		System.out.println("Test2  - "+suiteCommon);
		
	}
}

Class2:

package com.testing;

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

public class Class2 {
	@Test
	@Parameters({"browser","app"})
	public void Test(String browser,String app){
		System.out.println(browser);
		System.out.println(app);

	}
}

TestNG output multiple scope


 

Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...