Read attributes at test and suite level

451 views
Skip to first unread message

Venkatesh Ganesan

unread,
Mar 28, 2018, 10:00:42 AM3/28/18
to testng-users

Hi, 
I am new to testNG and I have a testng xml file which has a 
custom attribute defined at the suite level as follows: 
<suite name="Teq Test" path="C:\Users\">
<listeners>
<listener class-name="com.eci.raft.apollo.listeners.TestListener"></listener>
</listeners>
<parameter name="" value="">&parent;</parameter>
<test name="TeqTest" time-out="123" invocationCount="10"  >
<classes>
<class name="com.eci.raft.apollo.tests.alarm.BasicTest">
</class>

</classes>

</test>

</suite>

I would like to know if there is a way I can read the value of the 
path attribute defined at suite level and invocationCount defined at test level 

Thanks and regards,

Venkatesh Ganesan

Krishnan Mahadevan

unread,
Mar 28, 2018, 11:37:44 AM3/28/18
to testng...@googlegroups.com

Here’s how you do it.

 

Assuming that you have an xml like below:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="xml_suite" verbose="2">
    <
listeners>
        <
listener class-name="com.rationaleemotions.googleforums.groupdeps.xml.XmlListener"/>
    </
listeners>
    <
parameter name="suite_level" value="Cedric"/>
    <
test name="xml_test">
        <
parameter name="suite_level" value="Julien"/>
        <
classes>
            <
class name="com.rationaleemotions.googleforums.groupdeps.xml.XmlTestClassExample"/>
        </
classes>
    </
test>
</
suite>

 

Here’s a listener that retrieves the <suite> level and <test> level parameters.

 

import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.ITestContext;
import org.testng.TestListenerAdapter;

public class XmlListener extends TestListenerAdapter implements ISuiteListener {
   
@Override
   
public void onStart(ISuite suite) {
        System.
err.println("Suite Level parameter : " + suite.getParameter("suite_level"));
    }

   
@Override
   
public void onFinish(ISuite suite) {
    }

   
@Override
   
public void onStart(ITestContext context) {
        System.
err.println("Test Level parameter : " + context.getCurrentXmlTest().getLocalParameters().get("suite_level"));
    }
}

 

And, if  you want to retrieve the values from within a @Test method, here’s how you do it.

 

import org.testng.ITestContext;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class XmlTestClassExample {
   
@Test
   
public void testMethod() {
        ITestContext context = Reporter.getCurrentTestResult().getTestContext();
        System.
err.println("<test> level parameter value : " + context.getCurrentXmlTest().getLocalParameters().get("suite_level"));
        System.
err.println("<suite> level parameter value : " + context.getSuite().getParameter("suite_level"));
    }
}

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
testng-users...@googlegroups.com.
To post to this group, send email to
testng...@googlegroups.com.
Visit this group at
https://groups.google.com/group/testng-users.
For more options, visit
https://groups.google.com/d/optout.

Venkatesh Ganesan

unread,
Mar 29, 2018, 8:18:59 AM3/29/18
to testng-users
Thank you Krishnan sir for prompt reply.

But actually i want to do a constraint check in the testng xml. 
If we are using as a parameter tag, then we cannot validate the parameter name.
In case of have attributes in the respective tags, we can validate them using DTD file.

However, I see that in TestNGContentHandler class, under method xmlTest(boolean start, Attributes attributes),
these attributes object contains all the attributes that are mentioned in the <test> tag of testng xml.

Here only the required attribute values are set and others are discarded, so is there any provision to access the other attributes which are getting discarded.

Please find my testng.xml as below

<!DOCTYPE suite SYSTEM "./src/main/resources/testng-1.0.dtd" [<!ENTITY parent PUBLIC "xml" "parameters.xml">]>
<suite name="Teq Test" >
<listeners>
<listener class-name="com.eci.raft.apollo.listeners.TestListener2"></listener>
</listeners>
<parameter name="" value="">&parent;</parameter>
<test name="TeqTest" time-out="123" invocationCount="10" dataProvider="dp">
<parameter name="teqId" value="3"></parameter>
<classes>
<class name="com.eci.raft.apollo.tests.alarm.AlarmAttributes">
</class>

<class name="com.eci.raft.apollo.tests.alarm.BasicAlarmsTest">
</class>

</classes>

</test>

</suite>


Thanks and regards,

Venkatesh Ganesan

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.


To post to this group, send email to

Krishnan Mahadevan

unread,
Mar 30, 2018, 1:56:44 PM3/30/18
to testng...@googlegroups.com

Venkatesh,

 

You would have to do quite a lot of work, if you would like to get these custom attributes that you have added, as part of the existing XmlSuite (or) XmlTest classes.

 

But that being said, you should still be able to plug-in your logic, which extracts these values, but stores them in a custom data structure that you are managing.

 

Here’s how you do it.

 

  1. Extend org.testng.xml.TestNGContentHandler wherein you override the method org.testng.xml.TestNGContentHandler#startElement and plug-in the logic of extracting attributes, saving them etc.,
  2. Extend “org.testng.xml.SuiteXmlParser” and duplicate its implementation of org.testng.xml.SuiteXmlParser#parse into your method, but use the class obtained in (1) instead of using TestNGContentHandler
  3. So after you invoke “contentHandler.getSuite()” within the parse() method, you would have formed a proper <suite> object, but at the same time, you would also have access to the Attributes object via your sub-classed variant of TestNGContentHandler
  4. Now to wire in your implementation of (2), create a file META-INF/services/org.testng.xml.ISuiteParser and refer to the class from (2) in this file. This is basically what they call as SPI

 

Hopefully that should help you do what you are trying.

 

PS: I haven’t done this on my own, but am telling you based on what I u’stand reading TestNG code.

 

Hope that helps!

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Reply-To: <testng...@googlegroups.com>
Date: Thursday, March 29, 2018 at 5:49 PM
To: testng-users <testng...@googlegroups.com>
Subject: Re: [testng-users] Read attributes at test and suite level

 

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.


To post to this group, send email to

You received this message because you are subscribed to the Google Groups "testng-users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.


To post to this group, send email to

Venkatesh Ganesan

unread,
Apr 24, 2018, 2:12:36 AM4/24/18
to testng-users
Hi Krishnan,

Thank you for the response. The solution worked fine for me and it was really helpful.
Thanks a lot for your support.

Thanks and regards,

Venkatesh Ganesan

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.


To post to this group, send email to

--
You received this message because you are subscribed to the Google Groups "testng-users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.


To post to this group, send email to

Reply all
Reply to author
Forward
0 new messages