Accessing values in annotation of a test in a different class

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

Accessing values in annotation of a test in a different class

praveen133t

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

--
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: Accessing values in annotation of a test in a different class

⇜Krishnan Mahadevan⇝
Praveen,

Why not just invoke : Reporter.getCurrentTestResult().getMethod()

getCurentTestResult() when invoked always guarantees that it will return back the corresponding ITestResult object of the current TestNG method. Since your startInstance() is being invoked from within an @Test method, you should be good.

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/

On Tue, Oct 4, 2016 at 1:59 AM, prvn <[hidden email]> wrote:

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

--
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: Accessing values in annotation of a test in a different class

praveen133t
Thanks Krishnan. Yes, I can do that. However the annotation @TestConfig I mentioned in my example needs to be accessed by the whole suite for some reasons. It's not just for that particular @Test. That's why I want to access the values inside @TestConfig in the BaseClass. 

On Tuesday, October 4, 2016 at 12:00:16 AM UTC-4, Krishnan wrote:
Praveen,

Why not just invoke : Reporter.getCurrentTestResult().getMethod()

getCurentTestResult() when invoked always guarantees that it will return back the corresponding ITestResult object of the current TestNG method. Since your startInstance() is being invoked from within an @Test method, you should be good.

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/

On Tue, Oct 4, 2016 at 1:59 AM, prvn <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="4s4_Muf1AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">neevar...@...> wrote:

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

--
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="4s4_Muf1AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="4s4_Muf1AgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
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: Accessing values in annotation of a test in a different class

⇜Krishnan Mahadevan⇝


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/

On Tue, Oct 4, 2016 at 7:31 PM, prvn <[hidden email]> wrote:
Thanks Krishnan. Yes, I can do that. However the annotation @TestConfig I mentioned in my example needs to be accessed by the whole suite for some reasons. It's not just for that particular @Test. That's why I want to access the values inside @TestConfig in the BaseClass. 

​[Krishnan] Agreed. Reporter.getCurrentTestResult() will retrieve an ITestResult object of any valid TestNG annotated method. So its not just confined to @Test, but will also work for @BeforeClass, @AfterClass etc., Would that work ? If you are trying to parse the custom annotation values much before even TestNG kicking in, then the only other alternative that I can think of is the org.testng.IAnnotationTransformer#transform from IAnnotationTransformer interface wherein the individual Method objects are also passed to you. So you can still access/retrieve your custom annotations via the Method object.



On Tuesday, October 4, 2016 at 12:00:16 AM UTC-4, Krishnan wrote:
Praveen,

Why not just invoke : Reporter.getCurrentTestResult().getMethod()

getCurentTestResult() when invoked always guarantees that it will return back the corresponding ITestResult object of the current TestNG method. Since your startInstance() is being invoked from within an @Test method, you should be good.

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/

On Tue, Oct 4, 2016 at 1:59 AM, prvn <[hidden email]> wrote:

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

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

--
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: Accessing values in annotation of a test in a different class

praveen133t
Yes. I can do that. However I would consider as a workaround for my issue. But I would appreciate a way to access these values from my custom annotations way before TestNg is invoked. Do you have an example for the IAnnotationTransformer implementation pls?

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

On Tuesday, October 4, 2016 at 10:11:51 AM UTC-4, Krishnan wrote:


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/

On Tue, Oct 4, 2016 at 7:31 PM, prvn <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="0iPwFUcXAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">neevar...@...> wrote:
Thanks Krishnan. Yes, I can do that. However the annotation @TestConfig I mentioned in my example needs to be accessed by the whole suite for some reasons. It's not just for that particular @Test. That's why I want to access the values inside @TestConfig in the BaseClass. 

​[Krishnan] Agreed. Reporter.getCurrentTestResult() will retrieve an ITestResult object of any valid TestNG annotated method. So its not just confined to @Test, but will also work for @BeforeClass, @AfterClass etc., Would that work ? If you are trying to parse the custom annotation values much before even TestNG kicking in, then the only other alternative that I can think of is the org.testng.IAnnotationTransformer#transform from IAnnotationTransformer interface wherein the individual Method objects are also passed to you. So you can still access/retrieve your custom annotations via the Method object.



