/**
对比图片进行校验是否成功
**/
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. java字典序全排列

    import java.util.Arrays; /** *字典序全排列 *字符串的全排列 *比如单词"too" 它的全排列是"oot","oto&q ...

  2. BI实施过程中的工具与服务

    成功的BI项目,不仅仅是应用了BI工具软件,还要具备完善的BI服务体系,才能称之为真正成功的商业智能bi项目. 现在的BI(商业智能)比起几年前的ERP一样,成为CIO们关注的焦点.在ERP等基础信息 ...

  3. MVC中的成员资格,授权,安全性

    使用 Authorize 特性登录 Authorize 是 ASP.NET MVC 自带的默认授权过滤器, 可用来限制用户对操作方法的访问. 保护控制器操作 Authorize 特性在表单身份验证和 ...

  4. ue4 ios

    project settings package 可以指定非APK打包,确定资源是否发布 可以指定content下某个目录所有文件(非.uassert)都打包 ios环境下fopen打开文件需要指定路 ...

  5. asp.net mvc 5 web api 关于Requested resource does not support options 问题

    1.用visual studio 2015 建立一个 web api 应用程序.记住这是一个 web api 应用. 2.新建一个web api . 3.用C#访问,代码如下:[没有问题,返回正确] ...

  6. SublimeText的奇特应用

    1.ctrl+shift+D:选中文本,粘贴复制 2.ctrl+D:批量选择文本 3.zen-coding:Tab键

  7. HDU5870 Alice's Adventure in Wonderland

    大概做法是这样的 考虑最朴素的做法,预处理出1到所有点的最短路数组dis1和方案数数组cnt1,和预处理出n到所有点的最短路数组dis2和方案数数组出cnt2,然后暴力枚举点对(A,B),如果A和B之 ...

  8. java选择排序

    /** * 选择排序 * @param a * @date 2016-10-8 * @author shaobn */ public static void selectSort(int[] a){ ...

  9. Java中关于先有鸡还是先有蛋的问题----Class&Object

    在Java中,我们常常会看到一个类型:Class.并且在类似Person.class,cache.getClass()等代码中见到它的身影. 众所周知,Class是用来描述一个类的类型,而Object ...

  10. Python各式装饰器

    Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义. 一.函数式装饰器:装饰器本身是一个函数. 1.装饰函数:被装饰对象是一个函数 [1]装饰器无参数: a.被装饰对象无参数: ...