Create after method for running only after several methods (UI,Java,TestNG)

Viewed 82

Hello tell me please how can I run after method only after several methods? I try to create it for UI autotests with Java + TestNG + Maven. Thanks in advance

1 Answers

Check it out this simple code, this might make a little clue to you how @AfterGroups works:

import org.testng.annotations.AfterGroups;
import org.testng.annotations.Test;

public class MyTest {
    @Test(groups= {"group1"})
    public void yourTest1() {
        System.out.println("yourTest1");
    }

    @Test(groups= {"group1"})
    public void yourTest2() {
        System.out.println("yourTest2");
    }

    @Test
    public void yourTest3() {
        System.out.println("yourTest3");
    }

    @AfterGroups("group1")
    public void tearDown() {
        System.out.println("tearDown");
    }
}

Output:

yourTest1
yourTest2
tearDown
yourTest3

For @AfterGroups which handles multiple groups, please define with multiple groups name, looks like this:

public class MyTest {
    @Test(groups= {"group1"})
    public void yourTest1() {
        System.out.println("yourTest1");
    }

    @Test(groups= {"group2"})
    public void yourTest2() {
        System.out.println("yourTest2");
    }

    @Test
    public void yourTest3() {
        System.out.println("yourTest3");
    }

    @AfterGroups(groups= {"group1", "group2"})
    public void tearDown() {
        System.out.println("tearDown");
    }
}

Output:

yourTest1
tearDown
yourTest2
tearDown
yourTest3

Hope this helps

Related