java编辑XML文件工作不正常
我正在使用此脚本编辑XML文件。TestNG使用此XML文件运行测试。它包含有关我要运行的测试的信息。测试运行后,我想用测试结果的位置等更新XML文件
我写这个剧本是为了做我想做的事
public void editXMLFile(String nameOfTest, String resultLoc){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse("testng.xml");
NodeList tests = doc.getElementsByTagName("test");
Element test = null;
for (int i = 0; i < tests.getLength(); i++) {
test = (Element) tests.item(i);
String testNames = test.getAttribute("name");
if (testNames.equals(nameOfTest)) {
//System.out.println("Found element!");
NodeList params = test.getElementsByTagName("parameter");
Element runType = null, baselineLocation = null;
for (int i1 = 0; i1 < params.getLength(); i1++) {
runType = (Element) params.item(i1);
String paramNames = runType.getAttribute("name");
if (paramNames.equalsIgnoreCase("runType")) {
//System.out.println("Found the runType");
if (runType.getAttribute("value").equalsIgnoreCase(
"baseline")) {
for (int j = 0; j < params.getLength(); j++) {
baselineLocation = (Element) params.item(j);
if (baselineLocation.getAttribute("name")
.equalsIgnoreCase("baselineLocation")) {
//System.out.println("Found baselineLocation!");
runType.setAttribute("value", "actual");
baselineLocation.setAttribute("value", resultLoc);
System.out.println("resultLoc = " + resultLoc);
System.out.println("Test name = " + _testName);
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
// Prepare the output file
File file = new File("C:/Users/sfd/Desktop/testng.xml");
StreamResult sr = new StreamResult(file);
Result result = sr;
// Write the DOM document to the file
Transformer xformer = TransformerFactory
.newInstance().newTransformer();
xformer.transform(source, result);
}
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
这是XML文件
<?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 NOT on 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="testdata.csv"/> <!-- Location of the CSV file that you want to run tests with -->
<parameter name="resultLocation" value="C:\Users\sfd\Desktop"/> <!-- TestNG will create two folder in this location, screenshots and test-output-datestamp -->
<parameter name="baselineLocation" value=""/> <!-- Location of the baseline location -->
<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?-->
<test name="Test localhost">
<parameter name="Browser" value="chrome"/> <!-- chrome or firefox or IE or Android Native-->
<parameter name="url" value='' />
<parameter name="CSV" value="testdata.csv"/>
<parameter name="resultLocation" value="C:\Users\sfd\Desktop\testmap"/> <!-- TestNG will create two folder in this location, screenshots and test-output-datestamp -->
<parameter name="baselineLocation" value=""/> <!-- Location of the baseline run. leave value empty. This will be filled in by TestNG itself. -->
<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>
</suite> <!-- Suite end of suite -->
我面临的问题是:;当我在xml文件中只有一个测试时,它工作得很好。但是,当XML文件中有多个测试时,将只更新最后一个测试。两个测试的基线位置都应该更新,而只有最后一个测试得到更新。我认为这种方法在逻辑上有缺陷,但我不确定具体是什么
# 1 楼答案
如果多次调用editXMLFile()以更新多个测试名称,则会出现问题。您正在方法中写入更新的XML文件。由一个测试名称匹配创建的XML将被后续的测试名称匹配条件覆盖
您需要推迟编写XML文件,直到用所有测试名称更新doc对象
重构的一个可能解决方案
希望这能给出一个公平的想法