简体   繁体   中英

Run Tests through a Dynamic Custom TestNG File through Maven/Testng

I'm trying to setup my testng.xml in way where it points to a dynamically customized testng xml file to determine what needs to be tested based on how many mobile devices are connected, (drivers.xml). I have a Java Class the creates and contains Appium Drivers, then pass those values for each driver into another Java class to setup parameters and values for each driver, so that I can use @Parameters annotation to pass the required driver info for parallel testing.

Currently the way I have testng.xml file setup doesn't seem to run any tests at all. I've tried looking for answers online, but for TestNG, there doesn't seem to be much of getting values from other custom xml files. (I'm not sure if I'm phrasing my question correctly or not.) Has anyone encountered problems for this setup?

Here are the Xml files and the Java class that creates the custom xml file:

testNG.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>

drivers.xml (custom xml file):

<?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>

Xml Maker Java Class:

public class XMLMaker {

    private boolean docTypeDeclared = false;

    public DocumentBuilderFactory docDriverSetup;

    public DocumentBuilder driverSetup;

    public int connectedDevices = 0;

    Document doc;

    TransformerFactory transformerFactory;

    Transformer transformer;

    public void setupDriverXMLFile(List <AppiumDriver<MobileElement>> driverList) {

        try {

            docDriverSetup = DocumentBuilderFactory.newInstance();

            driverSetup = docDriverSetup.newDocumentBuilder();

            doc = driverSetup.newDocument();


            transformerFactory = TransformerFactory.newInstance();
            transformer = transformerFactory.newTransformer();

            if(docTypeDeclared == false) {
                DOMImplementation domImpl = doc.getImplementation();
                DocumentType docType = domImpl.createDocumentType("suite", "","http://testng.org/testng-1.0.dtd");
                doc.appendChild(docType);
                docTypeDeclared = true;
            }

            Element suiteElement = doc.createElement("suite");


            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", (String)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 packageName = doc.createElement("package");
                packageName.setAttribute("name", "BaseTest");
                devicePort.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();
        } 

    }


    public void createDriverFile() throws TransformerConfigurationException {
        transformerFactory = TransformerFactory.newInstance();
        transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("./drivers.xml"));

        try {
            transformer.transform(source, result);

            System.out.println("File Updated");
        } catch (TransformerException e) {
            e.printStackTrace();
        }

    }

}

Figured out the Issue. First, the name for the Suite File path needed to be "drivers.xml" instead "./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>

Second, my XmlMaker Class wasn't setting up the Xml file creation, such as a name is required with the Suite tags, declaring doctype, plus how I had to do some tweaks with how to do Element objects when constructing my custom xml file.

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM