Stop execution of @Beforemethod

274 views
Skip to first unread message

Gagandeep Singh

unread,
Oct 10, 2019, 12:46:04 AM10/10/19
to testng-users
Hi,

I have a Parent class called Parent and a child class called Child, which extends Parent class. Parent class has a method A annotated with @BeforeMethod annotation and child class has a method B which is also annotated with @BeforeMethod annotation. Now, is there a way to stop the execution of method A which is annotated with @BeforeMethod annotation. Here I will explain it with an example:
 

public class Parent {

       @BeforeMethod
        protected void a() {
         System.out.println("I am inside parent class @BeforeMethod");
   }

}

public class Child extends Parent {
      @BeforeMethod
       private void b() {
        System.out.println("I am inside child class @BeforeMethod");
     }
}

Now, is there a way to stop the execution of parent class BeforeMethod from child class BeforeMethod? or might be using a custom listener? 

Note: This parent class is also extended by other classes. So, in other cases @BeforeMethod inside the parent class should run as expected but should not run only when child class test are executed.

Any help or suggestions would be appreciated. TIA.

⇜Krishnan Mahadevan⇝

unread,
Oct 10, 2019, 2:07:39 AM10/10/19
to testng...@googlegroups.com
I don't think its going to be possible.

I think TestNG goes from base class to child classes (at least that's how I remember it happening) or maybe the order is non deterministic because of reflection being used [ One of these is true, I can't remember at the top of my head as to which is the one ]
You may need to re-think on how you want to do this.

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 Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/75128be3-65f2-4390-8a24-4dd77b523806%40googlegroups.com.

Gagandeep Singh

unread,
Oct 10, 2019, 2:38:44 AM10/10/19
to testng-users
Right, thanks :) 


On Thursday, October 10, 2019 at 7:07:39 PM UTC+13, Krishnan Mahadevan wrote:
I don't think its going to be possible.

I think TestNG goes from base class to child classes (at least that's how I remember it happening) or maybe the order is non deterministic because of reflection being used [ One of these is true, I can't remember at the top of my head as to which is the one ]
You may need to re-think on how you want to do this.

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 Scribblings @ https://rationaleemotions.com/


On Thu, Oct 10, 2019 at 10:16 AM Gagandeep Singh <singh....@gmail.com> wrote:
Hi,

I have a Parent class called Parent and a child class called Child, which extends Parent class. Parent class has a method A annotated with @BeforeMethod annotation and child class has a method B which is also annotated with @BeforeMethod annotation. Now, is there a way to stop the execution of method A which is annotated with @BeforeMethod annotation. Here I will explain it with an example:
 

public class Parent {

       @BeforeMethod
        protected void a() {
         System.out.println("I am inside parent class @BeforeMethod");
   }

}

public class Child extends Parent {
      @BeforeMethod
       private void b() {
        System.out.println("I am inside child class @BeforeMethod");
     }
}

Now, is there a way to stop the execution of parent class BeforeMethod from child class BeforeMethod? or might be using a custom listener? 

Note: This parent class is also extended by other classes. So, in other cases @BeforeMethod inside the parent class should run as expected but should not run only when child class test are executed.

Any help or suggestions would be appreciated. TIA.

--
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...@googlegroups.com.

Gagandeep Singh

unread,
Oct 10, 2019, 6:51:58 PM10/10/19
to testng-users
I believe from your experience, you still might be able to recommend me the right way to achieve what I am trying to do. I will explain you my use case in detail. 

I have parent suite class with the following configuration which is further extended by many classes. 

@BeforeClass(alwaysRun = true)
@Parameters({"browser", "logInPageUrl", "usernameField", "passwordField", "loginButton", "userMenu"})
protected void invokeBrowserAndLogIn(@Optional("chrome") String browser, @Optional("https://www.xyz.com/") String logInPageUrl,
@Optional("os_username") String usernameField, @Optional("os_password") String passwordField,
@Optional("login") String loginButton, @Optional("admin") String userMenu) {
switch (browser.toLowerCase()) {
case "firefox":
WebDriverManager.firefoxdriver().setup();
driver.set(new FirefoxDriver());
break;
case "ie":
if(!System.getProperty("os.name").toLowerCase().contains("mac")) {
WebDriverManager.iedriver().setup();
driver.set(new InternetExplorerDriver());
} else {
System.out.println("You appear to be running MAC operating system. The IE driver only runs on Windows operating system. Tests not executed. Please review!");
throw new IllegalStateException("You appear to be running MAC operating system. The IE driver only runs on Windows operating system. Tests not executed. Please review!");
}
break;
default:
WebDriverManager.chromedriver().setup();
driver.set(new ChromeDriver());
}
testEnvironment(logInPageUrl);
login(usernameField, passwordField, loginButton, userMenu);
}


Now many classes share the same configuration methods but some classes need additional data/configuration. So, I have added extra parameters to the list which breaks method overriding and leaves me with 2 @BeforeClass methods. Here is a configuration method which is specific to a particular class where I have added additional parameter:

@BeforeClass(alwaysRun = true)
@Parameters({"browser", "logInPageUrl", "usernameField", "passwordField", "loginButton", "userMenu", "newParameter"})
protected void invokeBrowserAndLogIn(@Optional("chrome") String browser, @Optional("https://www.xyz.com/") String logInPageUrl,
@Optional("os_username") String usernameField, @Optional("os_password") String passwordField,
@Optional("login") String loginButton, @Optional("admin") String userMenu, @Optional("new value") String NewParameter) {
    switch (browser.toLowerCase()) {
case "firefox":
WebDriverManager.firefoxdriver().setup();
driver.set(new FirefoxDriver());
break;
case "ie":
if(!System.getProperty("os.name").toLowerCase().contains("mac")) {
WebDriverManager.iedriver().setup();
driver.set(new InternetExplorerDriver());
} else {
System.out.println("You appear to be running MAC operating system. The IE driver only runs on Windows operating system. Tests not executed. Please review!");
throw new IllegalStateException("You appear to be running MAC operating system. The IE driver only runs on Windows operating system. Tests not executed. Please review!");
}
break;
default:
WebDriverManager.chromedriver().setup();
driver.set(new ChromeDriver());
}
testEnvironment(logInPageUrl);
login(usernameField, passwordField, loginButton, userMenu);

}

 Now, what do you propose? Do you have any suggestion or recommendation to handle my use case?








On Thursday, October 10, 2019 at 7:07:39 PM UTC+13, Krishnan Mahadevan wrote:
I don't think its going to be possible.

I think TestNG goes from base class to child classes (at least that's how I remember it happening) or maybe the order is non deterministic because of reflection being used [ One of these is true, I can't remember at the top of my head as to which is the one ]
You may need to re-think on how you want to do this.

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 Scribblings @ https://rationaleemotions.com/


On Thu, Oct 10, 2019 at 10:16 AM Gagandeep Singh <singh....@gmail.com> wrote:
Hi,

I have a Parent class called Parent and a child class called Child, which extends Parent class. Parent class has a method A annotated with @BeforeMethod annotation and child class has a method B which is also annotated with @BeforeMethod annotation. Now, is there a way to stop the execution of method A which is annotated with @BeforeMethod annotation. Here I will explain it with an example:
 

public class Parent {

       @BeforeMethod
        protected void a() {
         System.out.println("I am inside parent class @BeforeMethod");
   }

}

public class Child extends Parent {
      @BeforeMethod
       private void b() {
        System.out.println("I am inside child class @BeforeMethod");
     }
}

Now, is there a way to stop the execution of parent class BeforeMethod from child class BeforeMethod? or might be using a custom listener? 

Note: This parent class is also extended by other classes. So, in other cases @BeforeMethod inside the parent class should run as expected but should not run only when child class test are executed.

Any help or suggestions would be appreciated. TIA.

--
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...@googlegroups.com.

Krishnan Mahadevan

unread,
Oct 10, 2019, 11:29:44 PM10/10/19
to testng...@googlegroups.com

I think you should basically get rid of all parameters in your configuration method and instead just work with native injection wherein you inject an ITestContext object via which you retrieve the parameters map and work with it.

 

You could perhaps build a utility method which accepts an ITestContext object and returns back various different pojos (one pojo which is common across all tests, and then subclassed pojos which are specific to particular tests).

 

That should streamline things a lot.

 

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

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/9a24f157-33b1-4973-8318-0a0f53af33f6%40googlegroups.com.

Gagandeep Singh

unread,
Oct 10, 2019, 11:45:06 PM10/10/19
to testng...@googlegroups.com
Umm well that sounds great but to be honest, I'm just a beginner and I have no idea what you are talking about!  How to do native injection and pojo's and stuff? I have absolutely no idea what that is and how to achieve it. Can you give me a starting point like a resource or something? 

You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/HElSfOCXFyg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/DB118C57-EB71-48AC-AB0A-138C1A713535%40gmail.com.

Krishnan Mahadevan

unread,
Oct 10, 2019, 11:47:09 PM10/10/19
to testng...@googlegroups.com

Gagandeep Singh

unread,
Oct 10, 2019, 11:49:06 PM10/10/19
to testng...@googlegroups.com
Thank you so much. Really appreciate your response :) 

Arthi Vigneshwari

unread,
Oct 12, 2019, 2:25:16 PM10/12/19
to testng...@googlegroups.com
Hi Gagandeep,

@BeforeMethod goes from top to bottom i.e. from base class to child class.
@AfterMethod goes from child class to parent class.

Thanks,
Arthi

Gagandeep Singh

unread,
Oct 15, 2019, 6:21:56 PM10/15/19
to testng-users
Thanks for your clarification Arthi, appreciate it!


On Sunday, October 13, 2019 at 7:25:16 AM UTC+13, Arthi Vigneshwari wrote:
Hi Gagandeep,

@BeforeMethod goes from top to bottom i.e. from base class to child class.
@AfterMethod goes from child class to parent class.

Thanks,
Arthi

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

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages