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.
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 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.
<!-- 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.
Output:
What are TestNG Parameters?
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.
Table of Content
- What are TestNG Parameters?
- Features of TestNG Paramters
- How to Run TestNG Parameters?
- Conclusion
- FAQ’s on TestNG Parameters
In this article, we will explore the concept of TestNG Parameters, Features, and Steps to Run TestNG Parameters.
Contact Us