I want to know if it is possible to use default methods from interface with TestNG @BeforeMethod
annotation?
Here is sample, which I tried:
@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() {
try {
driver.init();
DriverUnit.preconfigureDriver(Driver.driver.get());
driver.get().manage().deleteAllCookies();
driver.get().get(baseUrl);
} catch (TimeoutException e) {
Logger.logEnvironment("QT application is not available");
}
}
@AfterMethod(alwaysRun = true)
default public void end() {
if (driver.get() != null) {
try {
driver.get().quit();
} catch (UnreachableBrowserException e) {
Logger.logDebug("UnreachableBrowser on close");
} finally {
driver.remove();
}
}
}
When I run typical TestNG test method, like:
public class AppUiDemo implements ITestBase {
@Test(enabled = true)
public void checkWebDriverCreation() {
...
}
start()
and end()
methods aren't called. Driver instance isn't created for test execution.
Is it possible to make something like it with default
method and TestNG methods?
If I change the interface to a regular class, before and after methods are called (driver instance is created fine):
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() {
....
The problem is that my test class is already extending another class:
public class MainTest extends ExecutionContext
Thus I can't extend TestBase
.
Is it possible to use interface with any implementation for execution code before and after test methods?