The problem is that my test class is already implemented some class:
public class MainTest extends ExecutionContext ...
Thus, I can move general logic to parent class as it is usually done:
public class TestBase {
protected final String baseUrl = Config.getProperty(Config.TEST_HOST);
protected final String driverName = Config.getProperty(Config.BROWSER);
protected final DriversEnum driverInstance = DriversEnum.valueOf(driverName.toUpperCase());
@BeforeMethod(alwaysRun = true)
public void start() {
...
}
@AfterMethod(alwaysRun = true)
public void end() {
Logger.logDebug("Start AFTER Method");
}
I tried to use Java 8 features like default
or static
methods. Here is a sample:
@Listeners(TestListener.class)
public interface ITestBase {
String baseUrl = Config.getProperty(Config.TEST_HOST);
String driverName = Config.getProperty(Config.BROWSER);
DriversEnum driverInstance = DriversEnum.valueOf(driverName.toUpperCase());
@BeforeMethod(alwaysRun = true)
default public void start() {
...
}
@AfterMethod(alwaysRun = true)
default public void end() {
...
}
When I run typical TestNG test method, like:
public class AppUiDemo implements ITestBase {
@Test
public void checkWebDriverCreation() {
...
}
start()
and end()
methods haven't called.
Is it possible to use interface with any implementation for execution code before and after test methods with TestNG?