TestNG DataProvider Read Test Data From Xml File

DataProvider is used to pass numerous data sets to a test method in TestNG. This can improve test efficiency greatly. In this article, we will tell you how to read data from an XML file in testNG dataprovider. We will also tell you how to easily maintain and edit an XML file. Whatever data source you use, you should first make sure that the data type and size in the dataprovider returned array should match the type and size of the parameter of the test method in TestNG.  

1. Read Testing Data From Xml File.

1.1 Generate Test Data Xml File.

People always think XML file is not good for edit. But we found the below method to make it easy to edit an XML file.

  1. Download OpenOffice and install it. It is open-source and free.
  2. Choose Spreadsheet when open OpenOffice for the first time.
  3. Input the test data in the spreadsheet, you can input any number of rows, and each row contains 2 columns A & B. Column A’s value is an email address, column B’s value is the email password.
  4. Save the file as userAccount.ods. Please note do not export it to XML directly, because maybe that action can cause your PC hanged.
  5. Because .ods is just zip format then you can change the file extension to zip. Then the file name is changed to userAccount.zip. Unzip it to a local folder. There is a content.xml in that folder. It is just where the test data you entered exists.

1.2 Read Test Data From content.xml.

We will read test data from the above content.xml in the below example. We use TestNG in the below example also.


  1. Change the source extension from .ods to .zip.
  2. Get the file content.xml in the above zip file.
  3. Parse content.xml and return test data in it.
  4. Rename extension from .zip back to .ods.
  5. Because there is a bug in the current website, so we have to split a long java method into two parts as below. Sorry for the inconvenience.
      /* Read from content.xml. */
      public Object[][] xmlFileDataProvider() throws IOException {
    	  Object[][] retDataArr = null;
    	  /* Get source .ods document and change it's extension to a .zip.*/
    	  String srcFilePath = "C:/WorkSpace/dev2qa.com/Doc/Passing JUnit DataProvider Test Data From Xml Or Excel sheet/userAccount.ods";
    	  int dotLastIdx = srcFilePath.lastIndexOf(".");
    	  String srcZipFilePath = srcFilePath.substring(0, dotLastIdx) + ".zip";
    	  File srcFile = new File(srcFilePath);
    	  srcFile.renameTo(new File(srcZipFilePath));
    	  /* Read the content.xml. */
    	  ZipFile zFile = new ZipFile(srcZipFilePath);
    	  Enumeration<? extends ZipEntry> zEntries = zFile.entries();
    	        ZipEntry zEntry = zEntries.nextElement();
    	        String zEntryName = zEntry.getName();
    	        	 InputStream stream = zFile.getInputStream(zEntry);
    	        	 /* Parse the stream and return the test data then break.*/
    	        	 retDataArr = this.parseXmlStream(stream);
    	  /* Rename the .zip document back to original .ods document . */
    	  File srcZipFile = new File(srcZipFilePath);
    	  srcZipFile.renameTo(new File(srcFilePath));
    	  return retDataArr;
      /* Parse the test data from content.xml input stream. */
      private Object[][] parseXmlStream(InputStream xmlInputStream)
    	  List<List<String>> testDataList = new ArrayList<List<String>>();
    			  DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
    			  DocumentBuilder domBuilder = domBuilderFactory.newDocumentBuilder();
    			  Document document = domBuilder.parse(xmlInputStream);
    			  /* Get table list. */
    			  NodeList nodeList = document.getElementsByTagName("table:table");
    				  int nlSize = nodeList.getLength();
    				  for(int i=0;i<nlSize;i++)
    					  Node node = nodeList.item(i);
    					  NamedNodeMap nodeMap = node.getAttributes();
    					  String tableName = nodeMap.getNamedItem("table:name").getNodeValue();
    					  //System.out.println("tableName = " + tableName);
    					  /* Get first spread Sheet where we store test data.*/
    						  NodeList cNodeList = node.getChildNodes();
    							  int cNodeListSize = cNodeList.getLength();
    							  for(int j=0;j<cNodeListSize;j++)
    								  Node cNode = cNodeList.item(j);
    								  String cNodeName = cNode.getNodeName();
    								  /* Get each row in Sheet1. */
    									  NodeList cellNodeList = cNode.getChildNodes();
    										  int cellNodeListSize = cellNodeList.getLength();
    										  if(cellNodeListSize >= 2)
    											  Node userEmailNode = cellNodeList.item(0);
    											  Node passwordNode = cellNodeList.item(1);
    											  /* Add each row value to the list. */
    											  List rowList = new ArrayList<String>();
    		  }catch(Exception ex)
    	  /* Translate the list to an array.*/
    	  int listSize = testDataList.size();
    	  String[][] retDataArr = new String[listSize][2];
    	  for(int i=0;i<listSize;i++)
    		  List<String> rowList = testDataList.get(i);
    		  retDataArr[i][0] = rowList.get(0);
    		  retDataArr[i][1] = rowList.get(1);
    	  return retDataArr;

2. Use DataProvider  In TestNG.

We will use testNG dataprovider in a test method testXmlDataProvider(String userName, String password). Please note the input parameter, the type and number of parameters should match the dataprovider returned array.

Test Case Steps:

  1. Open a local login.html.
  2. Input the userName and password in the login form.
  3. Click submit button.
@Test(dataProvider = "xmlFileDataProvider")
public void testXmlDataProvider(String userName, String password) throws InterruptedException {
          System.out.println("Start test with data, userName = " + userName + " , password = " + password);
	  By userNameById = By.id("userName");
	  By passwordById = By.id("password");
	  By submitById = By.id("submitBtn");

The Firefox webdriver object will be created in the @BeforeClass method and destroyed in the @AfterClass method.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.