Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 42: Parameters In TestNG or Parameterization of Methods in a TestNG Class | Make Selenium Easy

Posted on 11/17/2018 By admin

Hello Folks,

Arguments or Parameters help us to achieve reusability of codes and cleanliness of codes. We can do overloading of methods using different number, types and order of parameters to serve differently for different set of values.

TestNG also allows you to parameterized your tests. We will see how can we achieve parameterisation of methods in a TestNG class.

Usage of parameterized methods of TestNG in Selenium:

Some real time examples are as below:

  1. You need to login to your application with different users with different roles. You can not create multiple methods for each user with role type. Instead of that you can create a parameterized methods which will accept user type and login as required.
  2. You may need to connect different databases and each database may have different credentials. This can be achieved easily using parameterized methods.

You can parameterized @Test annotated methods as well as @BeforeXXXX @AfterXXXX methods. It is not like that you can parameterized on test methods. It is an interview question. 

Let’ create some configuration methods and test methods with parameters:

package Parameters;

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

public class ParametersThroughXML {

        @BeforeMethod
        public void configurationBeforeMethod(String confBeforeParameter)
        {
                System.out.println("Paramters for before configuration method: "+confBeforeParameter);
        }
        
        @Test
        public void testMethod(String testParameters)
        {
                System.out.println("Paramters for test method: "+testParameters);
        }
        
        @AfterMethod
        public void configurationAfterMethod(String confAfterParameter)
        {
                System.out.println("Paramters for after configuration method: "+confAfterParameter);
        }
}

Run above testng class as a testng suite:

Got some error as below??

I see many people get this error but they can not understand why they are getting this. So when you run any test class which has parameterized methods without parameterized testng xml, you will get above error message.

How to run parameterized methods of testng class?

There are two ways of running parameterized methods of a testng class. They are:

  1. Using testng xml
  2. Using DataProviders annotated methods.

Passing parameters values to methods from testng xml:

Step 1: Update your parameterized methods with @Parameters annotation:

To provide a parameter from testng xml, you need to use Parameters annotation with method. Remember a method can have multiple annotations.

Example:

package Parameters;

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

public class ParametersThroughXML {

        /* We need to add Parameters annotation to read value from testng xml. Note here that
         * attribute name passed in Parameters must be same as testng xml. 
        */ 
        @Parameters({"confBeforeParameter"})
        @BeforeMethod
        public void configurationBeforeMethod(String confBeforeParameter)
        {
                System.out.println("Paramters for before configuration method: "+confBeforeParameter);
        }
        
        @Parameters({"testParameters1","testParameters2"})
        @Test
        public void testMethod(String testParameters1, String testParameters2)
        {
                System.out.println("Paramters one for test method: "+testParameters1);
                System.out.println("Paramters two for test method: "+testParameters2);
        }
        
        @Parameters({"confAfterParameter"})
        @AfterMethod
        public void configurationAfterMethod(String confAfterParameter)
        {
                System.out.println("Paramters for after configuration method: "+confAfterParameter);
        }
}

Notes:

  1. Parameters annotation is used to read parameters from testng xml for methods in a testng class.
  2. Attribute names provided in Parameters annotations must be same as parameters name provided in testng xml.
  3. Count of attributes in Parameters annotation must be same as count of arguments in that method on which it is used otherwise you will get exception. For Example: If a method accepts two arguments, then @Parameters must have only two attributes.
  4. Attributes are mapped in order as it appears in Parameters annotation with method arguments. For example: First attribute in Parameters annotation is mapped to first attribute in method and so on.

Step 2: Update your testng xml with parameter tags:

Update your testng xml with parameter tags as below for all parameters of methods in testng class as below:

Understand mapping of parameterized methods of testng call with testng xml below:

Now run testng xml. Please note you must need to run from testng xml otherwise you will get injection exception again.

Output:

Hope it will be clear for you how can we use Parameters annotation in a TestNG class with methods and parameter tag in testng xml to achieve parameterization of methods in testng.

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

Uncategorized

Post navigation

Previous Post: Hierarchy of Selenium Classes and Interfaces | Make Selenium Easy
Next Post: TestNG Tutorials 36: Can a Test Method Return a Value in TestNG? | Make Selenium Easy

Related Posts

AddToBuildPath – Make Selenium Easy Uncategorized
August 25, 2018 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
TestNG Tutorials 64: Dependency in TestNG – ignoreMissingDependencies – Another Way of Achieving Soft Dependencies Uncategorized
image – Make Selenium Easy Uncategorized
TestNG Tutorials – Page 5 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark