164. Using @BeforeClass and @AfterClass TestNG Annotations





As we've already discussed the advantage of  @BeforeClass over the @BeforeMethod TestNG Annotation and also the advantage of @AfterClass over the @AfterMethod TestNG annotation in our previous post. Lets implement the @BeforeClass and @AfterClass TestNG annotations.

As part of Refactoring the Selenium Automation Code we've to use @BeforeClass and @AfterClass TestNG annotations instead of @BeforeMethod and @AfterMethod TestNG annotation.

Lets Implement This: (i.e. Implementing @BeforeClass and @AfterClass TestNG annotations)

1. Lets view the code containing the @BeforeMethod and @AfterMethod TestNG annotations as shown below:




2. Replace the @BeforeMethod TestNG annotation with @BeforeClass TestNG annotation as shown below:



3. Replace the @AfterMethod TestNG  annotation with @AfterClass TestNG annotation as shown below:



4. Save and Run the 'Class79.java' file by selecting the 'TestNG' option as shown below:



5. In this case, Observe that the Firefox Browser has launched only once for performing the above three tests as shown in the below video.

Watch the Below Video:

Click Here to watch the Video

Download this Project:


Click here to download this project and import into Eclipse IDE  on your machine.


Other TestNG annotations:

As we have gone through the below basic TestNG annotations:

1. @Test
2. @BeforeMethod
3. @AfterMethod
4. @BeforeClass
5. @AfterClass

The below remaining TestNG annotations will be explained when the need arrives:

6.   @BeforeGroups
7.   @AfterGroups
8.   @BeforeSuite
9.   @AfterSuite
10. @Parameters
11. @DataProvider
12. @Factory
13. @Listerners


Please comment below to feedback or ask questions.

Creating testing.xml configuration file will be explained in the next post.



Followers

Labels