-2

How to demonstrate real time example of using BeforeTest, AfterTest, BeforeSuite, AfterSuite, BeforeClass, AfterClass, BeforeMethod, AfterMethod annotations in TestNG Selenium.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Please follow the documentation from testNG: https://testng.org/doc/documentation-main.html#annotations – Infern0 Aug 19 '19 at 10:29
  • Hi, welcome to Stack Overflow! You'd need to review the documentation from testNG to learn how to use this functionality, Stack overflow is a site for asking and answering questions around specific problems you're having, which need workable examples and code provided with a clear view of your specific issue and desired outcome. What you're asking for is a broad overview of functionality, which isn't what this site is for. Please review: https://meta.stackoverflow.com/questions/258589/breaking-down-too-broad-and-trying-to-understand-it and https://stackoverflow.com/help/how-to-ask – Jsmith2800 Aug 19 '19 at 13:12

1 Answers1

0

For demonstrating a real time example of using BeforeTest, AfterTest, BeforeSuite, AfterSuite, BeforeClass, AfterClass annotations through you don't need Selenium.

Once you install the TestNG plugin within your IDE you simply need to:

  • Mention the annotations for the methods. Example:

    • @BeforeSuite
    • @BeforeClass
    • @BeforeMethod
    • @BeforeTest
    • @Test
    • @AfterTest
    • @AfterMethod
    • @AfterClass
    • @AfterSuite
  • Add the relevant imports for the classes.

    • import org.testng.annotations.BeforeSuite;
    • import org.testng.annotations.BeforeClass;
    • import org.testng.annotations.BeforeMethod;
    • import org.testng.annotations.BeforeTest;
    • import org.testng.annotations.Test;
    • import org.testng.annotations.AfterTest;
    • import org.testng.annotations.AfterMethod;
    • import org.testng.annotations.AfterClass;
    • import org.testng.annotations.AfterSuite;
  • Sample Code Block:

    package demo;
    
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    public class TestNG_Annotation_Demo {
    
        @BeforeSuite
        public void before_suite()
        {
            System.out.println("I am in BeforeSuite");
        }
    
        @BeforeClass
        public void before_class()
        {
            System.out.println("I am in BeforeClass");
        }
    
        @BeforeMethod
        public void before_method()
        {
            System.out.println("I am in BeforeMethod");
        }
    
        @BeforeTest
        public void before_test()
        {
            System.out.println("I am in BeforeTest");
        }
    
        @Test
        public void test()
        {
            System.out.println("I am in Test");
        }
    
        @AfterTest
        public void after_test()
        {
            System.out.println("I am in AfterTest");
        }
    
        @AfterMethod
        public void after_method()
        {
            System.out.println("I am in AfterMethod");
        }
    
        @AfterClass
        public void after_class()
        {
            System.out.println("I am in AfterClass");
        }
    
        @AfterSuite
        public void after_suite()
        {
            System.out.println("I am in AfterSuite");
        }
    }
    
  • Console Output:

    [RemoteTestNG] detected TestNG version 6.14.2
    I am in BeforeSuite
    I am in BeforeTest
    I am in BeforeClass
    I am in BeforeMethod
    I am in Test
    I am in AfterMethod
    I am in AfterClass
    I am in AfterTest
    PASSED: test
    
    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
    I am in AfterSuite
    
    ===============================================
    Default suite
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352