selenium截图对比校验方法
/**
对比图片进行校验是否成功
**/
package com.allin.pc;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestCompareImages {
public WebDriver driver;
private String baseUrl;
@BeforeClass
public void setUp(){
baseUrl = "http://sogou.com";
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(baseUrl);
driver.findElement(By.xpath(".//*[@id='query']")).sendKeys("123");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterClass
public void tearDown(){
driver.close();
}
@Test
public void testImageComparison() throws InterruptedException, IOException{
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Thread.sleep(3000);
//对sogo首页进行截屏
FileUtils.copyFile(screenshot, new File("d:\\sogouHomePage_actual.jpg"));
//生成了两个文件对象,一个是期望的图片,一个是实际测试过程中产生的图片
File fileInput = new File("d:\\sogouHomePage_expected.jpg");
File fileOutPut = new File("d:\\sogouHomePage_actual.jpg");
/*
以下部分为两个文件进行像素比对的算法实现,获取文件的像素个数大小,然后使用循环的方式将两张图片的
所有项目进行一一对比,如有任何一个像素不相同,则退出循环,将matchFlag变量的值设定为false,
最后使用断言语句判断matchFlag是否为true。如果为true表示两张图片完全一致,如果为false
表示两张图片并不是完全匹配
*/
BufferedImage bufileInput = ImageIO.read(fileInput);
DataBuffer dafileInput = bufileInput.getData().getDataBuffer();
int sizefileInput = dafileInput.getSize();
BufferedImage bufileOutPut = ImageIO.read(fileOutPut);
DataBuffer dafileOutPut = bufileOutPut.getData().getDataBuffer();
int sizefileOutPut = dafileOutPut.getSize();
Boolean matchFlag = true;
if(sizefileInput == sizefileOutPut){
for(int j = 0; j<sizefileInput; j ++){
if(dafileInput.getElem(j) != dafileOutPut.getElem(j)) {
matchFlag = false;
break;
}
}
}
else
matchFlag = false;
Assert.assertTrue("测试过程中截图与期望截图不一致", matchFlag );
}
}
selenium截图对比校验方法的更多相关文章
- Java&Selenium截图方法封装
Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...
- uiautomator——简单的将自动化测试与截图对比相结合使用的小例子!
1.在使用uiautomator进行自动化测试过程当中,执行某些动作之后,要进行判断是否已执行的动作成功时,需要添加检查点来判断,因此添加判断点除了使用id.text.view等方式进行之外,存在一种 ...
- python——几种截图对比方式!
本次记录的几种截图对比方式,主要是为了在进行手机自动化测试时,通过截图对比来判断测试的正确性,方式如下: # -*- coding: utf- -*- ''' 用途:利用python实现多种方法来实现 ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...
- Selenium常见元素定位方法和操作的学习介绍
参考地址: https://www.cnblogs.com/eastmount/p/4810690.html 这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操 ...
- Selenium ActionChains、TouchAction方法
ActionChains和TouchAction可以用来模拟点击.双击.滑动等事件.ActionChains用于执行PC端的鼠标移动.按键.拖拽等事件:TouchActions用法与ActionCha ...
- JS常用校验方法(判断输入框是否为空,数字,电话,邮件,四舍五入等)
JS常用校验方法: 1.判断输入框是否为空,为空时弹出提示框 2.关闭窗口 3.检查输入字符串是否为数字 4.强制把大写转换成小写 5.手机号码校验,长度为11位数字. 6.电子邮件校验 7.电话号码 ...
- selenium+python定位元素方法
定位元素方法 官网地址:http://selenium-python.readthedocs.org/locating-elements.html 这里有各种策略用于定位网页中的元素(l ...
- Python_selenium二次封装selenium的几个方法
Python_selenium二次封装selenium的几个方法 将常用的几个webdriver方法封装到自己写的一个类中去,此实例中是将"浏览器后退.浏览器前进.打开站点和关闭浏览器&qu ...
随机推荐
- tomcat404报错解决
本地启动tomcat,不报错.也可正常打开http://localhost:8080 但是eclipse中启动tomcat不报错.但是打开http://localhost:8080时报404 思考,原 ...
- 使用clearInterval清除计时循环时,最后一次循环还是会执行解决办法
原代码: var interv=setInterval(function(){ alert("setInterval执行"); },2000) clearInterval(inte ...
- 纯手写SpringMVC架构,用注解实现springmvc过程
1.第一步,首先搭建如下架构,其中,annotation中放置自己编写的注解,主要包括service controller qualifier RequestMapping 第二步:完成对应的anno ...
- matlab直方图均衡,使用向量优化
matlab自带有histeq函数对图像进行直方图均衡 自己写了一个,改成向量化形式,效率提高了一点,但是比自带的还是差很多,差不多9倍 function D = my_histeq(I) [m,n] ...
- Big Data
Hadoop安装教程_伪分布式配置_CentOS6.4/Hadoop2.6.0 http://dblab.xmu.edu.cn/blog/install-hadoop-in-centos/ Spark ...
- Spring操作指南-针对JDBC配置声明式事务管理(基于XML)
- Spring操作指南-AOP基本示例(基于XML)
- 修复Magento SQLSTATE[23000]: Integrity constraint
magneto在意外情况下报错Magento SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry,出现这个问题最 ...
- Unity Mono IDE Setting
Mac: 修改快捷键 删除一整行 Delete Entire Line Command+D Format Document Shift+Command+F 在文件中查找 Fin in Fil ...
- 细数那些我们都习惯了的Java谣言
我是一个Java的反对者,至于为什么,我想最大的一个原因是它不实在,不管是当年sun所说的一些言论,还是如今Java用户的一些言论,都有蛊惑之嫌,甚至很多太假了,而这些言论层出不穷,其实就语言本身我不 ...