I want to know if it is possible to use default methods from the interface with the TestNG @BeforeMethod test?
Here is an example 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 the typical TestNG test method, for example:
public class AppUiDemo implements ITestBase { @Test(enabled = true) public void checkWebDriverCreation() { ... }
start() and end() methods are not called. A driver instance is not created to run the test.
Is it possible to do something similar with the default method and TestNG methods?
If I changed the interface to a regular class, before and after calling the methods (the driver instance is created perfectly):
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 cannot extend TestBase .
Is it possible to use an interface with any implementation for the execution code before and after the testing methods?
source share