1 回答
TA贡献1820条经验 获得超9个赞
想通了这个问题。首先,套件文件路径的名称需要是“drivers.xml”而不是“./drivers.xml”。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All-Tests">
<suite-files>
<suite-file path="drivers.xml"></suite-file>
</suite-files>
</suite>
其次,我的 XmlMaker 类没有设置 Xml 文件的创建,例如 Suite 标签需要一个名称,声明 doctype,以及在构建我的自定义 xml 文件时我必须对如何做 Element 对象进行一些调整。
public class XMLMaker {
public DocumentBuilderFactory docDriverSetup;
public DocumentBuilder driverSetup;
public int connectedDevices = 0;
Document doc;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
public void setupDriverXMLFile(List <AppiumDriver<MobileElement>> driverList) {
System.out.println("List size: "+ driverList.size());
try {
docDriverSetup = DocumentBuilderFactory.newInstance();
driverSetup = docDriverSetup.newDocumentBuilder();
doc = driverSetup.newDocument();
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://testng.org/testng-1.0.dtd");
Element suiteElement = doc.createElement("suite");
suiteElement.setAttribute("name", "All-tests");
for(AppiumDriver<MobileElement> driver: driverList) {
Element rootElement = doc.createElement("test");
suiteElement.appendChild(rootElement);
rootElement.setAttribute("name", (String) driver.getCapabilities().getCapability("deviceName"));
Element deviceNameEle = doc.createElement("parameter");
deviceNameEle.setAttribute("name", "deviceName");
deviceNameEle.setAttribute("value", (String) driver.getCapabilities().getCapability("deviceName"));
rootElement.appendChild(deviceNameEle);
Element platformEle = doc.createElement("parameter");
platformEle.setAttribute("name", "platform");
platformEle.setAttribute("value", driver.getPlatformName()+"");
rootElement.appendChild(platformEle);
Element udidEle = doc.createElement("parameter");
udidEle.setAttribute("name", "udid");
udidEle.setAttribute("value", (String)driver.getCapabilities().getCapability("udid"));
rootElement.appendChild(udidEle);
Element urlPort = doc.createElement("parameter");
urlPort.setAttribute("name", "URL");
urlPort.setAttribute("value", (String)driver.getCapabilities().getCapability("appiumURL"));
rootElement.appendChild(urlPort);
Element devicePort = doc.createElement("parameter");
if((driver.getPlatformName()+"").
toLowerCase().contains("android")) {
devicePort.setAttribute("name", "port");
devicePort.setAttribute("value", driver.getCapabilities().getCapability("systemPort")+"");
}
if ((driver.getPlatformName()+"").
toLowerCase().contains("ios")) {
devicePort.setAttribute("name", "port");
devicePort.setAttribute("value", (String)driver.getCapabilities().getCapability("wdaLocalPort"));
}
rootElement.appendChild(devicePort);
Element packages = doc.createElement("packages");
rootElement.appendChild(packages);
Element packageName = doc.createElement("package");
packageName.setAttribute("name", "BaseTest");
packages.appendChild(packageName);
connectedDevices++;
}
suiteElement.setAttribute("parallel", "tests");
suiteElement.setAttribute("thread-count", connectedDevices+"");
doc.appendChild(suiteElement);
} catch(ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
}
添加回答
举报