<?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 -->
我正在尝试编辑此XML,因此我可以更改2个参数,resultLocation和runType。到目前为止,我已经尝试过使用DocumentBuilder的一些东西,但是我无法通过它来查找属性。这是我到目前为止尝试过的代码。第一部分有效,我可以进入测试元素。
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();
}
}
}
答案 0 :(得分:1)
看起来像某种类型的家庭作业,所以不会给你完整的来源,但在片段之下会处理你想要改变的第一个参数。它比您尝试的解决方案更清晰,更能抵抗XML结构的变化。您可以使用它为第二个参数执行相同的操作。
XPath xp = XPathFactory.newInstance().newXPath();
Element node = (Element) xp.evaluate("//test/parameter[@name='resultLocation']",
doc, XPathConstants.NODE);
node.setAttribute("name", "new attribute value");