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