Selenium截图方法一:

Selenium中截图类TakeScreenshout,这个类主要是获取浏览器窗体内的内容,不包括浏览器的菜单和桌面的任务栏区域,我们用百度首页来截图,看看截图效果。

FileUtils.copyFile(srcFile, new File("屏幕截图", time + ".png"));“屏幕截图”是我们自己创建的文件夹用来存放截图文件,此文件夹在project(工程)的更目录;

当然也是可以设置保存到其他目录下:Files.copyFile(srcFile, new File("D:\\资料图片", time + ".png"));

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.google.common.io.Files;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScreenShot {

private static WebDriver driver;
public static void main(String[] args) throws Exception {

System.setProperty("webdriver.chrome.driver",".\\drivers\\chromedriver.exe");

driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();

/**
* 截屏操作
* 图片已当前时间命名
*/
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //转换时间格式
String time = dateFormat.format(Calendar.getInstance().getTime()); //获取当前时间
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //执行屏幕截取
Files.copyFile(srcFile, new File("屏幕截图", time + ".png")); //利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件;"屏幕截图"即时保存截图的文件夹
Thread.sleep(2000);
driver.quit();

}

}

Selenium截图方法二:

Robot截屏

示例代码:(示例中的图片是保存再该工程的根目录下)

package com.sandy;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.imageio.ImageIO;

import com.google.common.io.Files;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.internal.WrapsDriver;

