【问题标题】:How to call default method from interface with TestNG tests and Selenium?如何从带有 TestNG 测试和 Selenium 的接口调用默认方法?
【发布时间】:2018-04-30 11:38:40
【问题描述】:

我想知道是否可以使用带有 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();
            }
        }
    }

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

public class AppUiDemo implements ITestBase {
    @Test(enabled = true)
    public void checkWebDriverCreation() {
      ...
    }

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() {
    ....

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

public class MainTest extends ExecutionContext

因此我无法扩展TestBase

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

【问题讨论】:

  • 我不关注。我在您的界面中看到了一些 static 方法,但没有看到默认方法。我还在您的界面中看到非static、非final 字段,那么给出了什么? “它不起作用”是指“它无法编译”吗?因为不,它不会。
  • @JohnBollinger 我修正了错字。我也尝试过静态方法。
  • @JohnBollinger 你错了。我愿意。此外,String baseUrl 默认为 public static final。并键入类似的内容不是强制性的。在我看来,它是多余的,并且使可读性变得更加困难。

标签: java selenium-webdriver java-8 testng default-method


【解决方案1】:

是的,这是可能的,但允许在测试中使用接口方法的 TestNG 版本尚未发布。您需要从this 存储库下载它。

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

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

然后添加TestNG依赖:

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

示例:

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");
    }
}

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");
    }
}

上述测试的输出将是:

从默认接口方法之前 从静态接口方法之前 上课前 我是测试1

【讨论】:

  • 太棒了!我想知道 TestNG 方面是否仍在进行中
  • @Anton 能否提供更多信息如何从您共享的存储库中获取?我在那里找不到testng
  • 用 maven 的例子更新了我的答案。可以找到 jar 文件的确切链接是 oss.sonatype.org/content/repositories/snapshots/org/testng/…
【解决方案2】:

TestNg 版本“6.14.2”可以实现

替代回购,不再需要。
其他版本我没试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2018-08-28
    • 2019-03-03
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多