<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test Suite Name" parallel="none">
<listeners>
<listener class-name="testng.MyListener"></listener>
</listeners>
<test name="Test localhost">
<parameter name="Browser" value="chrome"/> <!-- chrome or firefox or IE or Android Native-->
<parameter name="url" value='' /> <!-- URL of the webpage you want to test -->
<parameter name="CSV" value="data.csv"/> <!-- Location of the CSV file that you want to run tests with -->
<parameter name="resultLocation" value="C:\Users\xvdf\Desktop\testmap"/> <!-- TestNG will create two folders in this location, screenshots and test-output-datestamp -->
<parameter name="runType" value="baseline" /> <!-- actual or baseline . baseline = first run. actual is second run, to do a compare for example -->
<classes>
<class name="testng.runTest"></class>
</classes>
</test> <!-- Test CAN YOU SEE THIS?-->
</suite> <!-- Suite end of suite -->
I am trying to edit this XML so I can change 2 parameters, resultLocation and runType. So far I have tried a few things with the DocumentBuilder, but I cant see to get it to find the attributes. This is the code I have tried so far. The first part works, I can get to the test element.
package testng;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ModifyXMLFile {
public static void main(String argv[]) {
try {
String filepath = "testng.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = null;
try {
doc = docBuilder.parse(filepath);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the root element
Node suite = doc.getFirstChild();
Node test = suite.getFirstChild().getNextSibling().getNextSibling().getNextSibling();
System.out.println(test);
NamedNodeMap testName = test.getAttributes();
Node nsdf = testName.getNamedItem("name");
if(nsdf.getNodeValue().equals("Test localhost")){
System.out.println("Found Element!");
}//This actually works.
Node loc = test.getFirstChild().getNextSibling().getNextSibling();
System.out.println(loc);
NamedNodeMap sdf = loc.getAttributes();
Node as = sdf.getNamedItem("Browser");
if(as.getNodeValue().equals("chrome")){
System.out.println("Browser found!");
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
}
}
attribute? what you are expecting?