如何在特定测试中的运行时添加参数

时间:2018-08-13 05:39:39

标签: java selenium testng

suite.xml文件中有多个测试标签,如下所示:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose="1" name="UK-Suite"
    skipfailedinvocationcounts="false" junit="false" parallel="methods"
    data-provider-thread-count="2" annotations="JDK">

    <test verbose="2" name="AuthTxn-Tests"
        annotations="JDK" preserve-order="true">
        <parameter name="testDataFileXLS"
            value="src/test/resources/testdata/TxnTestData/AdjustTestCases.xlsx" />
        <parameter name="declineDataFileXLS"
            value="src/test/resources/testdata/TxnTestData/DoubeTests.xlsx" />
        <parameter name="token"
            value="src/test/resources/testdata/TestDefaults.json" />
        <classes>
            <class name="com.amazon.test.e2e.token.PPTest">
            </class>
        </classes>
    </test>
    <test verbose="2" name="LUTON-AuthTxn-Tests" annotations="JDK"
        preserve-order="true">
        <parameter name="testDataFileXLS"
            value="src/test/resources/testdata/TxnTestData/AuthTxnTestCases.xlsx" />
        <parameter name="declineDataFileXLS"
            value="src/test/resources/testdata/TxnTestData/DoubeTests.xlsx" />
        <parameter name="token"
            value="src/test/resources/testdata/TestDefaults.json" />
        <classes>
            <class name="com.amazon.test.e2e.token.PPTest">
            </class>
        </classes>
    </test>
    </suite>

我只想在运行时添加<parameter name="OS" value="linux"/>,然后再调用@Test方法进行特定测试。所有测试标签都有一个通用类,但是它将根据测试数据执行不同的流执行。我只想在运行时为特定的测试名称添加参数,例如“ AuthTxn-Tests”。

任何线索?

2 个答案:

答案 0 :(得分:0)

您可以尝试@Optional参见javadoc

TestNG将使用您的默认值初始化该参数,如果未指定任何内容,则此参数将为null,并且测试不会因缺少参数而失败。

答案 1 :(得分:0)

使用this链接,我编写了一些代码,可用于在xml文件中动态添加<parameter>。它将在第一个<parameter>标签内的<classes>之前添加<test>元素-

try {
        String filepath = "path to your xml";
        DocumentBuilderFactory docFactory = 
        DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);

        Node test_tag = doc.getElementsByTagName("test").item(0);
        String name = "OS";
        String value = "Linux";
        NamedNodeMap attr = test_tag.getAttributes();

        // append a new node to test
        Element parameter = doc.createElement("parameter");
        parameter.setAttribute("name", name);
        parameter.setAttribute("value", value);
        test_tag.appendChild(parameter);
        Node classes = doc.getElementsByTagName("classes").item(0);
        test_tag.insertBefore(parameter, classes);


        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filepath));
        transformer.transform(source, result);

        System.out.println("Done");

       } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
       } catch (TransformerException tfe) {
        tfe.printStackTrace();
       } catch (IOException ioe) {
        ioe.printStackTrace();
       } catch (SAXException sae) {
        sae.printStackTrace();
       }

注意:您需要添加followinf导入-

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;

}

编辑- 这段代码会重写您的xml文件,之后您需要以编程方式执行已编辑的文件,以执行最终的xml,例如this