public class ScreenShot {

private static WebDriver driver;
public static void main(String[] args) throws Exception {

System.setProperty("webdriver.chrome.driver",".\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
robotSnapshot();

Thread.sleep(2000);
driver.quit();

}

/**
* 截屏方法二、Robot实现截屏
* @throws Exception
*/
public static void robotSnapshot() throws Exception {
//调用截图方法
BufferedImage img = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(img, "png", new File("robot_screen01.png"));
}

Selenium截图方法三:

在测试的过程中,有时候不需要截取整个屏幕,只需要截取某个元素(或者目标区域)的图片

示例代码:

package com.sandy;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.imageio.ImageIO;

import com.google.common.io.Files;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.internal.WrapsDriver;

public class ScreenShot {

private static WebDriver driver;
public static void main(String[] args) throws Exception {

System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();

WebElement element = driver.findElement(By.id("su"));
elementSnapshot(element);
//System.currentTimeMillis()、Calendar.getInstance().getTimeInMillis()获取时间戳的方法
Files.copyFile(elementSnapshot(element), new File("屏幕截图", System.currentTimeMillis()+".png"));
Thread.sleep(2000);
driver.quit();

}

/**
* 部分截图(元素截图)
* 有时候需要元素的截图,不需要整个截图
* @throws Exception
*/
public static File elementSnapshot(WebElement element) throws Exception {
//创建全屏截图
File screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage image = ImageIO.read(screen);
//获取元素的高度、宽度
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();

//创建一个矩形使用上面的高度,和宽度
Rectangle rect = new Rectangle(width, height);
//元素坐标
Point p = element.getLocation();
BufferedImage img = image.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
ImageIO.write(img, "png", screen);
return screen;
}
}

//进行元素截图,并返回图片路径
public String PartScreenshotFilePath(WebElement element) throws Exception {
//创建时间格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
//创建全屏截图
File fullScreenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage image = ImageIO.read(fullScreenShot);
//获取元素的高度、宽度
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
//创建一个矩形使用上面的高度,和宽度
Rectangle rect = new Rectangle(width, height);
//元素坐标
Point p = element.getLocation();
BufferedImage img = image.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
File partScreenShot = new File("ScreenShots", simpleDateFormat.format(date)+".png");
ImageIO.write(img, "png", partScreenShot);
//获取截图文件路径
String ScreenshotFilePath = partScreenShot.getPath();
return ScreenshotFilePath;
}
//对两次的截图进行对比
public boolean ScreenshotCompare(String oldScreenshotPath, String newScreenshotPath) throws IOException {
//实例化截图文件
File oldFile = new File(oldScreenshotPath);
File newFile = new File(newScreenshotPath);
//读取截图文件
BufferedImage bOldFile = ImageIO.read(oldFile);
BufferedImage bNewFile = ImageIO.read(newFile);
//存储截图文件数据
DataBuffer dOldFile = bOldFile.getData().getDataBuffer();
DataBuffer dNewFile = bNewFile.getData().getDataBuffer();
//获取截图文件尺寸
int oldFileSize = dOldFile.getSize();
int newFileSize = dNewFile.getSize();
//截图文件进行对比
boolean matchFlag = true;
if (oldFileSize == newFileSize) {
for (int i = 0; i < oldFileSize; i++) {
if (dOldFile.getElem(i) != dNewFile.getElem(i)) {
matchFlag=false;
break;
}
}
} else {
matchFlag=false;
}
return matchFlag;
}

Java的selenium代码随笔(8)的更多相关文章

  1. Java的selenium代码随笔(5)

    //以下七种方法主要用于生成年.月.日.小时.分钟和秒的信息,用于生成保存截图的文件目录名和文件名/** 格式化输出日期* * @return 返回字符型日期*/public static Strin ...

  2. Java的selenium代码随笔(2)

    import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatrans ...

  3. Java的selenium代码随笔(1)

    package ShareClass; import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;impor ...

  4. Java的selenium代码随笔(7)

    //判断元素是否存在public boolean IsElementPresent (WebElement webElement, By by) { boolean status = false; t ...

  5. Java的selenium代码随笔(6)

    //获取元素列表public List<WebElement> ListElements(WebElement webElement, By parentBy, By childrenBy ...

  6. Java的selenium代码随笔(4)

    //高亮操作元素public void highlight(WebElement webElement) {JavascriptExecutor javascriptExecutor = (Javas ...

  7. Java的selenium代码随笔(3)

    /** 以下方法主要用于切换页面*/public void SetPageSwitch(String pageTitle) {Set<String> allWindowsHandles = ...

  8. Java使用Selenium几个例子

    零.姿势 Selenium分为两个版本:Selenium RC和Selenium Webdriver.现在用Selenium Webdriver比较多. Selenium是一套工具,而不仅仅是一个操纵 ...

  9. 正则表达式学习笔记(附:Java版示例代码)

    具体学习推荐:正则表达式30分钟入门教程 .         除换行符以外的任意字符\w      word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s        space,空白符 ...

随机推荐

  1. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十三║Vue实战:Vuex 其实很简单

    前言 哈喽大家周五好,马上又是一个周末了,下周就是中秋了,下下周就是国庆啦,这里先祝福大家一个比一个假日嗨皮啦~~转眼我们的专题已经写了第 23 篇了,好几次都坚持不下去想要中断,不过每当看到群里的交 ...

  2. 【Dubbo篇】--Dubbo框架的使用

    一.前述 Dubbo是一种提供高性能,透明化的RPC框架.是阿里开源的一个框架. 官网地址:http://dubbo.io/ 二.架构 组件解释: Provider: 提供者.发布服务的项目.Regi ...

  3. vimium使用

    vimium使用 chrome下面的vimium插件已经慕名已久,迟迟没有做尝试,今天在家有空就熟悉了一下vimium,感觉还是棒棒的.记录一下一些使用心得. 常用按钮 j 向上滚动 k 向下滚动 d ...

  4. Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分

    1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...

  5. 从零开始搭建一个规范的vue-cli 3.0项目

    在这一集我们将讲到如何从安装vue-cli开始,到新建一个本地项目,再到vscode中关于eslint的配置,以及本地项目关联公司远程项目的基本操作. 一,初始化本地项目 1,首先,全局安装vue-c ...

  6. 系列文章|OKR与敏捷(三):赋予团队自主权

    OKR与敏捷开发的原理有着相似之处,但已经使用敏捷的团队再用OKR感觉会显得多余.这种误解的根源就在于对这两种模式不够了解,运用得当的情况下,OKR和敏捷可以形成强强联合的效果,他们可以创造出以价值为 ...

  7. jsom快速入门

    JSON JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法,具有自我描述性.类似 XML, 但比 XML 更小 ...

  8. Linux高级运维 第二章 Linux基本操作和自己动手组装服务器

    2.1  Linux网络相关概念和修改IP地址的方法 2.1.1  网卡的命名规则       Centos 6的网卡命名方式:它会根据情况有所改变而非唯一且固定,在CENTOS6之前,网络接口使用连 ...

  9. 如何将workerman部署到windows服务器上面

    一直以来对php的即时通讯都很好奇,其实是不知道应该怎么来实现,后来了解到了swoole和workerman这两个神器,他们都可以实现即时通信的功能,其中swoole是C语言编写的php扩展,其配置起 ...

  10. 浏览器的同源策略及CORS跨域解决方案 DRF

    一个源的定义 如果两个页面的协议,端口(如果有指定)和域名都相同,则两个页面具有相同的源. 举个例子: 下表给出了相对http://a.xyz.com/dir/page.html同源检测的示例: UR ...