import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.log4testng.Logger;

public class TestngRetry implements IRetryAnalyzer{
    private static Logger logger = Logger.getLogger(TestngRetry.class);
    private int retryCount = 1;
    private static int maxRetryCount;
    static {
        //外围文件配置最大运行次数
        maxRetryCount = 3;
}
    @Override
    public boolean retry(ITestResult result) {
        if (retryCount <= maxRetryCount) {
            String message = "running retry for  '" + result.getName() + "' on class " + this.getClass().getName() + " Retrying "
                    + retryCount + " times";
            logger.info(message);
            Reporter.setCurrentTestResult(result);
            Reporter.log("RunCount=" + (retryCount + 1));
            retryCount++;
            return true;
        }
        return false;
    }

}
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;
import org.testng.internal.annotations.IAnnotationTransformer;

public class RetryListener implements IAnnotationTransformer{

    @Override
    public void transform(ITestAnnotation annotation, Class testClass,
            Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();
        if(retry == null){
            annotation.setRetryAnalyzer(TestngRetry.class);
        }

    }

}

具体测试用例如下:

@Test(description="zy.com.Public",retryAnalyzer=RetryListener.class)
  public void testBegin()throws Exception{
      Thread.sleep(5000);
      for(int i=0;i<=6;i++){
          publicMethod.scrollTo(driver);
          Thread.sleep(3000);
      }
      WebElement el = driver.findElement(By.id(id.get("Slpash_begin")));
      Thread.sleep(1000);
          el.click();
          Thread.sleep(4000);
  }

运行之后,该监听没有响应,不知道是什么原因,麻烦各位路过的高手指点迷津。


↙↙↙阅读原文可查看相关链接,并与作者交流