TestNG annotation BeforeMethod and Aftermethod Issue in Selenium JAVA

1.1k Views Asked by At

I have written the following test using the testNG annotations:

public class Create {
    
    WebDriver driver;
    
    @BeforeClass
    
    public void testsetup() {
        
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }
        
        @BeforeMethod
        public void Login() {
        driver.get("www.xyz.com");//just an example
    }
    
    @Test(priority=3)
    
    public void AccountCreate() {
        System.out.println("Test3");
    }

        }
    
    @Test(priority=1)
    public void CompanyCreate() {
        System.out.println("Test1");
    }

    @Test(priority=2)
    public void VerifyResult() {
        System.out.println("Test2");
            }
    }
    
    @AfterMethod
    public void Logout() {
        
        System.out.println("print after method");
    }
    
    @AfterClass
    public void CloseBrowser() {
        driver.close();
        
    }
}

The o/p is like this:

print after method
test1
print after method
test2
print after method
test3

Observations; @BeforeClass executes first, then @Beforemethod executes, then @Aftermethod and then @Test(priority=1), @Aftermethod then @Beforemethod and then @Test(priority=2) and so on.

But after all the @Test run, then only the @Aftermethod executes. Anyone, please help me on this. I really couldn't find what exactly is the problem.

1

There are 1 best solutions below

0
On

There is no issue in your code block. I took your code, simplified it a bit replacing the Browsing Context line with Sysout() statements and here is the execution result:

  • Code Block:

    public class Aftermethod_Test {
    
        WebDriver driver;
    
        @BeforeClass
        public void testsetup() {
    
            System.out.println("print before class");
        }
    
        @BeforeMethod
        public void Login() {
            System.out.println("print before method");
        }
    
        @Test(priority=3)
    
        public void AccountCreate() {
            System.out.println("Test3");
        }
    
        @Test(priority=1)
        public void CompanyCreate() {
            System.out.println("Test1");
        }
    
        @Test(priority=2)
        public void VerifyResult() {
            System.out.println("Test2");
        }
    
        @AfterMethod
        public void Logout() {
            System.out.println("print after method");
        }
    
        @AfterClass
        public void CloseBrowser() {
            System.out.println("print after class");
        }
    }
    
  • Console Output:

    print before class
    print before method
    Test1
    print after method
    print before method
    Test2
    print after method
    print before method
    Test3
    print after method
    print after class
    PASSED: CompanyCreate
    PASSED: VerifyResult
    PASSED: AccountCreate
    
    ===============================================
        Default test
        Tests run: 3, Failures: 0, Skips: 0
    ===============================================
    
    
    ===============================================
    Default suite
    Total tests run: 3, Failures: 0, Skips: 0
    ===============================================
    

Analysis

This observation looks just perfect and as per your expectation.

Your usecase

Earlier it was observed at times Sysout() from @Test reaches to the console later then @Aftermethod. A simple solution would be to update your test environment with all the latest binaries:

  • TestNG: <version>6.10</version>
  • JDK is upgraded to current levels JDK 8u251.
  • Selenium is upgraded to current levels Version 3.141.59.

Reference

You can find a detailed relevant discussion in: