Open In App

What are TestNG Parameters?

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

TestNG (Test Next Generation) is a popular testing framework for Java applications that facilitates the efficient and organized execution of test cases. One of its key features is the ability to parameterize tests, allowing developers to run the same test method with different sets of data. TestNG parameters provide a flexible way to pass values to test methods, enhancing test reusability and making it easier to perform data-driven testing.

In this article, we will explore the concept of TestNG Parameters, Features, and Steps to Run TestNG Parameters.

What are TestNG Parameters?

In TestNG, parameters play an important role in enhancing test flexibility and reusability by allowing the dynamic passing of specific values during runtime. Similar to parameter declarations in other programming languages, TestNG parameters are utilized to pass distinct values to functions, enabling the execution of multiple operations with different inputs or varying operations with the same input.

Example:

Consider a scenario where a variable needs to be input into a text box. While it’s possible to modify the variable’s name in the test code for different values, the complexity and length of the code often make parameters a more efficient choice. This not only enhances code readability but also contributes to code precision.

Syntax:

<parameter name=”param” value=”First parameter” />

Features of TestNG Paramters

  1. Simplified Value Passing: TestNG parameters facilitate the straightforward passing of values within methods, simplifying the process of incorporating dynamic inputs into test scenarios.
  2. Efficient Value Storage and Reuse: The framework enables the efficient storage and reuse of values in the code, promoting a more organized and streamlined approach to managing test data.
  3. Streamlined Data Flow Management: TestNG parameters contribute to the efficient setup and management of data flow across multiple tests, providing a structured and scalable solution for handling diverse test scenarios.
  4. Flexibility in Value Adjustment: Parameters offer flexibility by allowing values to be adjusted without directly modifying test parameters. This flexibility is valuable for adapting tests to varying conditions.

How to Run TestNG Parameters?

This method is used to send the parameter value (string type) directly to the test method at runtime from the testng.xml file. To use this method, we need to pass a parameter value to the test method using the parameter description. Let’s create a simple example where we will pass a parameter value to the test method from an XML configuration file. Follow these steps:

Step 1: Open Eclipse and create a new Java project. Create a sample package within the project, and in the package, add a class with TestNG parameters to specify test data.

Step 2: Create a Java class file named “TestNGParameterDemoXML” in the sample package to implement TestNG parameters and define your test methods.

creating-java-file

Create a Java class file

Step 3: Create a program using TestNG in “TestNGParameterDemoXML” to automate the Google homepage, retrieve its title, and parameterize the browser and URL in the testng.xml configuration file for flexible testing configurations. See the code for details.

  • The @Parameters annotation can be applied to methods with annotations like @Test, @Before/@After, and @Factory.
  • Additionally, the @Parameters annotation can be used on a class constructor.
Java
// Java Program to Automate Google HomePage

package TestNGParameterDemoXML; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Parameters; 
import org.testng.annotations.Test; 

public class ParameterTest { 
    // Create first WebDriver reference. 
    WebDriver driver; 

    // Set up method to initialize WebDriver based on browser parameter.
    @Parameters({"Browser"}) 
    @BeforeTest 
    public void setUp(String browser) { 
        if(browser.equalsIgnoreCase("Firefox")) { 
            driver = new FirefoxDriver(); 
            System.out.println("Firefox opened"); 
        } else { 
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); 
            driver = new ChromeDriver(); 
            System.out.println("Chrome opened"); 
        } 
        driver.manage().window().maximize(); 
    } 

    // Test method to navigate to the specified URL.
    @Parameters({"URL"}) 
    @Test(priority = 1) 
    public void getURL(String URL) { 
        driver.get(URL); 
    } 

    // Test method to retrieve and print the title of the webpage.
    @Test(priority = 2) 
    public void getTitle() { 
        String title = driver.getTitle(); 
        System.out.println("Title of Webpage: " + title); 
    } 

    // Close method to quit the WebDriver after tests are executed.
    @AfterTest 
    public void close() { 
        driver.close(); 
    } 
}


Step 4: Create a new file named “ParameterTestDemo1.xml” in the TestNG project. This XML file will be used to configure and execute TestNG tests, including parameters for the “ParameterTest” class.

  • In the TestNG XML configuration file (testng.xml), parameters can be defined under the <suite> or <test> tags. If there are two parameters with the same name, the one defined in <test> takes precedence.
  • This allows overriding parameter values for specific tests while maintaining a default value for all tests.
XML
<!-- TestNG suite configuration with name "Suite" and verbosity level 1 -->
<suite name="Suite" verbose="1"> 
    <!-- Test configuration with name "Test" -->
    <test name="Test"> 
        <!-- Parameters for the test: Browser and URL -->
        <parameter name="Browser" value="Firefox"/> 
        <parameter name="URL" value="https://www.google.com"/> 
        
        <!-- Specify the test class -->
        <classes> 
            <class name="ParameterTestDemoXML.ParameterTestDemo1"/> 
        </classes> 
    </test> 
</suite>


Step 5: Open the testng.xml file in Eclipse, right-click on it, and choose “Run As” > “TestNG Suite”. This action executes the configured TestNG suite. The Eclipse console window will display the output of the test execution, including information about the tests, their status, and any logs or messages generated during the process.



Executing TestNG Suite

TestNG Parameters Output

built and ran sample programs using the parameterization tools in TestNG

Output:

Output

Conclusion

In conclusion, TestNG parameters provide testers with a robust mechanism for customizing test execution, enhancing testing efficiency, and offering a clear analysis of usage behavior. TestNG significantly contributes to increased efficiency and reliability in software testing by delivering flexibility, reducing code duplication, easing maintenance, and improving overall test management.

FAQ’s on TestNG Parameters

What exactly are TestNG Parameters?

Ans: TestNG Parameters refer to the values that testers can pass to their test methods. These values help in parameterizing tests, allowing for more dynamic and customizable testing scenarios.

How do TestNG Parameters enhance testing flexibility?

Ans: TestNG Parameters enable testers to execute the same test method with different input values. This flexibility allows for more comprehensive testing coverage and the ability to test various scenarios without writing separate test methods.

Can TestNG Parameters be used to test different data sets?

Ans: Yes, TestNG Parameters are commonly used to test different data sets. Testers can pass different values as parameters to a test method, allowing for testing with various input data and conditions.

Are TestNG Parameters only limited to primitive data types?

Ans: No, TestNG Parameters support not only primitive data types like integers and strings but also complex data types such as arrays and objects. This versatility allows for testing a wide range of scenarios.

How are TestNG Parameters specified in test methods?

Ans: TestNG Parameters are specified using the @Parameters annotation in TestNG test methods. Testers can define parameters in XML configuration files or directly within the test class using annotations, making them easily accessible within the test methods.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads