Java+Selenium+Testng-web UI自动化测试框架-9用例执行失败时截图

自动化测试用例运行过程中,可根据意愿进行一些截图记录,本框架是在用例执行失败时进行截图。

在util包下新建TakeScreenshot.class代码如下

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

/**
 * 截图.
 */
public class TakeScreenshot extends BaseFunction{
    
    public String takeScreenShot(String classname, String methodname) {
        // 获取截图file
        File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        String filePath = getFilePath(classname, methodname);
        try {
            // 将图片移动到指定位置
            FileUtils.moveFile(file, new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }


    public String getFilePath(String classname, String methodname) {
        // 创建储存图片的路径,不存在则创建
        File dir = new File("test-output/report/screenshots");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        String dateStr = dateFormat.format(new Date());
        // 获取新的文件名,包含时间,类名,方法名
        String fileName = dateStr + "_" + classname + "_" + methodname + ".jpg";
        // 获取文件路径
        String filePath = dir.getAbsolutePath() + "/" + fileName;
        return filePath;

    }
}

 因为本框架是在用例执行失败时截图的,所以在上文介绍的ReportListener类中的onTestFailure(ITestResult result)方法中调用takeScreenShot方法。

   @Override  
    public void onTestFailure(ITestResult result) {  
        String error;
        try {            
        StringBuilder sb2 = new StringBuilder("<tr><td align=\"center\">");    
        sb2.append((result.getMethod().getRealClass()+"."+result.getMethod().getMethodName()).substring(16));    
        sb2.append("</td><td align=\"center\"><font color=\"red\">Failed</font><br>"); 
        sb2.append(result.getTestName());
        sb2.append("<p align=\"center\">用例执行<font color=\"red\">失败</font>,原因:<br>");  
        sb2.append("<br><a style=\"background-color:#CCCCCC;\">");          
        Throwable throwable = result.getThrowable();    
        if(throwable.getMessage().indexOf("Session info")!=-1) 
        {
            int endIndex = throwable.getMessage().indexOf("(Session info");     
            error = throwable.getMessage().substring(0, endIndex);
         }
        else 
        {
            error = throwable.getMessage();//断言失败只打印断言
        }
        sb2.append(error);
        sb2.append("</a></p></td>");  
        
        long time =
                (result.getEndMillis() - result.getStartMillis())/1000;
        sb2.append("</td><td align=\"center\">" + time + "</td></tr>");       
        s2.append(sb2);
        fail = fail+1;
        String classname = result.getTestClass().getName();
        String methodname = result.getMethod().getMethodName();
        TakeScreenshot shot = new TakeScreenshot();
        String screenshotPath = shot.takeScreenShot(classname, methodname);
        screenshotPaths.add(screenshotPath);       
        }
        catch(Exception e) {        
            Log4jUtil.error(e);
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42409365/article/details/88388938