On Tuesday, October 4, 2016 at 12:00:16 AM UTC-4, Krishnan wrote:
Praveen,

Why not just invoke : Reporter.getCurrentTestResult().getMethod()

getCurentTestResult() when invoked always guarantees that it will return back the corresponding ITestResult object of the current TestNG method. Since your startInstance() is being invoked from within an @Test method, you should be good.

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/" rel="nofollow" target="_blank" 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/" rel="nofollow" target="_blank" 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/

On Tue, Oct 4, 2016 at 1:59 AM, prvn <[hidden email]> wrote:

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

--
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 [hidden email].
Visit this group at <a href="https://groups.google.com/group/testng-users" rel="nofollow" target="_blank" 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" rel="nofollow" target="_blank" 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="0iPwFUcXAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="0iPwFUcXAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
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: Accessing values in annotation of a test in a different class

⇜Krishnan Mahadevan⇝
I don't quite understand how this is a work-around. 
Unless and until TestNG kicks in, parses your classes [ via reflection ] and builds up an execution model [ a graph which TestNG maintains internally to decide how should the execution happen ], there's no way in which you can get to know what your custom annotation values would have as values.

Here's a sample that I wrote up sometime back which attempts at building dynamic dependency amongst tests using an Annotation Transformer : https://gist.github.com/krmahadevan/1155871

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

Going by this line, IMO the implementation kind of sounds a bit convoluted.
Reporting capabilities is not something that is ever going to change and is also not something that should vary from test to test. So I wouldn't include it as part of a custom annotation.

Browser capabilities is again something that I would usually strive to keep as a common parameter across the JVM, because one usually would like to execute all of their tests in one shot against a particular browser flavor. And even if it has to be tied to a particular @Test annotated test method, I wouldn't require to parse it as early as "right when TestNG begins execution". I would usually want to defer parsing this annotation to as late as "before executing a @Test annotation" which means I can move the logic of reading the annotation, figure out the proxy and the capabilities into a listener such as IInvokedMethodListener#beforeInvocation() wherein I anyways have access to the TestNG's ITestNGMethod object. But then again these are my personal observations.

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/

On Tue, Oct 4, 2016 at 8:53 PM, prvn <[hidden email]> wrote:
Yes. I can do that. However I would consider as a workaround for my issue. But I would appreciate a way to access these values from my custom annotations way before TestNg is invoked. Do you have an example for the IAnnotationTransformer implementation pls?

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

On Tuesday, October 4, 2016 at 10:11:51 AM UTC-4, Krishnan wrote:


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/

On Tue, Oct 4, 2016 at 7:31 PM, prvn <[hidden email]> wrote:
Thanks Krishnan. Yes, I can do that. However the annotation @TestConfig I mentioned in my example needs to be accessed by the whole suite for some reasons. It's not just for that particular @Test. That's why I want to access the values inside @TestConfig in the BaseClass. 

​[Krishnan] Agreed. Reporter.getCurrentTestResult() will retrieve an ITestResult object of any valid TestNG annotated method. So its not just confined to @Test, but will also work for @BeforeClass, @AfterClass etc., Would that work ? If you are trying to parse the custom annotation values much before even TestNG kicking in, then the only other alternative that I can think of is the org.testng.IAnnotationTransformer#transform from IAnnotationTransformer interface wherein the individual Method objects are also passed to you. So you can still access/retrieve your custom annotations via the Method object.



On Tuesday, October 4, 2016 at 12:00:16 AM UTC-4, Krishnan wrote:
Praveen,

Why not just invoke : Reporter.getCurrentTestResult().getMethod()

getCurentTestResult() when invoked always guarantees that it will return back the corresponding ITestResult object of the current TestNG method. Since your startInstance() is being invoked from within an @Test method, you should be good.

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/

On Tue, Oct 4, 2016 at 1:59 AM, prvn <[hidden email]> wrote:

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

--
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]om.
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 testng-users...@googlegroups.com.
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.

