0

I have a test method belonging to multiple groups, I want to ensure that one @BeforeGroups method (setupHomePage) runs before another (setupUniversalSearchPage). The order must be defined within java only as I am supposed to use any testng xml. Although in similar case in another test class the order of execution is as desired.

public abstract class HomePageTest extends TestBase
{
   @BeforeGroups("homePageTests")
   public void setupHomePage()
   {
       loadHomePage();
   }
}

public abstract class UniversalSearchPageTest extends HomePageTest
{
   @BeforeGroups("universalPageTests")
   public void setupUniversalSearchPage()
   {
       navigateToSearchPage();
   }
}

public class UniversalSearchPageBasicTest extends UniversalPageTest
{
   @Test(groups = {"homePageTests","universalPageTests")
   public void searchVerificationTest()
   {
       //test code here
   }
}

So far I have tried following : 1. reversing the order of groups in @Test annotation of searchVerificationTest 2. adding @Test(groups = "homePageTests") above declaration of UniversalSearchPageBasicTest class.

I would like to know how the order @BeforeGroups method is determined as it is different in one class than other. If it is of any relation I am using maven in eclipse and Appium I am new to testng , let me know if I am missing anything basic here

Anurag Shukla
  • 379
  • 1
  • 3
  • 11

1 Answers1

1

@BeforeMethod methods will run in inheritance order - the highest superclass first, then going down the inheritance chain. @AfterMethod methods run in reverse order (up the inheritance chain).

Note, however, that the ordering of multiple annotated methods within one class is not guaranteed (so it's best to avoid that).

Also there is a online documentation available, checkout http://testng.org/doc/documentation-main.html#annotations

anshul Gupta
  • 1,182
  • 8
  • 17
  • The important thing to note is that order is not guaranteed between methods in the same class with the same annotation. – Julian Jun 16 '17 at 16:10
  • But somehow in my case it is not following that order, it tries to run setupUniversalSearchPage() method before setupHomePage() – Anurag Shukla Jun 19 '17 at 07:37
  • @AnuragShukla is it happening for UniversalSearchPageBasicTest test case ? – anshul Gupta Jun 19 '17 at 08:06
  • @anshulGupta yes , if it is of any relevance I am using testng 6.11 – Anurag Shukla Jun 19 '17 at 09:25
  • @AnuragShukla the problem why you might be seeing this is because of UniversalSearchPageTest becoming base class for UniversalSearchPageBasicTest test. Try using super and make a call. – anshul Gupta Jun 19 '17 at 09:28
  • In case case , shouldn't the @beforeGroups in highest superclass run first ? – Anurag Shukla Jun 19 '17 at 09:36
  • That is the problem, the order guarantee between the method is not supported. One of the possible hack could be try providing the alphabetic order of the test case. Test case which need to executed first should be in first alphabetic place and so on. Try doing it once or use Super(). – anshul Gupta Jun 19 '17 at 09:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147042/discussion-between-anurag-shukla-and-anshul-gupta). – Anurag Shukla Jun 19 '17 at 09:45