Get class variable in superclass @BeforeClass

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

Get class variable in superclass @BeforeClass

garvitagirotraev
Hi,

My framework is designed in way that every package has one property file. Base is superclass of every class.

Current working : In every test class, i have one variable 
String path = '"path to prop file";

Class A extends SequentialRun
{
@BeforeClass
public void load()
    {
        loadPropertyFile(path);  // loadPropertyFile is function defined in Base class.

    }
}

Required : Instead of writing loadPropertyFile in @Before class in every class. I want to define it once in super class , 1 @BeforeClass method. I do not know how to pass variable path from subclass to this method.

Class SequentailRun
{
  @BeforeClass
   public void load()
    {
         loadPropertyFile(path);  // how to pass class A path here?

     }
}

Thanks !!

--
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: Get class variable in superclass @BeforeClass

akash kansal
Hi Garvit,

There is Reflection classes in java.
Try them.

On Mon, Jul 30, 2018 at 3:29 PM, <[hidden email]> wrote:
Hi,

My framework is designed in way that every package has one property file. Base is superclass of every class.

Current working : In every test class, i have one variable 
String path = '"path to prop file";

Class A extends SequentialRun
{
@BeforeClass
public void load()
    {
        loadPropertyFile(path);  // loadPropertyFile is function defined in Base class.

    }
}

Required : Instead of writing loadPropertyFile in @Before class in every class. I want to define it once in super class , 1 @BeforeClass method. I do not know how to pass variable path from subclass to this method.

Class SequentailRun
{
  @BeforeClass
   public void load()
    {
         loadPropertyFile(path);  // how to pass class A path here?

     }
}

Thanks !!

--
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: Get class variable in superclass @BeforeClass

⇜Krishnan Mahadevan⇝

You could build this with a proper design itself.

 

Here’s how you do it:

  1. Since you mentioned that you have a base class from which all tests extend, you add an abstract method which when invoked would return the location of the properties file.
  2. All your test classes would now have to implement the abstract method defined in (#1).

 

Here’s how it would look like:

 

import org.testng.annotations.BeforeClass;

public abstract class SequentialRun {
 
@BeforeClass
 
public void beforeClass() {
    loadPropertiesFile(getPropertiesFileLocation());
  }

 
public static void loadPropertiesFile(String file) {
    System.
err.println("File loaded from " + file);
  }

 
public abstract String getPropertiesFileLocation();
}

 

import org.testng.annotations.Test;

public class ClassA extends SequentialRun {

 
@Override
 
public String getPropertiesFileLocation() {
   
return "src/test/resources/ClassA.properties";
  }

 
@Test
 
public void testMethodA() {
    System.
err.println("Hello world");
  }
}

 

If you don’t want to take the abstract class route , you can do it via custom annotations. Here’s how you do it:

 

  1. Define a custom annotation which can be used to capture the location of your properties file. Annotate your child class with this annotation.
  2. Now from within your base class’s @BeforeClass, read the current classes’ annotation and if this annotation was detected you get hold of the properties file location.

 

Here’s a sample:

 

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

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PropertiesFile {
  String value();
}

 

import org.testng.annotations.BeforeClass;

public class AnotherSequentialRun {
 
@BeforeClass
 
public void beforeClass() {
    loadPropertiesFile(getPropertiesFileLocation());
  }

 
public static void loadPropertiesFile(String file) {
    System.
err.println("File loaded from " + file);
  }

 
private String getPropertiesFileLocation() {
   
PropertiesFile propsFile = getClass().getAnnotation(PropertiesFile.class);
   
if (propsFile == null) {
     
throw new IllegalStateException("Annotation @" + PropertiesFile.class.getName()
          +
" was not found on class " + getClass().getName());
    }
   
return propsFile.value();
  }
}

 

import org.testng.annotations.Test;

@PropertiesFile("src/test/resources/anotherclassa.properties")
public class AnotherClassA extends AnotherSequentialRun {
 
@Test
 
public void testMethod() {
    System.
err.println("Hello world");
  }
}

 

 

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 akash kansal <[hidden email]>
Reply-To: <[hidden email]>
Date: Monday, July 30, 2018 at 3:47 PM
To: <[hidden email]>
Subject: Re: [testng-users] Get class variable in superclass @BeforeClass

 

Hi Garvit,

 

There is Reflection classes in java.

Try them.

 

On Mon, Jul 30, 2018 at 3:29 PM, <[hidden email]> wrote:

Hi,

 

My framework is designed in way that every package has one property file. Base is superclass of every class.

 

Current working : In every test class, i have one variable 

String path = '"path to prop file";

 

Class A extends SequentialRun

{

@BeforeClass

public void load()

    {

        loadPropertyFile(path);  // loadPropertyFile is function defined in Base class.

 

    }

}

 

Required : Instead of writing loadPropertyFile in @Before class in every class. I want to define it once in super class , 1 @BeforeClass method. I do not know how to pass variable path from subclass to this method.

 

Class SequentailRun

{

  @BeforeClass

   public void load()

    {

         loadPropertyFile(path);  // how to pass class A path here?

 

     }

}

 

Thanks !!

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

--
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: Get class variable in superclass @BeforeClass

garvitagirotraev
I tried with custom annotation , it worked well.

Thanks !!

On Tuesday, July 31, 2018 at 8:41:52 AM UTC+5:30, Krishnan Mahadevan wrote:

You could build this with a proper design itself.

 

Here’s how you do it:

  1. Since you mentioned that you have a base class from which all tests extend, you add an abstract method which when invoked would return the location of the properties file.
  2. All your test classes would now have to implement the abstract method defined in (#1).

 

Here’s how it would look like:

 

import org.testng.annotations.BeforeClass;

public abstract class SequentialRun {
 
@BeforeClass
 
public void beforeClass() {
    loadPropertiesFile(getPropertiesFileLocation());
  }

 
public static void loadPropertiesFile(String file) {
    System.
err.println("File loaded from " + file);
  }

 
public abstract String getPropertiesFileLocation();
}

 

import org.testng.annotations.Test;

public class ClassA extends SequentialRun {

 
@Override
 
public String getPropertiesFileLocation() {
   
return "src/test/resources/ClassA.properties";
  }

 
@Test
 
public void testMethodA() {
    System.
err.println("Hello world");
  }
}

 

If you don’t want to take the abstract class route , you can do it via custom annotations. Here’s how you do it:

 

  1. Define a custom annotation which can be used to capture the location of your properties file. Annotate your child class with this annotation.
  2. Now from within your base class’s @BeforeClass, read the current classes’ annotation and if this annotation was detected you get hold of the properties file location.

 

Here’s a sample:

 

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

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PropertiesFile {
  String value();
}

 

import org.testng.annotations.BeforeClass;

public class AnotherSequentialRun {
 
@BeforeClass
 
public void beforeClass() {
    loadPropertiesFile(getPropertiesFileLocation());
  }

 
public static void loadPropertiesFile(String file) {
    System.
err.println("File loaded from " + file);
  }

 
private String getPropertiesFileLocation() {
   
PropertiesFile propsFile = getClass().getAnnotation(PropertiesFile.class);
   
if (propsFile == null) {
     
throw new IllegalStateException("Annotation @" + PropertiesFile.class.getName()
          +
" was not found on class " + getClass().getName());
    }
   
return propsFile.value();
  }
}

 

import org.testng.annotations.Test;

@PropertiesFile("src/test/resources/anotherclassa.properties")
public class AnotherClassA extends AnotherSequentialRun {
 
@Test
 
public void testMethod() {
    System.
err.println("Hello world");
  }
}

 

 

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="j1-B-6zCCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...> on behalf of akash kansal <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="j1-B-6zCCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">akashka...@...>
Reply-To: <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="j1-B-6zCCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...>
Date: Monday, July 30, 2018 at 3:47 PM
To: <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="j1-B-6zCCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@...>
Subject: Re: [testng-users] Get class variable in superclass @BeforeClass

 

Hi Garvit,

 

There is Reflection classes in java.

Try them.

 

On Mon, Jul 30, 2018 at 3:29 PM, <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="j1-B-6zCCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">garvitag...@...> wrote:

Hi,

 

My framework is designed in way that every package has one property file. Base is superclass of every class.

 

Current working : In every test class, i have one variable 

String path = '"path to prop file";

 

Class A extends SequentialRun

{

@BeforeClass

public void load()

    {

        loadPropertyFile(path);  // loadPropertyFile is function defined in Base class.

 

    }

}

 

Required : Instead of writing loadPropertyFile in @Before class in every class. I want to define it once in super class , 1 @BeforeClass method. I do not know how to pass variable path from subclass to this method.

 

Class SequentailRun

{

  @BeforeClass

   public void load()

    {

         loadPropertyFile(path);  // how to pass class A path here?

 

     }

}

 

Thanks !!

--
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="j1-B-6zCCQAJ" 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="j1-B-6zCCQAJ" 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="j1-B-6zCCQAJ" 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="j1-B-6zCCQAJ" 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.