--
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: Accessing values in annotation of a test in a different class

Julien Herr
Check my answer on StackOverflow: http://stackoverflow.com/questions/39839331/accessing-values-in-annotation-of-a-test-in-a-different-class-testng/39856546#39856546

Le mercredi 5 octobre 2016 05:37:47 UTC+2, Krishnan a écrit :
I don't quite understand how this is a work-around. 
Unless and until TestNG kicks in, parses your classes [ via reflection ] and builds up an execution model [ a graph which TestNG maintains internally to decide how should the execution happen ], there's no way in which you can get to know what your custom annotation values would have as values.

Here's a sample that I wrote up sometime back which attempts at building dynamic dependency amongst tests using an Annotation Transformer : <a href="https://gist.github.com/krmahadevan/1155871" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fkrmahadevan%2F1155871\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHnu_t9oMYwJbC15adB7e3aMexmtw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fkrmahadevan%2F1155871\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHnu_t9oMYwJbC15adB7e3aMexmtw&#39;;return true;">https://gist.github.com/krmahadevan/1155871

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

Going by this line, IMO the implementation kind of sounds a bit convoluted.
Reporting capabilities is not something that is ever going to change and is also not something that should vary from test to test. So I wouldn't include it as part of a custom annotation.

Browser capabilities is again something that I would usually strive to keep as a common parameter across the JVM, because one usually would like to execute all of their tests in one shot against a particular browser flavor. And even if it has to be tied to a particular @Test annotated test method, I wouldn't require to parse it as early as "right when TestNG begins execution". I would usually want to defer parsing this annotation to as late as "before executing a @Test annotation" which means I can move the logic of reading the annotation, figure out the proxy and the capabilities into a listener such as IInvokedMethodListener#beforeInvocation() wherein I anyways have access to the TestNG's ITestNGMethod object. But then again these are my personal observations.

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/

On Tue, Oct 4, 2016 at 8:53 PM, prvn <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="mT-AvkFDAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">neevar...@...> wrote:
Yes. I can do that. However I would consider as a workaround for my issue. But I would appreciate a way to access these values from my custom annotations way before TestNg is invoked. Do you have an example for the IAnnotationTransformer implementation pls?

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

On Tuesday, October 4, 2016 at 10:11:51 AM UTC-4, Krishnan wrote:


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/" rel="nofollow" target="_blank" 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/" rel="nofollow" target="_blank" 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/

On Tue, Oct 4, 2016 at 7:31 PM, prvn <[hidden email]> wrote:
Thanks Krishnan. Yes, I can do that. However the annotation @TestConfig I mentioned in my example needs to be accessed by the whole suite for some reasons. It's not just for that particular @Test. That's why I want to access the values inside @TestConfig in the BaseClass. 

​[Krishnan] Agreed. Reporter.getCurrentTestResult() will retrieve an ITestResult object of any valid TestNG annotated method. So its not just confined to @Test, but will also work for @BeforeClass, @AfterClass etc., Would that work ? If you are trying to parse the custom annotation values much before even TestNG kicking in, then the only other alternative that I can think of is the org.testng.IAnnotationTransformer#transform from IAnnotationTransformer interface wherein the individual Method objects are also passed to you. So you can still access/retrieve your custom annotations via the Method object.



On Tuesday, October 4, 2016 at 12:00:16 AM UTC-4, Krishnan wrote:
Praveen,

Why not just invoke : Reporter.getCurrentTestResult().getMethod()

getCurentTestResult() when invoked always guarantees that it will return back the corresponding ITestResult object of the current TestNG method. Since your startInstance() is being invoked from within an @Test method, you should be good.

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/" rel="nofollow" target="_blank" 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/" rel="nofollow" target="_blank" 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/

On Tue, Oct 4, 2016 at 1:59 AM, prvn <[hidden email]> wrote:

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

--
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 [hidden email].
Visit this group at <a href="https://groups.google.com/group/testng-users" rel="nofollow" target="_blank" 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" rel="nofollow" target="_blank" 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 testng-users...@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" rel="nofollow" target="_blank" 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" rel="nofollow" target="_blank" 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="mT-AvkFDAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="mT-AvkFDAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
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: Accessing values in annotation of a test in a different class

