zoukankan      html  css  js  c++  java
  • selenium 截图 添加时间戳

    在自动化程序中运行的代码报错信息或者是相关日志有可能并无法直观的判断出错信息。因此截图是避免不了的。为了避免因为重复运行或者是图片名称相同导致截图被覆盖。

    建议在截图时使用时间戳,保证截图图片名称的唯一性。

     1 import java.io.File;
     2 import java.io.IOException;
     3 import java.text.DateFormat;
     4 import java.text.SimpleDateFormat;
     5 import java.util.Calendar;
     6 import org.apache.commons.io.FileUtils;
     7 import org.openqa.selenium.OutputType;
     8 import org.openqa.selenium.TakesScreenshot;
     9 import org.openqa.selenium.WebDriver;
    10 import org.openqa.selenium.chrome.ChromeDriver;
    11 import org.openqa.selenium.chrome.ChromeOptions;
    12 
    13 public class GetImg {
    14     public static void main(String[] args) {
    15         System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");
    16         ChromeOptions Options = new ChromeOptions();
    17         Options.addArguments("user-data-dir=C:\Users\happy\AppData\Local\Google\Chrome\User Data");
    18         WebDriver driver = new ChromeDriver(Options);
    19         driver.get("https://www.baidu.com/");
    20         // 生成以时间戳为名的截图,避免图片重复导致的覆盖
    21         DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    22         Calendar calendar = Calendar.getInstance();
    23         String imageName = df.format(calendar.getTime());
    24         // 进行截图 并把截图保存在指定位置。
    25         File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    26         try {
    27             FileUtils.copyFile(srcFile, new File("d:\" + imageName + ".png"));
    28         } catch (IOException e) {
    29 
    30             e.printStackTrace();
    31         }
    32         driver.close();
    33         driver.quit();
    34 
    35     }
    36 }
  • 相关阅读:
    CentOS7.2中安装MongoDB
    django 面试题
    python pandas库——pivot使用心得
    归并排序
    python实现归并排序,归并排序的详细分析
    二分法查找
    二叉树的遍历
    RabbitMQ(python实现)学习之一:简单两点传输“Hello World”的实现
    邻接表存储图,DFS遍历图的java代码实现
    五、python使用模块
  • 原文地址:https://www.cnblogs.com/linxinmeng/p/6957440.html
Copyright © 2011-2022 走看看