Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use TestNG’s @Parameters annotation to pass a few named settings—such as a browser and base URL—from testng.xml into a Selenium test. Use @DataProvider when the test must run over multiple rows of data or when its inputs are generated in Java. The examples below show both approaches, explain parameter scope and defaults, and cover the isolation needed when data-driven tests run in parallel.

Choose between @Parameters and @DataProvider

Both mechanisms supply arguments to TestNG test methods, but they serve different purposes.

Use Best fit Example
@Parameters A small set of named configuration or environment values. Browser, base URL, locale, or credentials supplied by a suite or test configuration.
@DataProvider Multiple test cases, tabular input, or data created by Java or another source. Several username/password pairs, each producing a separate login-test invocation.

A practical rule is to use XML parameters to configure where and how a test runs, and a data provider to describe the cases the test should exercise. You can use both in the same test suite when the configuration and test cases are distinct.

Pass browser and URL values from testng.xml

Declare values in XML and list their names in @Parameters. TestNG maps the values to the Java method arguments in the order given in the annotation.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Java test

package tests;

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

public class HomeTest {
    @Parameters({"browser", "baseUrl"})
    @Test
    public void openHomePage(String browser, String baseUrl) {
        WebDriver driver = DriverFactory.createDriver(browser);
        try {
            driver.get(baseUrl);
            // Add assertions for the page under test.
        } finally {
            driver.quit();
        }
    }
}

DriverFactory.createDriver represents your project’s browser setup: it must return a WebDriver for the requested browser, and your build must include the Selenium and TestNG dependencies and a compatible driver/browser setup. The snippet’s parameter mapping is runnable once that project-specific factory is implemented.

XML suite

<suite name="UI suite">
  <parameter name="browser" value="chrome"/>
  <parameter name="baseUrl" value="https://example.test"/>
  <test name="smoke">
    <classes>
      <class name="tests.HomeTest"/>
    </classes>
  </test>
</suite>

Replace https://example.test with the application URL and chrome with a browser value your driver factory supports. Keep the parameter names identical in XML and in the annotation. Keep the annotation order aligned with the Java signature: browser supplies the first argument and baseUrl the second.

Set parameter scope and override values

TestNG allows parameters at suite, test, class, and methods scopes. The documented precedence is suite → test → class → methods: a narrower scope can override a broader value. Put values at the broadest scope that should share them, and use a narrower declaration when one test or method needs a different setting.

For example, a suite-level browser can be reused by tests, while a value declared under a particular <test> can customize that test. Check scope when the value received at runtime differs from the suite-level value you expected.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

TestNG also documents overriding parameters with JVM properties, such as -Dbrowser=firefox. The exact way you pass that option depends on your build or test runner. Use a value supported by your driver setup; the property does not itself install or configure a browser driver.

Supply a fallback with @Optional

If a parameter may be absent, mark its Java argument with @Optional and provide a default. TestNG supplies that value when the named parameter is missing.

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

public class SmokeTest {
    @Parameters("browser")
    @Test
    public void smoke(@Optional("chrome") String browser) {
        WebDriver driver = DriverFactory.createDriver(browser);
        try {
            // Exercise the smoke-test flow.
        } finally {
            driver.quit();
        }
    }
}

Use a default only when it is a safe and intentional fallback. If choosing the wrong browser, environment, or account could make a test misleading, require the value instead and fail clearly when configuration is missing.

Run a Selenium test with multiple datasets

For repeated cases, create a @DataProvider that returns rows. Each row is an Object[]; TestNG assigns its entries to the test method arguments for one invocation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class LoginTest {
    @DataProvider(name = "loginCases")
    public Object[][] loginCases() {
        return new Object[][] {
            {"alice", "correct-password"},
            {"bob", "another-password"}
        };
    }

    @Test(dataProvider = "loginCases")
    public void login(String username, String password) {
        WebDriver driver = DriverFactory.createDriver("chrome");
        try {
            driver.get("https://example.test/login");
            // Enter username and password, submit, and assert the expected result.
        } finally {
            driver.quit();
        }
    }
}