⇜Krishnan Mahadevan⇝
I saw your answer and have upvoted it Julien :)

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/

On Wed, Oct 5, 2016 at 12:35 PM, Julien Herr <[hidden email]> wrote:
Check my answer on StackOverflow: http://stackoverflow.com/questions/39839331/accessing-values-in-annotation-of-a-test-in-a-different-class-testng/39856546#39856546

Le mercredi 5 octobre 2016 05:37:47 UTC+2, Krishnan a écrit :
I don't quite understand how this is a work-around. 
Unless and until TestNG kicks in, parses your classes [ via reflection ] and builds up an execution model [ a graph which TestNG maintains internally to decide how should the execution happen ], there's no way in which you can get to know what your custom annotation values would have as values.

Here's a sample that I wrote up sometime back which attempts at building dynamic dependency amongst tests using an Annotation Transformer : https://gist.github.com/krmahadevan/1155871

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

Going by this line, IMO the implementation kind of sounds a bit convoluted.
Reporting capabilities is not something that is ever going to change and is also not something that should vary from test to test. So I wouldn't include it as part of a custom annotation.

Browser capabilities is again something that I would usually strive to keep as a common parameter across the JVM, because one usually would like to execute all of their tests in one shot against a particular browser flavor. And even if it has to be tied to a particular @Test annotated test method, I wouldn't require to parse it as early as "right when TestNG begins execution". I would usually want to defer parsing this annotation to as late as "before executing a @Test annotation" which means I can move the logic of reading the annotation, figure out the proxy and the capabilities into a listener such as IInvokedMethodListener#beforeInvocation() wherein I anyways have access to the TestNG's ITestNGMethod object. But then again these are my personal observations.

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/

On Tue, Oct 4, 2016 at 8:53 PM, prvn <[hidden email]> wrote:
Yes. I can do that. However I would consider as a workaround for my issue. But I would appreciate a way to access these values from my custom annotations way before TestNg is invoked. Do you have an example for the IAnnotationTransformer implementation pls?

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

On Tuesday, October 4, 2016 at 10:11:51 AM UTC-4, Krishnan wrote:


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/

On Tue, Oct 4, 2016 at 7:31 PM, prvn <[hidden email]> wrote:
Thanks Krishnan. Yes, I can do that. However the annotation @TestConfig I mentioned in my example needs to be accessed by the whole suite for some reasons. It's not just for that particular @Test. That's why I want to access the values inside @TestConfig in the BaseClass. 

​[Krishnan] Agreed. Reporter.getCurrentTestResult() will retrieve an ITestResult object of any valid TestNG annotated method. So its not just confined to @Test, but will also work for @BeforeClass, @AfterClass etc., Would that work ? If you are trying to parse the custom annotation values much before even TestNG kicking in, then the only other alternative that I can think of is the org.testng.IAnnotationTransformer#transform from IAnnotationTransformer interface wherein the individual Method objects are also passed to you. So you can still access/retrieve your custom annotations via the Method object.



On Tuesday, October 4, 2016 at 12:00:16 AM UTC-4, Krishnan wrote:
Praveen,

Why not just invoke : Reporter.getCurrentTestResult().getMethod()

getCurentTestResult() when invoked always guarantees that it will return back the corresponding ITestResult object of the current TestNG method. Since your startInstance() is being invoked from within an @Test method, you should be good.

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/

On Tue, Oct 4, 2016 at 1:59 AM, prvn <[hidden email]> wrote:

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

--
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]om.
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]om.
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 testng-users...@googlegroups.com.
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.

--
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: Accessing values in annotation of a test in a different class

praveen133t
In reply to this post by ⇜Krishnan Mahadevan⇝
Awesome. Thanks a lot Krishnan. I think it makes sense. I will slightly change my design and it should work for me

