/**
对比图片进行校验是否成功
**/
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. Spring boot学习一

    SSM框架:Spring+SpringMVC+MyBatisSpring Boot一.pom文件:pom.xml中添加:    <!--spring-boot-starter-web提供了对we ...

  2. magento目录结构说明,Magento文件夹结构说明,Magento folder structure

    /app – 程序根目录     /app/etc – 全局配置文件目录     /app/code – 所有模块安装其模型和控制器的目录     /app/code/core – 核心代码或经过认证 ...

  3. Python基础-字符编码与转码

    ***了解计算机的底层原理*** Python全栈开发之Python基础-字符编码与转码 需知: 1.在python2默认编码是ASCII, python3里默认是utf-8 2.unicode 分为 ...

  4. leetcode 28

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. WORD学习之制作座位表

    1.点击插入-表格-插入表格,将表格的列数设置为6,行数为10,再点击“确定”按钮

  6. Python对时间的转换

    1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 import time timeArray = time.strpt ...

  7. Coursera台大机器学习课程笔记4 -- Training versus Testing

     这节的主题感觉和training,testing关系不是很大,其根本线索在于铺垫并求解一个问题:    为什么算法PLA可以正确的work?因为前面的知识告诉我们,只有当假设的个数有限的时候,我们才 ...

  8. html5与html4的区别,如何一眼区分

    还是在面试过程中遇到的这个问题,但是当时感觉回答的不是很全面,回来以后自己做的总结: 以上这个问题如果教大家看源码,可能一眼很直观的就能认出,那个是html4,那个是html5,但是面试的时候问道这个 ...

  9. linux修改IP

     linux命令行修改IP的2个方法 2010-12-30 10:25:50 分类: LINUX 方式一: ifconfig eth0 192.168.1.18 netmask 255.255.255 ...

  10. Spring透过ApplicationListener来触发contextrefreshedevent事件

    Spring通过ApplicationListener接口来触发contextrefreshedevent事件在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据.加载 ...