The example demonstrates the binding pattern; replace the example URL and add the application-specific locators, actions, and assertions. Do not commit real credentials in source-controlled test data. Use an appropriate protected configuration source for secrets, and make sure each test case receives the data it is intended to verify.

Keep provider rows aligned with the test signature

Each row must supply the arguments expected by the test method, in matching order. In the example, the first value becomes username and the second becomes password. A missing, extra, or wrongly ordered value can cause invocation failures or exercise the wrong case.

Put a provider in another class

TestNG supports providers in a separate class through dataProviderClass. Keep the provider name identical in the annotation and the provider declaration.

@Test(dataProvider = "loginCases", dataProviderClass = LoginData.class)
public void login(String username, String password) {
    // Test one row supplied by LoginData.loginCases().
}

The provider class must contain the named provider method in the form expected by your TestNG setup. TestNG also documents iterator and custom-array provider forms, as well as injection of Method or ITestContext into data providers, for cases where the rows depend on the invoking test or execution context.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run data-provider invocations safely in parallel

TestNG’s @DataProvider has a parallel option, which defaults to false. Setting it to true allows generated invocations to run concurrently:

@DataProvider(name = "loginCases", parallel = true)
public Object[][] loginCases() {
    return new Object[][] {
        {"alice", "correct-password"},
        {"bob", "another-password"}
    };
}

Parallel execution can reduce elapsed time when cases are independent, but it makes shared browser state unsafe. Treat the WebDriver and mutable test data as invocation-scoped: create a separate driver for each invocation or thread, avoid sharing page state, and always quit each driver in teardown. A carefully managed driver factory or ThreadLocal can help, but it must not allow one invocation to read or quit another invocation’s driver. These are Selenium isolation practices required by concurrent execution, not guarantees provided automatically by the provider annotation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Inspect parameter values and troubleshoot failures

TestNG’s HTML reports show the parameters used to invoke test methods. Use the report to distinguish a mapping/configuration problem from a failure in the Selenium flow.

  • Parameter not found: Check that the XML name exactly matches the string in @Parameters. Spelling and capitalization must line up.
  • Arguments received in the wrong order: Compare the order in @Parameters with the Java method signature. TestNG maps according to the annotation order.
  • Unexpected value despite a suite setting: Check for an override at test, class, or methods scope; narrower scopes take precedence.
  • Missing parameter should be acceptable: Add @Optional("default") to that method argument. If absence should be an error, do not hide it with a default.
  • Provider name not found: Ensure @Test(dataProvider = "...") matches @DataProvider(name = "..."). If the provider is in another class, specify the correct dataProviderClass.
  • Provider arguments do not fit the test: Check that every row has the expected number and order of values for the test method signature.
  • Intermittent failures only when parallel: Remove shared WebDriver or mutable data state, isolate each invocation, and confirm teardown quits the invocation’s own driver.
  • Wrong browser or navigation fails: Confirm the received parameter in the TestNG report, then check that your driver factory supports that browser and that the supplied URL is the intended environment.

Capture test evidence without maintaining a browser script

For screenshots of web pages beyond your Selenium test flow, ScreenshotNeo is a website screenshot API and MCP server for developers. Its clean-shot workflow accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; those cleanup steps can be turned off. Only clean shots are billed: bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and billing status.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Or skip the browser setup

One GET request returns a screenshot or PDF. The following cURL example saves a WebP screenshot of the test site; see the ScreenshotNeo API documentation for request options and response details.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.test -o shot.webp

ScreenshotNeo also has an MCP server with take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Cookie banners, popups, and chat widgets are removed before capture, and bot checks, blank pages, and failed loads are never billed. Sign up for ScreenshotNeo free: 1,000 screenshots a month, no card.

Frequently Asked Questions

Can I use @Parameters and @DataProvider in the same TestNG project?

Yes. Use XML parameters for shared run configuration and a data provider for per-invocation test cases; keep those responsibilities distinct.

Where can I see the arguments TestNG passed to a test?

TestNG’s generated HTML reports show the parameters used to invoke test methods.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.