0

I have multiple test tags in suite.xml file as like below:

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

I just want to add <parameter name="OS" value="linux"/> at run time before invoke a @Test method to specific test. There is a common class for all the test tag but It will do different flow execution based upon the test data. I just want to add parameter at runtime for specific test name like "AuthTxn-Tests".

Any leads?

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84

2 Answers2

0

You may try @Optional see javadoc

TestNG will initialize such parameter with your default value or null if nothing specified and test will not fail due to missing parameters.

RocketRaccoon
  • 2,559
  • 1
  • 21
  • 30
0

Using this link, I wrote some code which you can use to add a <parameter> dynamically in your xml file. It will add a <parameter> element before your <classes> inside your first <test> tag -

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

Note: You need to add followinf imports -

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;

}

EDIT - This code only rewrites your xml file, you need to programmatically execute your edited file after this to execute the final xml something like this.

Shivam Mishra
  • 1,731
  • 2
  • 11
  • 29
  • It won't work, test method should also declare this parameters. – RocketRaccoon Aug 13 '18 at 07:53
  • @RocketRaccoon sorry I didn't get your concern, could you please elaborate? – Shivam Mishra Aug 13 '18 at 08:20
  • 1
    Your code just modify xml with additional nodes. Test method should also include these parameters and in case if such parameters declare only for one test tag in xml same test method should handle them somehow when executing from another test tag. So adding parameters in xml it is just part of solution. – RocketRaccoon Aug 13 '18 at 08:31
  • @RocketRaccoon ohh yea right. I have edited the answer. This solution is only to rewrite an existing xml file. – Shivam Mishra Aug 13 '18 at 08:56