一、截图

1. Firefox浏览器截图

FirefoxDriver firefoxDriver = new FirefoxDriver();
firefoxDriver.getScreenshotAs(OutputType type);

2. Chrome浏览器截图

ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.getScreenshotAs(OutputType type);

3. IE浏览器的截图

InternetExplorerDriver ieDriver = new InternetExplorerDriver();
ieDriver.getScreenshotAs(OutputType type);

二、代码实现

1.创建浏览器驱动参考:https://www.cnblogs.com/xiaozhaoboke/p/11074562.html

2.浏览器截图实现代码:

package cn.test;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;
import cn.test.BaseCase; public class Screenshots extends BaseCase {
@Test
public void test() {
try {
driver.get("http://www.baidu.com");//获取当前系统时间
Date date = new Date();
//定义系统时间输出格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String strTime = sdf.format(date);
File img =null;
if(driver instanceof ChromeDriver) {
//将driver向下转型chromeDriver
ChromeDriver chromeDriver = (ChromeDriver)driver;
//调用chromeDriver.getScreenshotAs()方法得到截图文件
img= chromeDriver.getScreenshotAs(OutputType.FILE);
}else if(driver instanceof FirefoxDriver){
FirefoxDriver firefoxDriver = (FirefoxDriver)driver;
img = firefoxDriver.getScreenshotAs(OutputType.FILE);
}else if(driver instanceof InternetExplorerDriver) {
InternetExplorerDriver ieDriver = (InternetExplorerDriver)driver;
img = ieDriver.getScreenshotAs(OutputType.FILE);
}
//使用commons.io包提供的FileUtils工具类,直接copy一份保存到指定路径下
//copyFile方法传入的都是File类型
FileUtils.copyFile( img, new File("src/test/resources/"+strTime+"_img.png"));
} catch (Exception e) {
e.printStackTrace();
} }
}

学习后总结,不足之处后续补充,未完待续。。。

最新文章

  1. Ue4的容器(数据结构)
  2. 一些关于Viewport与device-width的东西~
  3. OpenJudge计算概论-文字排版
  4. vc2008程序发布指南
  5. Umbraco中的ModelBuilder
  6. UEFI+GPT下安装Win10+Ubuntu16.04双系统
  7. Ubuntu14.04安装pycharm用于Python开发环境部署,并且支持pycharm使用中文输入
  8. Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示
  9. POI 生成、导出Excel(包含多个sheet)带 图片
  10. C#格式化字符串大全
  11. C++ 获取当前正在执行的函数的相关信息
  12. NYOJ16|嵌套矩形|DP|DAG模型|记忆化搜索
  13. 图像增强算法(直方图均衡化、拉普拉斯、Log、伽马变换)
  14. vector 简介
  15. NodeJS下的Mongodb操作
  16. C++设计模式 之 “对象创建”模式:Factory Method、Abstract Factory、Prototype、Builder
  17. 使用zookeeper自带的zkCli.sh客户端工具实现对zk的CURD常见操作详解
  18. Exchange Server 2016 管理邮箱收发限制
  19. mysql查询日期相关的
  20. Bootstrap组件系列之福利篇几款好用的组件(推荐)

热门文章

  1. 一文让你彻底理解SELECT语句的执行逻辑
  2. CF1322B-Present【双指针】
  3. NOI.AC#2144-子串【SAM,倍增】
  4. P4643-[国家集训队]阿狸和桃子的游戏【结论】
  5. python操作Redis方法速记
  6. ch_nginx.sh
  7. css新增属性之边框
  8. Oracle基础命令操作总结
  9. Spring框架访问数据库的两种方式的小案例
  10. [JUC-5]ConcurrentHashMap源码分析JDK8