如何从TestNG测试和Selenium接口调用默认方法?

naz*_*art 5 java testng java-8 selenium-webdriver default-method

我想知道是否可以使用来自TestNG @BeforeMethod注释的接口的默认方法?

这是我试过的样本:

@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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我运行典型的TestNG测试方法时,如:

public class AppUiDemo implements ITestBase {
    @Test(enabled = true)
    public void checkWebDriverCreation() {
      ...
    }
Run Code Online (Sandbox Code Playgroud)

start()并且end()不调用方法.未为测试执行创建驱动程序实例.

是否可以使用default方法和TestNG方法制作类似的东西?

如果我将接口更改为常规类,则调用方法之前和之后(驱动程序实例创建正常):

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() {
    ....
Run Code Online (Sandbox Code Playgroud)

问题是我的测试类已经扩展了另一个类:

public class MainTest extends ExecutionContext
Run Code Online (Sandbox Code Playgroud)

因此我无法延伸TestBase.

是否可以在测试方法之前和之后对执行代码的任何实现使用接口?

小智 5

是的,这是可能的,但允许在测试中使用接口方法的TestNG版本尚未发布.你需要下载它从这个仓库.

如果您使用Maven,您可以在以下位置指定其他存储库pom.xml:

<repository>
    <id>testng</id>
    <name>testng</name>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
Run Code Online (Sandbox Code Playgroud)

然后添加TestNG依赖:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.11.1-SNAPSHOT</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

例:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

ITest界面

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

上述测试的输出将是:

    before from default interface method
    before from static interface method
    before from class
    I am test1