How to pass Parameters in TestNG

 HOME

One of the important features of TestNG is the ability to pass different test data to a test case as arguments, which is called parametrization

There are mainly two ways through which we can provide parameter values to TestNG tests.

  1. Through testng.xml  XML configuration file
  2. Through DataProviders

In this tutorial, we will discuss using testng.xml for parametrization. If we need to pass some simple values such as String or Integer types to the test methods at runtime, there is something called @Parameter where parameter values through TestNG XML configuration files pass to test.

@Parameters("value")

Let us explain how we can use parameters. To start with, add the below dependencies to the POM.xml in the case of the Maven project.

<dependencies>
  
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>3.141.59</version>
      </dependency>
      
      <dependency>
          <groupId>io.github.bonigarcia</groupId>
          <artifactId>webdrivermanager</artifactId>
          <version>5.1.0</version>
       </dependency>

      <dependency>
           <groupId>org.testng</groupId>
           <artifactId>testng</artifactId>
           <version>7.5</version>
           <scope>test</scope>
      </dependency>

  </dependencies>

Step 1 – Create a JAVA test class, say, TestNGParameterizationDemo.java

Step 2 – Add test method parameterizedTest() to the test class. This method takes a string as an input parameter

Add the annotation @Parameters(“browser”) to this method. The parameter passes a value from testng.xml

Step 3 – Create a TestNG.xml and pass the value of the parameter in this configuration file.

import static org.testng.Assert.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class TestNGParameterizationDemo {
	
	 WebDriver driver;
	 
     @BeforeMethod
     @Parameters("browser")
     public void parameterizedTest(String browser) {
        if (browser.equalsIgnoreCase("firefox")) {

        	 driver = WebDriverManager.firefoxdriver().create();
             System.out.println("Browser Started :" + browser);
          
        } else if (browser.equalsIgnoreCase("chrome")) {
  
        	 driver = WebDriverManager.chromedriver().create();
             System.out.println("Browser Started :" + browser);
        }
        
        driver.get("https://opensource-demo.orangehrmlive.com/");
        driver.manage().window().maximize();
    }
     
     @Test
     public void validCredentials()  {

    	 driver.findElement(By.xpath("//*[@id='txtUsername']")).sendKeys("Admin");
    	 driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys("admin123");
    	 driver.findElement(By.xpath("//*[@id='btnLogin']")).click();
         String newPageText = driver.findElement(By.xpath("//*[@id='content']/div/div[1]/h1")).getText();
         System.out.println("newPageText :" + newPageText);
         assertEquals(newPageText,"Dashboard");
     }

     @Test
     public void invalidCredentials() {
     	
         driver.findElement(By.xpath("//*[@id='txtUsername']")).sendKeys("1234");
         driver.findElement(By.xpath("//*[@id='txtPassword']")).sendKeys("12342");
         driver.findElement(By.xpath("//*[@id='btnLogin']")).click();
         String actualErrorMessage = driver.findElement(By.xpath("//*[@id='spanMessage']")).getText();
         System.out.println("Actual ErrorMessage :" + actualErrorMessage);
         assertEquals(actualErrorMessage,"Invalid credentials");

     }

     @AfterMethod
     public  void closeBrowser() {
         driver.quit();
     }
}

TestNG.xml looks like as shown below. Here, the parameter name is the browser name value for the browser is “Chrome”. So, this “Chrome” value is passed to Test as a parameter and as a result, a Google Chrome browser opens. Similarly, the same tests are run using Firefox, as it is mentioned in the testng.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite ">
 
  <test name="Chrome Test">
   <parameter name="browser" value="chrome" />
   <classes>
      <class name="com.example.TestNGParameterizationDemo" />
   </classes>
  </test> <!-- Test -->
  
   <test name="Firefox Test">
   <parameter name="browser" value="firefox" />
   <classes>
      <class name="com.example.TestNGParameterizationDemo" />
   </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

The output of the above program is

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s