On Tuesday, October 4, 2016 at 11:37:47 PM UTC-4, Krishnan wrote:
I don't quite understand how this is a work-around. 
Unless and until TestNG kicks in, parses your classes [ via reflection ] and builds up an execution model [ a graph which TestNG maintains internally to decide how should the execution happen ], there's no way in which you can get to know what your custom annotation values would have as values.

Here's a sample that I wrote up sometime back which attempts at building dynamic dependency amongst tests using an Annotation Transformer : <a href="https://gist.github.com/krmahadevan/1155871" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fkrmahadevan%2F1155871\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHnu_t9oMYwJbC15adB7e3aMexmtw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fkrmahadevan%2F1155871\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHnu_t9oMYwJbC15adB7e3aMexmtw&#39;;return true;">https://gist.github.com/krmahadevan/1155871

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

Going by this line, IMO the implementation kind of sounds a bit convoluted.
Reporting capabilities is not something that is ever going to change and is also not something that should vary from test to test. So I wouldn't include it as part of a custom annotation.

Browser capabilities is again something that I would usually strive to keep as a common parameter across the JVM, because one usually would like to execute all of their tests in one shot against a particular browser flavor. And even if it has to be tied to a particular @Test annotated test method, I wouldn't require to parse it as early as "right when TestNG begins execution". I would usually want to defer parsing this annotation to as late as "before executing a @Test annotation" which means I can move the logic of reading the annotation, figure out the proxy and the capabilities into a listener such as IInvokedMethodListener#beforeInvocation() wherein I anyways have access to the TestNG's ITestNGMethod object. But then again these are my personal observations.

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/

On Tue, Oct 4, 2016 at 8:53 PM, prvn <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="mT-AvkFDAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">neevar...@...> wrote:
Yes. I can do that. However I would consider as a workaround for my issue. But I would appreciate a way to access these values from my custom annotations way before TestNg is invoked. Do you have an example for the IAnnotationTransformer implementation pls?

So based on the values in my @Testconfig I need to set things like "browser capabilities, proxy settings, type of test and reporting etc". This is all I need to do.

On Tuesday, October 4, 2016 at 10:11:51 AM UTC-4, Krishnan wrote:


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/" rel="nofollow" target="_blank" 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/" rel="nofollow" target="_blank" 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/

On Tue, Oct 4, 2016 at 7:31 PM, prvn <[hidden email]> wrote:
Thanks Krishnan. Yes, I can do that. However the annotation @TestConfig I mentioned in my example needs to be accessed by the whole suite for some reasons. It's not just for that particular @Test. That's why I want to access the values inside @TestConfig in the BaseClass. 

​[Krishnan] Agreed. Reporter.getCurrentTestResult() will retrieve an ITestResult object of any valid TestNG annotated method. So its not just confined to @Test, but will also work for @BeforeClass, @AfterClass etc., Would that work ? If you are trying to parse the custom annotation values much before even TestNG kicking in, then the only other alternative that I can think of is the org.testng.IAnnotationTransformer#transform from IAnnotationTransformer interface wherein the individual Method objects are also passed to you. So you can still access/retrieve your custom annotations via the Method object.



On Tuesday, October 4, 2016 at 12:00:16 AM UTC-4, Krishnan wrote:
Praveen,

Why not just invoke : Reporter.getCurrentTestResult().getMethod()

getCurentTestResult() when invoked always guarantees that it will return back the corresponding ITestResult object of the current TestNG method. Since your startInstance() is being invoked from within an @Test method, you should be good.

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/" rel="nofollow" target="_blank" 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/" rel="nofollow" target="_blank" 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/

On Tue, Oct 4, 2016 at 1:59 AM, prvn <[hidden email]> wrote:

I have a custom Annotation I created as below


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


I have test that extends a BaseClass.


import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below


public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethodclass? 

--
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 [hidden email].
Visit this group at <a href="https://groups.google.com/group/testng-users" rel="nofollow" target="_blank" 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" rel="nofollow" target="_blank" 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 testng-users...@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" rel="nofollow" target="_blank" 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" rel="nofollow" target="_blank" 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="mT-AvkFDAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="mT-AvkFDAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
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.