Selenium

TestNG Parameterization in Selenium Java – How to Pass Parameters from XML using @Parameters

TestNG Parameterization in Selenium Java – Pass Parameters from XML using @Parameters

By Bhau Automation • Learn TestNG Parameterization Practically

🎯 What You Will Learn

  • What is Parameterization in TestNG?
  • Why Parameterization is needed in Test Automation
  • How to use @Parameters annotation
  • How to pass parameters from TestNG XML file
  • Real-time example of passing browser name and URL
💡 Pro Tip: Parameterization helps you avoid hardcoding values and makes your tests flexible, reusable, and environment-independent.

📘 What is Parameterization in TestNG?

Parameterization in TestNG allows you to pass data or configuration values to test methods dynamically from external sources like XML files. This is useful when the same test needs to run with different sets of input data or configurations.

🔧 Using @Parameters Annotation

The @Parameters annotation in TestNG is used to pass parameters directly from the TestNG XML file to your test methods. This makes your automation framework more flexible and eliminates hardcoded values.

Syntax:

@Parameters({"parameterName"})
public void testMethod(String parameterName) {
    // use parameter
}
  

💻 Example – Passing Browser and URL from XML

// DemoTest.java
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class DemoTest {

  @Test
  @Parameters({"browser", "url"})
  public void launchApp(String browser, String url) {
      System.out.println("Browser Name: " + browser);
      System.out.println("Application URL: " + url);
  }
}
  

TestNG XML Configuration:




  
    
    
    
      
    
  

  

🔍 Output

Browser Name: chrome
Application URL: https://www.google.com
  

📈 Advantages of Parameterization

  • ✅ Avoids hardcoding of data
  • ✅ Allows flexible test configuration
  • ✅ Reduces code duplication
  • ✅ Enables environment-based testing (QA, Stage, Prod)
  • ✅ Simplifies test maintenance

⚙️ Best Practices

  • Always define default values for parameters if not provided.
  • Keep parameters organized and descriptive in XML.
  • Use parameterization for credentials, URLs, and browser configuration.
Next Step: Explore @DataProvider annotation for data-driven testing in TestNG.

🎥 Watch Full Tutorial on YouTube

👉 Watch: TestNG Parameterization in Selenium Java

🚀 Created with ❤️ by Bhau Automation

Back to All Articles