Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

TestNG Framework

  • What is TestNG Framework?
  • Advantages and Disadvantages of TestNG:
  • Difference between TestNG And Junit framework
  • What is TestNG Annotations?
  • Install TestNG In Eclipse & IntelliJ?
  •  Hierarchy In TestNG Annotations
  • TestNG’s prioritization
  • TestNG Dependent
  • Reporter Class in TestNG
  • TestNG Reports
  • Assertions in TestNG
  • TestNG Groups
  • TestNG Parameters
  •  Cross-Browser Testing in TestNG
  •  Parallel Testing in TestNG
  • Data Providers
  • TestNG Listeners
  • Rerunning failed tests in TestNG
View Categories
  • Home
  • Tutorials
  • TestNG
  • TestNG Framework
  • TestNG Parameters

TestNG Parameters

TestNG Parameters

1. What Are TestNG Parameters?
TestNG parameters let you inject external values (strings, numbers, etc.) into your test or configuration methods, without hard-coding them. You declare parameters in your testng.xml and then annotate your methods with @Parameters, so you can drive things like URLs, browsers, credentials, or any other config at runtime.

// Example of using a parameter in a test method

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class ParameterExample {

  @Test

  @Parameters(“env”)            // name of the XML parameter

  public void testEnv(String env) {

    System.out.println(“Running tests against environment: ” + env);

    // … use `env` to pick base URL, credentials, etc. …

  }

}

2. How to Run TestNG Parameters?

  1. Define your parameters in testng.xml.
  2. Annotate your methods with @Parameters(“name”) (or multiple names).
  3. Point your TestNG runner (Eclipse, CLI, Maven) at that XML—TestNG reads the values and injects them.

Run via CLI:

java -cp “bin:libs/*” org.testng.TestNG testng.xml

With Maven (Surefire):

<build>

  <plugins>

    <plugin>

      <artifactId>maven-surefire-plugin</artifactId>

      <configuration>

        <suiteXmlFiles>

          <suiteXmlFile>testng.xml</suiteXmlFile>

        </suiteXmlFiles>

      </configuration>

    </plugin>

  </plugins>

</build>

mvn test


3. Define TestNG Parameters at the Suite Level
Putting <parameter> directly under <suite> makes it available to all <test> blocks and classes:

<!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd”>

<suite name=”MySuite”>

  <!– Suite-level parameter –>

  <parameter name=”env” value=”staging”/>

  <parameter name=”browser” value=”chrome”/>

  <test name=”SmokeTests”>

    <classes>

      <class name=”com.example.tests.ParameterExample”/>

    </classes>

  </test>

  <test name=”RegressionTests”>

    <classes>

      <class name=”com.example.tests.ParameterExample”/>

    </classes>

  </test>

</suite>

All methods annotated with @Parameters(“env”) or @Parameters(“browser”) in any class will receive these values when you run MySuite.


4. Overriding Power in TestNG Parameters
You can override suite-level values by redefining a parameter at the <test> level:

<suite name=”OverrideSuite”>

  <!– default values –>

  <parameter name=”browser” value=”chrome”/>

  <!– Uses Chrome –>

  <test name=”ChromeTests”>

    <classes>

      <class name=”com.example.tests.ParameterExample”/>

    </classes>

  </test>

  <!– Overrides to Firefox –>

  <test name=”FirefoxTests”>

    <parameter name=”browser” value=”firefox”/>

    <classes>

      <class name=”com.example.tests.ParameterExample”/>

    </classes>

  </test>

</suite>

  • Suite-level browser=chrome applies by default.
  • FirefoxTests explicitly sets browser=firefox, overriding the suite default for that <test> only.

5. Optional Parameters
Sometimes you want a sane default if a parameter isn’t provided in the XML. Use @Optional in your method signature:

import org.testng.annotations.Optional;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class OptionalParameterExample {

  @Test

  @Parameters(“timeout”)

  public void timeoutTest(@Optional(“30”) int timeoutInSeconds) {

    System.out.println(“Timeout is set to ” + timeoutInSeconds + ” seconds”);

    // … use timeoutInSeconds …

  }

}

  • If <parameter name=”timeout” …/> is present, TestNG converts its value (e.g. “45”) to an int.
  • If it’s missing, TestNG uses the default 30 defined in @Optional(“30”).

Putting It All Together #

<!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd”>

<suite name=”FullConfigSuite”>

  <!– Suite-level defaults –>

  <parameter name=”env”     value=”dev”/>

  <parameter name=”browser” value=”chrome”/>

  <test name=”Smoke”>

    <!– inherits env=dev, browser=chrome –>

    <classes>

      <class name=”com.example.tests.FullParameterExample”/>

    </classes>

  </test>

  <test name=”CrossBrowser”>

    <!– override browser only –>

    <parameter name=”browser” value=”firefox”/>

    <classes>

      <class name=”com.example.tests.FullParameterExample”/>

    </classes>

  </test>

</suite>

package com.example.tests;

import org.testng.annotations.Optional;

import org.testng.annotations.Parameters;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

public class FullParameterExample {

  private String env;

  private String browser;

  private int retries;

  @BeforeClass

  @Parameters({“env”,”browser”,”retries”})

  public void setUp(String env,

                    String browser,

                    @Optional(“2”) int retries) {

    this.env = env;

    this.browser = browser;

    this.retries = retries;

    System.out.printf(“Env=%s, Browser=%s, Retries=%d%n”, env, browser, retries);

    // … initialize WebDriver based on browser …

  }

  @Test

  public void sampleTest() {

    System.out.println(“Running test on ” + env + ” in ” + browser);

    // … test logic …

  }

}

With this pattern you get:

  • Suite-level defaults for broad configuration.
  • Test-level overrides for specific runs (e.g. different browsers).
  • Optional fallbacks so missing parameters don’t break your suite.
testng framework
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
TestNG Groups Cross-Browser Testing in TestNG
Table of Contents
  • Putting It All Together
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105