How to call the default method from the interface with the TestNG and Selenium tests?

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?

+5
source share
2 answers

Yes, it’s possible, but the TestNG version, which allows you to use interface methods in tests, has not yet been released. You need to download it from this repository.

If you are using Maven , you can specify an additional repository in pom.xml :

 <repository> <id>testng</id> <name>testng</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </repository> 

And then add the TestNG dependency:

 <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.11.1-SNAPSHOT</version> </dependency> 

Example:

 package test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class DummyTest implements ITest { @BeforeMethod public void beforeTest() { System.out.println("before from class"); } @Test public void test1() { System.out.println("I am test1"); } } 

and ITest interface

 package test; import org.testng.annotations.BeforeMethod; public interface ITest { @BeforeMethod default void beforeDefaultInterface() { System.out.println("before from default interface method"); } @BeforeMethod static void beforeStaticInterface() { System.out.println("before from static interface method"); } } 

Test result above:

  before from default interface method
     before from static interface method
     before from class
     I am test1
+5
source

Possible with TestNg version '6.14.2'

An alternative repo is no longer required.
I have not tried other versions.

0
source

Source: https://habr.com/ru/post/1273447/


All Articles