首页 > 解决方案 > 如何通过 Listener 类为 TestNG 的 @BeforeSuite 方法设置超时

问题描述

我想通过 Listener 类为 BeforeSuite 方法设置一些超时。我找不到这方面的帮助。(我正在使用 Selenium WebDriver)我有很多测试套件,我们为每个测试设置了超时时间,并且还想限制 BeforeSuite 方法的时间。我想通过一个监听器来设置它,而不是在每个套件中添加它。

@BeforeSuite(groups = {"aa-bb-cc"})
    public void abcdef(ITestContext context) throws Exception {
        ....}
@AfterSuite(groups = {"aa-bb-cc"})
    public void cleanup() {
        quitDriver();
    }

如何使用侦听器为上述 2 种方法设置超时(而不是像 @BeforeSuite(groups = {"aa-bb-cc"}, timeOut=600000) 那样对超时进行硬编码)

标签: selenium-webdrivertestng

解决方案


您可以使用 org.testng.IAnnotationTransformer2

文档, Java 文档 IAnnotationTransformer2

如果要修改除 @Test 之外的任何 TestNGannotation,请使用此接口而不是 IAnnotationTransformer。

您将需要覆盖具有参数 IConfigurationAnnotation 的方法

@Override
public void transform(IConfigurationAnnotation annotation, Class testClass,
        Constructor testConstructor, Method testMethod) {

    annotation.setTimeOut(10);

}

推荐阅读