Detailed explanation of data-driven examples using XML as data source in java and testng
The function of testng is very powerful. You can use @DataProvider to be used as data driver. The data source file can be EXCEL, XML, YAML, or even TXT text. Take XML as an example here:
Note: @DataProvider's return value type can only be Object[][] and Iterator<Object>[]
TestData.xml:
<?xml version="1.0" encoding="UTF-8"?><data> <testmethod1> <input>1</input> <button>2</button> </testmethod1> <testmethod1> <input>3</input> <button>4</button> </testmethod1> <testmethod2> <input>3</input> <button>4</button> </testmethod> <button>4</button> </testmethod4></data>
Use DOM4J to parse XML, ParserXml.java file:
package com.test;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class ParserXml { public List parser3Xml(String fileName) { File inputXml = new File(fileName); List list=new ArrayList(); int count = 1; SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(inputXml); Element employees = document.getRootElement(); for (Iterator i = employees.elementIterator(); i.hasNext();) { Element employee = (Element) i.next(); Map map = new HashMap(); Map tempMap = new HashMap(); for (Iterator j = employee.elementIterator(); j.hasNext();) { Element node = (Element) j.next(); tempMap.put(node.getName(), node.getText()); } map.put(employee.getName(), tempMap); list.add(map); } } catch (DocumentException e) { System.out.println(e.getMessage()); } return list; } }Then the parsed list is converted into Object[][] data and combined in @DataProvider.
TestData.java file:
package com.test;import java.io.File;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.testng.annotations.DataProvider;public class TestData { private List l; public TestData() { this.getXmlData(); } public void getXmlData() { ParserXml p = new ParserXml(); l = p.parser3Xml(new File("src/com/test/TestData.xml").getAbsolutePath()); } @DataProvider public Object[][] providerMethod(Method method){ List<Map<String, String>> result = new ArrayList<Map<String, String>>(); for (int i = 0; i < l.size(); i++) { Map m = (Map) l.get(i); if(m.containsKey(method.getName())){ Map<String, String> dm = (Map<String, String>) m.get(method.getName()); result.add(dm); } } Object[][] files = new Object[result.size()][]; for(int i=0; i<result.size(); i++){ files[i] = new Object[]{result.get(i)}; } return files; } }Then test the test file:
TestDataProvider.java file:
package com.test;import java.util.Map;import org.testng.annotations.*;public class TestDataProvider extends TestData { @Test(dataProvider="providerMethod") public void testmethod1(Map<?, ?> param){ System.out.println("method1 received:"+param.get("input")); } @Test(dataProvider="providerMethod") public void testmethod2(Map<?, ?> param){ System.out.println("method2 received:"+param.get("input")); } @Test(dataProvider="providerMethod") public void testmethod3(Map<?, ?> param){ System.out.println("method3 received:"+param.get("input")); } @Test public void testmethod4(){ System.out.println("method4 received:4"); }}Let's go back and analyze the XML file. There are two testmethod1 nodes, testmethod2, testmethod3, and testmethod4 nodes. In the TestDataProvider.java file, four test functions are defined: testmethod1, testmethod2, testmethod3, and testmethod4. Testmethod4 does not use dataProvider, so the end result should be that testmethod1 runs twice, testmethod2, testmethod3, and testmethod4 runs once, and the result is as follows:
method1 received:1method1 received:3method2 received:3method3 received:3method4 received:4PASSED: testmethod1({input=1, button=2})PASSED: testmethod1({input=3, button=4})PASSED: testmethod2({input=3, button=4})PASSED: testmethod3({input=3, button=4})PASSED: testmethod4============================================================================================================================================================================ In other words, in this way, you only need to write the test function first and then define the data in the XML file, and you can control whether the function runs, the number of runs and the data that runs.
OK, Let's try...
Thank you for reading, I hope it can help you. Thank you for your support for this site!