In the last post we tried to run multiple Appium test using TestNG suite by manually creating the TestNG.xml file. In this post we are going to go a little further to create a custom library for
Reading the method names from excel sheet which is flagged to run
Dynamically creating xml file
Running the test using testng.xml file using TestNG API
This way we can reduce the painful manual effort of creating custom TestNG xml file rather we can control the same via a single excel sheet for running a group of methods.
Please find below the complete video of the above discussion
Please find below the complete code for the above video
Custom TestNG Library Code
public static void createXml() throws Exception {
try {
// Initialization of drivers
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document document = documentBuilder.newDocument();
String xlFilePath = null;
xlFilePath = "Resource\\Initial.xls";
ExcelSheetLibrary lib = new ExcelSheetLibrary(xlFilePath);
// Get the flagged cells with value = "Y" from excel file
lib.GetFlaggedMethods("Flag");
// Get the number of parameter to be created in XML
int totalnoofelementsflaggedtorun = lib.flaggedMethod.size();
// Type the suite tag element in the XML file
Element rootElementsuite = document.createElement("suite");
// Type the parameter set of lines in the XML file
for (int elementcounter = 1; elementcounter <= totalnoofelementsflaggedtorun; elementcounter++) {
Element rootElementparameter = document
.createElement("parameter");
String[] flagElement = lib.flaggedMethod.get(elementcounter)
.toString().split(";");
rootElementparameter.setAttribute("name", flagElement[0]);
rootElementparameter.setAttribute("value", flagElement[1]);
rootElementsuite.appendChild(rootElementparameter);
}
// Type the root elements in the XML file
Element rootElementtest = document.createElement("test");
Element rootElementClass = document.createElement("classes");
Element childelementClass = document.createElement("class");
// Assign attribute to the root elements
childelementClass.setAttribute("name", "com.example.CalcAppTest");
Element rootElementgroups = document.createElement("methods");
// Assign attribute to the root elements
rootElementsuite.setAttribute("name", "SimpleAppiumFramework");
rootElementtest.setAttribute("name", "testing");
// Append values to the root elements
rootElementsuite.appendChild(rootElementtest);
rootElementtest.appendChild(rootElementClass);
rootElementClass.appendChild(childelementClass);
childelementClass.appendChild(rootElementgroups);
// rootElementgroups.appendChild(rootElementrun);
document.appendChild(rootElementsuite);
// Obtain the column value flag = "Y" in a loop
for (int elementcounter = 1; elementcounter <= totalnoofelementsflaggedtorun; elementcounter++) {
String element = "include";
Element em = document.createElement(element);
String[] flagElement = lib.flaggedMethod.get(elementcounter)
.toString().split(";");
em.setAttribute("name", flagElement[0]);
rootElementgroups.appendChild(em);
}
// Generate the file.
FileWriter fstream = new FileWriter("Resource\\testng.xml");
BufferedWriter out = new BufferedWriter(fstream);
// Generate the required XML output file
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
// Prints all the Generated Xml code in the File object
StreamResult result = new StreamResult(fstream);
transformer.transform(source, result);
// close the generated file.
out.close();
} catch (DOMException e) {
e.printStackTrace();
}
}
/**
* This method runs the XML suite file dynamically
*
**/
public static void autoRunXml() {
// Create a list
List files = new ArrayList();
// Add the required xml files to the list
files.add("Resource\\testNG.xml");
// create object for TestNG
TestNG tng = new TestNG();
// Add the list of files to create a suite
tng.setTestSuites(files);
// Run the suite
tng.run();
}
Code to Read Flagged methods
public static Hashtable GetFlaggedMethods(String ColumnName) {
try {
// Load all the dictionary
ColumnDictionary();
int methodcount = 1; // keycount flag will track of
// method count
for (int row = 0; row < RowCount(); row++) {
if (ReadCell(ColumnName, row).equals("Y")) {
// put method keycount and the method name
flaggedMethod.put(methodcount, ReadCell("MethodName", row)
+ ";" + ReadCell("ExcelSheetName", row));
methodcount++;
}
}
} catch (Exception e) {
}
return flaggedMethod;
}
Thanks for reading the post and watching the video !!!
Please leave your comments and let me know if there is anything I should update in this post.
Thanks,
Karthik KK