如何通过监听器为 TestNG 的 @BeforeSuite 方法设置超时 class

How to set timeout for TestNG's @BeforeSuite method thru a Listener class

我想通过 Listener class 为 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)) 那样对超时进行硬编码

你可以使用org.testng.IAnnotationTransformer2

Documentation, Java Doc IAnnotationTransformer2

Use this interface instead of IAnnotationTransformer if you want to modify any TestNGannotation besides @Test.

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

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

    annotation.setTimeOut(10);

}