WEB端UI自动化测试(十)截屏

在自动化执行过程中,难免会有步骤执行错误,我们希望能看到当时的具体场景,通过截屏可以实现

一、图片命名方式

一般包含时间,事件,这样我们就能知道是在哪个时间哪个方法上截的图

时间取当前系统时间Date(),使用SimpleDateFormat()来转换格式,代码如下

public class DateTime {
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
    public String DateTime() {
        return dateFormat.format(date);//将系统时间按照指定格式返回
    }
}

事件可以是当前类名、方法名,但是因为类名和方法名是英文的,并不直观,所以可以在你预期会截屏的地方直接写中文。

//获取当前类名        
String clazz = this.getClass().getName();

//获取当前方法
String method = Thread.currentThread() .getStackTrace()[1].getMethodName();

 

二、图片保存位置

1.selenium封装了getScreenshotAs()方法来截屏,默认保存在temp目录下

File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

2.将截图拷贝到指定目录下,并重命名

String screenPath = dir.getAbsolutePath() + "\\" + screenName;//getAbsolutePath绝对路径名,screenName是第一步中生成的文件名
FileUtils.copyFile(scrFile, new File(screenPath));

 

posted @ 2019-10-17 19:51  归藏  阅读(418)  评论(0)    收藏  举报