Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 57: DataProvider in TestNG – Passing External Data File Name to DataProvider Method Using TestNG xml

Posted on 01/14/2025 By admin

In last post, we have learnt how can we make parameterized DataProvider method in TestNG to provide test data to multiple Test methods from single DataProvider method. Let’s make it more customised.

Suppose you need to read data from external file like Excel, properties file etc in a DataProvider method. The name of external file is not constant and keep changing or you may need to pass different data sheet for execution. If external data files names are fixed , you can hard code it within script by following approach discussed in previous post. But if external data files name are not constant, you should not hard code within script.

To achieve this, we will mix concept of parameter from testng xml and DataProvider. Let’s learn it.

A DataProvider method can take an attribute of type ITestContext which can be used to read parameter value from testng xml. We will pass required data file name from testng xml to DataProvider method. If data file name changes, we can update it in testng xml. No need to make any changes in DataProvider methods.

Java Code:

package DataProvider;

import java.lang.reflect.Method;

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class CustomDataProviderWithParameter {

        // A data provider method which will return a required data based on Test method
        // name
        @DataProvider(name = "DataContainer")
        public static Object[] myDataProvider(Method m, ITestContext iTestContext) {
                
                // Getting the method name
                String methodName = m.getName();
                
                
                switch(methodName)
                {
                case "A" :
                {
                        // Getting parameter value from testng.xml to get data file name for desired method
                        String dataFilename= iTestContext.getCurrentXmlTest().getParameter("DataFileforA");
                        
                        // I am returning data file name here but you can read data file and return proper data 
                        return new String[] {dataFilename};
                }
                
                case "B" :
                {
                        String dataFilename= iTestContext.getCurrentXmlTest().getParameter("DataFileforB");
                        return new String[] {dataFilename};
                }
                
                case "C" :
                {
                        String dataFilename= iTestContext.getCurrentXmlTest().getParameter("DataFileforC");
                        return new String[] {dataFilename};
                }
                
                default:
                        return new String[] {"No Data File"};
                }

        }

        // A test method
        @Test(dataProvider = "DataContainer")
        public void A(String dataFilename) {
                System.out.println("DatafileName for method A is " + dataFilename);
        }

        // A test method
        @Test(dataProvider = "DataContainer")
        public void B(String dataFilename) {
                System.out.println("DatafileName for method B is " + dataFilename);
        }

        // A test method
        @Test(dataProvider = "DataContainer")
        public void C(String dataFilename) {
                System.out.println("DatafileName for method C is " + dataFilename);
        }
}

TestNG xml:

[xml]

[/xml]

Uncategorized

Post navigation

Previous Post: Introduction Of Page Object Model In Selenium WebDriver: Advantages And disadvatages
Next Post: @JsonInclude in jackson

Related Posts

TestNG Tutorials 59: DataProvider in TestNG – Running DataProvider Method in Parallel – Parallel DataProvider Method Uncategorized
June 2, 2018 – Make Selenium Easy Uncategorized
Java Program To Find Closest Value Of A Given Number in Unsorted Array Uncategorized
DownloadLocationChrome – Make Selenium Easy Uncategorized
Git Tutorial 30 – Understand Soft, Mixed And Hard Options Of Git Reset Uncategorized
REST Assured Tutorial 73 – How to ignore node/s for JSON comparison in JSONassert Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark