This post was most recently updated on July 31st, 2024
Exmaples for @BeforeMethod and @AfterMethod in TestNG.
@BeforeMethod allows the method to execute before the execution of each Test methods, whereas @afterMethod is executed after the execution of each Test methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package Annotations; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class BeforeAndAfter { @BeforeMethod public void BeforeStart(){ System.out.println("Before Method starts!"); } @AfterMethod public void AfterStart(){ System.out.println("After Method starts"); } @Test public void Test(){ System.out.println("Test with Default priority"); } @Test(priority=1) public void Test1(){ System.out.println("Test with priority"); } } |
Output:
First Method:
Before Method starts!
Test with Default priority
After Method starts
In the above output, we can observe for every Test first @BeforeMethod is executing and then eventually @AfterMethod will execute after each Test method.
Second Method:
Before Method starts!
Test with priority
After Method starts
2. @BeforeClass and @AfterClass
The annotated method @BeforeClass will execute only once before the first test method is invoked in that class. The @AfterClass annotation will be executed only once after all the test methods are invoked in that class. In the below output, we can see that @BeforeClass and @AfterClass are executed at the very beginning and very end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package Annotations; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class BeforeClassAndAfterClass { @BeforeClass public void BeforeStart(){ System.out.println("Before Class starts!"); } @AfterClass public void AfterStart(){ System.out.println("After Class starts"); } @Test public void Test(){ System.out.println("Test method with Default priority"); } @Test(priority=1) public void Test1(){ System.out.println("Test method with priority=1"); } } |
Output:
Before Class Method!
Test method with Default priority
Test method with priority=1
After Class Method!
When we run the above code, the output will be BeforeStart() method. It is executed before executing Test() and Test1() method. During the final step, AfterTest () method is executed. Both the methods executed only once.
3. @BeforeTest and @AfterTest
The @BeforeTest methods run after each Test methods. @BeforeTest can be used for creating an initial data set up and prior to running other test methods, whereas @AfterTest annotation will run after the other tests are complete.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package Annotations; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class BeforeTestAndAfterTest { WebDriver driver = null; @BeforeTest public void BeforeTest(){ System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://www.google.co.in"); System.out.println("Before Test method"); } @Test public void Test() throws InterruptedException{ WebElement Searchbar = driver.findElement(By.xpath("//input[@name='q']")); Searchbar.sendKeys("TestNG"); Thread.sleep(3000); WebElement GoogleSearchButton = driver.findElement(By.xpath("(//input[@name='btnK'])[1]")); GoogleSearchButton.click(); Thread.sleep(2000); WebElement TestngURL = driver.findElement(By.xpath("//a[h3[.='TestNG - Welcome']]")); TestngURL.click(); } @AfterTest public void AfterTest() throws InterruptedException{ System.out.println("Current page:"+driver.getTitle()); Thread.sleep(2000); driver.close(); } } |
When we run the above code, First BeforeMethod will execute in which we declaring Webdriver driver, driver path. Next Test method will execute where we are entering “TestNG” text in the search bar and then click on the search button.
At last AfterTest method will execute which will check the current opened page title and will print the page title eventually will close the page.
4. @BeforeSuite and @AfterSuite
@BeforeSuite method will run before all tests present in this suite and eventually @AfterSuite will run after all the tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package Annotations; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; public class BeforeSuiteAndAfterSuite { WebDriver driver; @BeforeSuite(alwaysRun=true) public void BeforeSuite(){ System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://www.google.com"); } @AfterSuite(alwaysRun=true) public void AfterSuite(){ System.out.println("Current page location:"+driver.getTitle()); driver.close(); } } |
In this tutorial, we’ve discussed some important annotations and attributes. Some other annotations that are also used include @AfterGroups and @BeforeGroups which we covered with @Test annotation.