0
votes

My question is as far as a framework goes should I be using such TestNG Annotations as @beforemethod @beforetest @aftertest? Or just have the code in each class?

For example why would I call the firefox driver with these @before annotations? I can understand that then i can reuse the code and just call a chrome driver for example, but most of the time running the same tests on chrome or other browsers require some modifications to pass, and I can just copy the whole code anyway.

So wouldn't it make sense to have the code or call the code directly into the class I am using for @test?

Also, with @afterTest why would i call teardown there? When i can just call it in every class?

Are there any advantages/disadvantages of using these annotations instead of writing the code directly in the class?

For example, I have this file which runs some tests:

package FireFox;

import org.testng.annotations.Test;

public class TestGroup {
//////////////////////////////////////////////////
    @Test(priority=1)
    public void SessionTasks() {
    Session_Tasks call = new Session_Tasks();
    call.sessionTasks();
    }  
////////////////////////////////////////////////    
    @Test(priority=2)
    public void SignUp () {
    Signup call = new Signup();
    call.signup();
    }
//////////////////////////////////////////////////
    @Test(priority=3)
    public void Signup_PostQ() {
    Signup_PostQs call = new Signup_PostQs();
    call.signup_postq();
    }
//////////////////////////////////////////////////
    @Test(priority=4)
    public void Sign_In() {
    SignIn call = new SignIn();
    call.signIn();
    }
//////////////////////////////////////////////////
    @Test(priority=5)
    public void Sign_In_PostQ() {
    SignIn_PostQs call = new SignIn_PostQs();
    call.signIn_postq();
    }
//////////////////////////////////////////////////
    @Test(priority=6)
    public void SessionConnect() {
    Session_Connect call = new Session_Connect();
    call.sessionConnect();
    }
//////////////////////////////////////////////////
    @Test(priority=7)
    public void TutorMenu() {
    Tutor_Menu call = new Tutor_Menu();
    call.Tutor_menu();
}

}

All the actions are contained in each class, opening specific browser, navigating to a url, and at the end of the test closing the browser. Why would I want to use any of the @before,@after annotations, as opposed to having everything contained in each class?

2
Whereas your question is valid, using of those attributes can differ a lot depending on how you are using them. I believe providing some codes of how you are using them would definitely help understand your question otherwise I am afraid it's too broad.Saifur
Hi Saifur, For example for a suite of Tests I always navigate to a url with Firefox. Why would I use the A-Before annotation instead of having these steps in each file itself? Or driver.close(); why should i use A-after annotations to save me time copying and pasting it? It seems like it's a similar way of achieving the same thing, I just don't understand the advantage of passing something off into TestNG.Elsid

2 Answers

1
votes

I think the main reason you would want to use @Before and @After annotations is so that your initial login and cleanup of browser tests are not showing up on your report. If you leave them in your report, then you end up showing in your report that you are testing the same code path over and over again. By putting the browser creation and teardown in outside @Configuration methods, you avoid this test path duplication.

Also, if you follow the recommended method of generating WebDriver instances within a DataProvider , which in turn passes the driver instance as an argument to test methods, then you can use the @BeforeMethod annotation to get a hold of that driver before the test method is called and do any necessary preparatory steps before the driver is used within the body of each test method. That is a MUCH overlooked feature of TestNG that JUnit is unable to do. Of course, doing it this way allows you to run multi-threaded, on a method by method basis, and increases the speed of your entire test suite.

1
votes

Freedom of Using @Before or @After or any other annotations in the Test code completely goes with the Tester.

  • The very basic reason for using annotations in your Test code is it makes your code easy-to-read and easy-to-understand for other Testers.Provides Clean code.
  • Using annotations like @Before,@After,@BeforeClass or many others helps you to reduce work and time to write repetitive codes again and again. Its a smart way to repeat same actions in your Test execution without actually writing it again.
  • Test cases can be Grouped more easily resulting into efficient parallel testing.
  • Very efficient use of such annotations will be in Test Suites. In
    Test-suites we usually experiences dependencies. This dependencies
    between the various Test-cases are written by Pre-conditons and
    Post-conditions
    . These pre-condictions and post-conditions we can
    handle very effectively and efficently by using such annotations.

Though its not a rule of thumb for using such annotations, but using them when required in your test code is observed to be a good practice. Hope this solves your confusion or question about using annotations with frameworks.