51Testing软件测试论坛

标题: testng对失败时截图处理 [打印本页]

作者: 天天酷派哦    时间: 2018-4-10 15:23
标题: testng对失败时截图处理
  1. 1.截图类:


  2. public class ScreenShot {
  3.     public WebDriver driver;

  4.     public ScreenShot(WebDriver driver) {
  5.         this.driver = driver;
  6.     }

  7.     private void takeScreenshot(String screenPath) {
  8.         try {
  9.             File scrFile = ((TakesScreenshot) driver)
  10.                     .getScreenshotAs(OutputType.FILE);
  11.             FileUtils.copyFile(scrFile, new File(screenPath));
  12.         } catch (IOException e) {
  13.             System.out.println("Screen shot error: " + screenPath);
  14.         }
  15.     }

  16.     public void takeScreenshot() {
  17.         String screenName = String.valueOf(new Date().getTime()) + ".jpg";
  18.         File dir = new File("test-output/snapshot");
  19.         if (!dir.exists())
  20.             dir.mkdirs();
  21.         String screenPath = dir.getAbsolutePath() + "/" + screenName;
  22.         this.takeScreenshot(screenPath);      
  23.     }
  24. }
  25. 2.我们可以用testng的一个监听器来监听错误时截图:

  26. public class DotTestListener extends TestListenerAdapter {

  27.     @Override
  28.     public void onTestFailure(ITestResult tr) {   
  29.          
  30.     }

  31. }
  32. 3.也就是说我们只需要在onTestFailure方法里面调用ScreenShot类里面的takeScreenshot方法即可,
复制代码







欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2