/**
对比图片进行校验是否成功
**/
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截图对比校验方法的更多相关文章

  1. Java&Selenium截图方法封装

    Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...

  2. uiautomator——简单的将自动化测试与截图对比相结合使用的小例子!

    1.在使用uiautomator进行自动化测试过程当中,执行某些动作之后,要进行判断是否已执行的动作成功时,需要添加检查点来判断,因此添加判断点除了使用id.text.view等方式进行之外,存在一种 ...

  3. python——几种截图对比方式!

    本次记录的几种截图对比方式,主要是为了在进行手机自动化测试时,通过截图对比来判断测试的正确性,方式如下: # -*- coding: utf- -*- ''' 用途:利用python实现多种方法来实现 ...

  4. [python爬虫] Selenium常见元素定位方法和操作的学习介绍

    这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...

  5. Selenium常见元素定位方法和操作的学习介绍

    参考地址: https://www.cnblogs.com/eastmount/p/4810690.html 这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操 ...

  6. Selenium ActionChains、TouchAction方法

    ActionChains和TouchAction可以用来模拟点击.双击.滑动等事件.ActionChains用于执行PC端的鼠标移动.按键.拖拽等事件:TouchActions用法与ActionCha ...

  7. JS常用校验方法(判断输入框是否为空,数字,电话,邮件,四舍五入等)

    JS常用校验方法: 1.判断输入框是否为空,为空时弹出提示框 2.关闭窗口 3.检查输入字符串是否为数字 4.强制把大写转换成小写 5.手机号码校验,长度为11位数字. 6.电子邮件校验 7.电话号码 ...

  8. selenium+python定位元素方法

    定位元素方法 官网地址:http://selenium-python.readthedocs.org/locating-elements.html        这里有各种策略用于定位网页中的元素(l ...

  9. Python_selenium二次封装selenium的几个方法

    Python_selenium二次封装selenium的几个方法 将常用的几个webdriver方法封装到自己写的一个类中去,此实例中是将"浏览器后退.浏览器前进.打开站点和关闭浏览器&qu ...

随机推荐

  1. 响应式布局 Bootstrap

    github介绍 (1)简单灵活可用于架构流行的 用户界面和交互接口 的html.css.javascript工具集 (2)基于html5.css3的bootstrap,具有大量的诱人特征: 友好的学 ...

  2. CSS使用position定位后导致元素浮动

    1.子元素 absolute/fixed定位后,子元素脱离文档流存在,它让出原来占的那个坑,父元素再也不能通过子元素来撑开高度了 <style> div{ position:absolut ...

  3. ACM集训的1B。。。。黑色星期五。。。。2333333

    题目: 印象中有好多个13号是星期五,13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900 ...

  4. java.lang.IllegalArgumentException: Result Maps collection does not contain value for java.lang.Integer

    今天做springmvc+mybatis+spring的项目的时候发现了一个异常.如下: org.apache.ibatis.builder.IncompleteElementException: C ...

  5. jQuery 隐藏/显示

    1.如何隐藏部分文本(简单案例) //样式 <style type="text/css">            div.ex {                bac ...

  6. PhpStorm 4.0 & 5.0 部署本地Web应用 (转)

    1.创建新的项目(project),创建完成之后单击工具栏的应用运行/调试(Select Run/Debug Configuration)的下拉菜单弹出 Edit Cofigurations选项,单击 ...

  7. JAVA静态代码审查之checkstyle

    技术总监来巡查,刚巧前段时间遇到了一个问题还没解决,就拉着大牛开问.结果,问题是解决了,还附带了另一个问题,或是要求出来,没啥技术含量,但是很麻烦的一个东西:代码格式. 之前我写代码,因为屏幕比较小, ...

  8. java随笔

    java与c++的几点区别 (1)Java比C++程序可靠性更高.有人曾估计每50行C++程序中至少有一个BUG.姑且不去讨论这个数字是否夸张,但是任何一个C++程序员都不得不承认C++语言在提供强大 ...

  9. com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command ' finished with non-zero exit value 1

    Error:Execution failed for task ':lenovoAlbum:processReleaseResources'. > com.android.ide.common. ...

  10. java线程(2016-4-7)

    Thread.yield()的调用是对线程调度器(Java线程机制的一部分,可以将CPU从一个线程转移到另一个线程)的一种建议,它在声明:"我已经执行完生命周期中最重要的部分了,此刻正是切换 ...