My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Parameterization in TestNG in Selenium using Eclipse

Emelie Barett's photo
Emelie Barett
·Nov 21, 2019

One of the interesting features that TestNG provides is parameterization. It is also known as Parametric Testing, In most business cases, you’ll come across scenarios where the implementation logic require several test cases. Parameterization provides developers with the facility of running the same test over and over again using different values.

And this is done using the TestNG @Parameters annotations.

Using TestNG you can pass parameters directly to the test functions in two ways:

  1. Through testng.xml file
  2. Through Dataproviders

In this article, we’ll see how to use Parameters in the testng.xml file and reference them in the .java source file. Let us understand this through a demonstrated example.

Before moving ahead, you should have an existing Project(here, example) created.

Now, let us move ahead and perform parameterized testing. Since @parameters annotation is a test annotation you’d need to create a TestNG class and also since we will be using parameters, which of course need to be defined in the testng.xml file so, we’ll mention the name of our .xml file as well.

Step 1:

  1. Right-click on the project(here, example).
  2. Click on New.
  3. Then, click on Other. Note: Project named example and a package named example are already created,

sele1.png

Step 2:

  1. Select TestNG class.
  2. Click on Next.

sele3.png

Step 3:

  1. Browse your source folder(src) under the project.
  2. Browse the package.
  3. Write the class name(here, exampleClass).
  4. Mention the name of the XML file(here, parameterization.xml).
  5. Then, click on Finish.

sele4.png

Step 4: After that, you can see the class(here, exampleClass) and .xml(here, parameterization.xml) file being populated under the example package.

sele6.png

Step 5: Navigate to your parameterization.xml file and add two of the parameter tags with name and value set to each one of them. In the first parameter, in the name key, we assign the value browser, which is user-defined. And in the value, we assign the value chrome(the browser using which we want to perform testing) In the second parameter, in the name key, we assign the value URL, and in the value, we assign the link to which we want the browser to navigate to.

sele7.png

Step 6: Copy and paste the below code in your local system’s Eclipse IDE.

package example;

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.annotations.Parameters; import org.testng.annotations.Test;

public class exampleClass { WebDriver driver; @Parameters({"browser","URL"}) @Test public void launchBrowser(String browser, String URL) {

  switch(browser) {


  case "chrome":  
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\Intellipaat-Team\\Downloads\\driver\\chromedriver.exe");
      driver = new ChromeDriver();

package example;

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.annotations.Parameters; import org.testng.annotations.Test;

public class exampleClass { WebDriver driver; @Parameters({"browser","URL"}) @Test public void launchBrowser(String browser, String URL) {

  switch(browser) {


  case "chrome":  
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\Intellipaat-Team\\Downloads\\driver\\chromedriver.exe");
      driver = new ChromeDriver();
      break;
  case "firefox": 
      System.setProperty("webdriver.gecko.driver", "C:\\Users\\Intellipaat-Team\\Downloads\\driver\\geckodriver.exe");
      driver = new FirefoxDriver();
      break;
  case "ie": 
      System.setProperty("webdriver.ie.driver", "C:\\Users\\Intellipaat-Team\\Downloads\\driver\\ieexploredriver.exe");
      driver = new InternetExplorerDriver();
      break;
  }
  driver.get(URL);
  System.out.println(URL);

}

}

The code should look like the image below:

sele8.png

Step 7: Run the code from the parameterization.xml file.

  1. Right-click on the screen.
  2. Click on Run As.
  3. After that, click on 1 TestNG Suite.

Step 8: The driver will communicate with the chrome browser and navigates to google.com

Step 9: Return to the Eclipse IDE

  1. Click on the Design tab.
  2. You can see the link it displays on the console.
  3. The result says the total number of tests run were one, and failed tests were none, so does the skipped tests.

Step 10: To see the index.html file which is created by the TestNG framework for reporting purpose, follow the below steps:

  1. Right-click on the project name.
  2. Click on Refresh.

Step 11: You will see the test-output folder populated under your package.

Step 12:

  1. Click on the test-output folder.
  2. Right-click on index.html file.
  3. Click on Open.

Step 13: You will be able to see the following screen.

  1. Click on the show button(hide in the image), you will see the test method that got executed successfully.
  2. You can see the parameters’ value that was passed to the String variables “browser” and “URL” in the parameterization.xml file, which was “chrome” and “google.com” respectively.

sele11.png

This brings us to the end of this article. In this article, we learned about what Parameterization in TestNG is using Selenium and we learned how to perform parameterization in the Eclipse IDE through a demonstrated hands-on example. I hope this article was informative. If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training course.