Read attributes at test and suite level

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Read attributes at test and suite level

Venkatesh Ganesan

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Read attributes at test and suite level

Krishnan Mahadevan

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/

From: <[hidden email]> on behalf of Venkatesh Ganesan <[hidden email]>
Reply-To: <[hidden email]>
Date: Wednesday, March 28, 2018 at 7:30 PM
To: testng-users <[hidden email]>
Subject: [testng-users] Read attributes at test and suite level

 

 

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

--
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
[hidden email].
To post to this group, send email to
[hidden email].
Visit this group at
https://groups.google.com/group/testng-users.
For more options, visit
https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Read attributes at test and suite level

Venkatesh Ganesan
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


On Wednesday, March 28, 2018 at 9:07:44 PM UTC+5:30, Krishnan Mahadevan wrote:

Here’s how you do it.

 

Assuming that you have an xml like below:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Ftestng.org%2Ftestng-1.0.dtd\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFHihXzQ1YlwcxcDehnw97TuniFLA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Ftestng.org%2Ftestng-1.0.dtd\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFHihXzQ1YlwcxcDehnw97TuniFLA&#39;;return true;">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 @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/

My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

From: <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3fgxFe91AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...> on behalf of Venkatesh Ganesan <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3fgxFe91AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">venkatesh...@...>
Reply-To: <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3fgxFe91AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...>
Date: Wednesday, March 28, 2018 at 7:30 PM
To: testng-users <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3fgxFe91AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...>
Subject: [testng-users] Read attributes at test and suite level

 

 

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

--
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
<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3fgxFe91AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to
<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3fgxFe91AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users@....
Visit this group at
<a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit
<a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Read attributes at test and suite level

Krishnan Mahadevan

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: <[hidden email]>
Date: Thursday, March 29, 2018 at 5:49 PM
To: testng-users <[hidden email]>
Subject: Re: [testng-users] Read attributes at test and suite level

 

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



On Wednesday, March 28, 2018 at 9:07:44 PM UTC+5:30, Krishnan Mahadevan wrote:

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/

From: <<a href="javascript:" target="_blank">testng...@...> on behalf of Venkatesh Ganesan <<a href="javascript:" target="_blank">venkatesh...@...>
Reply-To: <
<a href="javascript:" target="_blank">testng...@...>
Date: Wednesday, March 28, 2018 at 7:30 PM
To: testng-users <
<a href="javascript:" target="_blank">testng...@...>
Subject: [testng-users] Read attributes at test and suite level

 

 

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

--
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
<a href="javascript:" target="_blank">testng-users+unsubscribe@....
To post to this group, send email to
<a href="javascript:" target="_blank">testng-users@....
Visit this group at
https://groups.google.com/group/testng-users.
For more options, visit
https://groups.google.com/d/optout.

--
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
[hidden email].
To post to this group, send email to
[hidden email].
Visit this group at
https://groups.google.com/group/testng-users.
For more options, visit
https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Read attributes at test and suite level

Venkatesh Ganesan
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

On Friday, March 30, 2018 at 11:26:44 PM UTC+5:30, Krishnan Mahadevan wrote:

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 <a href="https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2Fsound%2FSPI-intro.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFqznHq32RuRGK4rOEaSorPlnz1gQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2Fsound%2FSPI-intro.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFqznHq32RuRGK4rOEaSorPlnz1gQ&#39;;return true;">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 @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/

My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

Reply-To: <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Yqk80q0aAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...>
Date: Thursday, March 29, 2018 at 5:49 PM
To: testng-users <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Yqk80q0aAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...>
Subject: Re: [testng-users] Read attributes at test and suite level

 

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



On Wednesday, March 28, 2018 at 9:07:44 PM UTC+5:30, Krishnan Mahadevan wrote:

Here’s how you do it.

 

Assuming that you have an xml like below:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Ftestng.org%2Ftestng-1.0.dtd\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFHihXzQ1YlwcxcDehnw97TuniFLA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Ftestng.org%2Ftestng-1.0.dtd\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFHihXzQ1YlwcxcDehnw97TuniFLA&#39;;return true;">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 @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/

My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

From: <[hidden email]> on behalf of Venkatesh Ganesan <[hidden email]>
Reply-To: <
[hidden email]>
Date: Wednesday, March 28, 2018 at 7:30 PM
To: testng-users <
[hidden email]>
Subject: [testng-users] Read attributes at test and suite level

 

 

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

--
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
[hidden email].
Visit this group at
<a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit
<a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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
<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Yqk80q0aAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to
<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Yqk80q0aAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users@....
Visit this group at
<a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit
<a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.