This article will discuss what before and after groups annotation is. BeforeGroups and AfterGroups are used to execute setup and teardown methods before and after a group of test cases are run, respectively.
@BeforeGroups
This annotation is used to specify a method that should be executed before the first test method in a group is run.
The method annotated with @BeforeGroups will run once before executing any test methods in the specified group.
@BeforeGroups("form")
public void setup() {
// This method will execute before any test method in the "form" group is executed
}
@AfterGroups
This annotation is used to specify a method that should be executed after the last test method in a group is run.
The method annotated with @AfterGroups will run once after all test methods in the specified group are executed.
@AfterGroups("tester")
public void teardown() {
// This method will execute after all test methods in the "tester" group are executed
}
package week4.day2;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Frame {
ChromeDriver driver;
@BeforeTest
private void setup() throws InterruptedException {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("http://www.programsbuzz.com/user/login");
driver.manage().window().maximize();
Thread.sleep(5000);
}
@BeforeGroups("form")
private void executionMessage2() {
System.out.println("----------------------Going to run the form group------------------");
}
@AfterGroups("form")
private void executionMessage4() {
System.out.println("----------------------form Ended------------------");
}
@Test(groups = { "form" })
private void midFrame() throws InterruptedException {
driver.findElement(By.id("edit-name")).sendKeys("Naruto");
}
@Test(groups = { "form" })
private void botFrame() {
driver.findElement(By.id("edit-pass")).sendKeys("uzumaki");
}
}
Consider this real-time example here. We are running the annotations before and after groups for the form group.
To run this, let us create a test suite XML file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test_suite">
<test name="form">
<classes>
<class name="week4.day2.Frame"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Now executing this will execute the respective annotations as to their expected behavior.
- Log in to post comments