java+selenium+testng(四)浏览器截图

在我们自动化测试中,如果跑到失败用例,除了日志查看失败位置,最好还有执行结果截图作为依据。

以下是浏览器截图的API

public static File takeScreenShot(String path){
    File file = null;
    if(browserName.equals("chrome")){
        //目前只写chrome
        ChromeDriver chromeDriver = (ChromeDriver) BrowserUtil.getDriver();
        //生成截图类型
        file = chromeDriver.getScreenshotAs(OutputType.FILE);
    }        
    File descFile = new File(path);
    //截图保存到本地
    try {
        FileUtils.copyFile(file, descFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return descFile;
}

 

posted @ 2020-07-23 14:50  小町  阅读(275)  评论(0编辑  收藏  举报