通过Maven / Testng通过动态自定义TestNG文件运行测试

时间:2018-10-11 03:54:39

标签: java xml maven selenium testng

我正在尝试设置testng.xml,使其指向动态自定义的testng xml文件,以便根据连接的移动设备数量(drivers.xml)来确定需要测试的内容。我有一个Java类,该类创建并包含Appium驱动程序,然后将每个驱动程序的值传递到另一个Java类中,以设置每个驱动程序的参数和值,以便我可以使用@Parameters批注传递所需的驱动程序信息以进行并行测试。

目前,我设置testng.xml文件的方式似乎根本没有运行任何测试。我尝试过在线寻找答案,但是对于TestNG,似乎没有太多从其他自定义xml文件获取值的方法。 (我不确定我的措词是否正确。)是否有人遇到此设置问题?

以下是Xml文件和创建自定义xml文件的Java类:

testNG.xml:

class testcase {
std::fstream& _myfile;
public:
testcase(std::fstream& fileArg):_myfile(fileArg){}

drivers.xml(自定义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>

Xml Maker Java类:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

-<suite name="suite">


-<test name="driver1">

<parameter name="platform" value="Android"/>
<parameter name="udid" value="192.168.174.102:5555"/>

<parameter name="URL" value="http://127.0.0.1:5000/wd/hub"/>


-<parameter name="port" value="8200">

<package name="BaseTest"/>

</parameter>

</test>


-<test name="driver2">

<parameter name="platform" value="Android"/>

<parameter name="udid" value="192.168.174.101:5555"/>

<parameter name="URL" value="http://127.0.0.1:5000/wd/hub"/>


-<parameter name="port" value="2801">

<package name="BaseTest"/>

</parameter>

</test>

</suite>

1 个答案:

答案 0 :(得分:0)

找出问题。首先,套件文件路径的名称必须是“ 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,以及在构造自定义项时如何对Element对象进行一些调整xml文件。

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();
        } 

    }