6.1精确比较网页截图图片

  目的:对于核心界面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比较。确认页面是否发生了改变

  被测网页的网址:

  http://www.baidu.com

  Java语言版本的API实例代码  

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod; 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.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class TestCompareImages {
WebDriver driver;
String url = "http://www.baidu.com";
@Test
public void testImageComparison() throws InterruptedException, IOException {
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Thread.sleep(3000);
//对百度首页进行截屏
FileUtils.copyFile(screenshot, new File("e:\\baiduHomePage_actual.jpg"));
//生成两个文件对象,一个是期望图片(期望图片需自己先准备),一个是测试过程中产生的图片
File fileInput = new File("e:\\baiduHomePage_expected.jpg");
File fileOutPut = new File("e:\\baiduHomePage_actual.jpg");
/*
* 以下部分分为两个文件进行像素比对的算法实现,获取文件的像素个数大小,然后使用循环对两张图片进行比对如果有任何一个像素不相同则退出循环
* */
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,"测试过程中的截图和期望的截图并不一致");
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

  

  6.2高亮显示正在被操作的元素

  目的:可以提示测试人员正在操作哪些元素

  被测网页的网址:

  http://www.baidu.com

  Java语言版本的API实例代码 

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class TestHighLightWebElement {
WebDriver driver;
String url = "http://www.baidu.com";
@Test
public void testHighLigHtWebElement() throws InterruptedException {
WebElement searInpuBox = driver.findElement(By.id("kw"));
WebElement submitButton = driver.findElement(By.id("su"));
//调用封装好的的方法高亮显示搜索框
highLightElement(searInpuBox);
searInpuBox.sendKeys("seleium");
//暂停3秒查看效果
Thread.sleep(3000);
//调用封装好的方法高亮显示搜索按钮
highLightElement(submitButton);
Thread.sleep(3000);
submitButton.click();
}
public void highLightElement(WebElement element) {
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("element = arguments[0];" +
"original_style = element.getAttribute('style');" +
"element.setAttribute('style', original_style + \";" +
"background: yellow; border: 2px solid red;\");" +
"setTimeout(function(){element.setAttribute('style', original_style);}, 1000);", element);
}
@BeforeMethod
public void beforeMethod() throws Exception{
System.setProperty("webdriver.chrome.driver","D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
} @AfterMethod
public void afterMethod() throws Exception{
driver.quit();
} }

WebDriver高级应用实例(6)的更多相关文章

  1. WebDriver高级应用实例(10)

    10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/ ...

  2. WebDriver高级应用实例(9)

    9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...

  3. WebDriver高级应用实例(8)

    8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...

  4. WebDriver高级应用实例(7)

    7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...

  5. WebDriver高级应用实例(5)

    5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...

  6. WebDriver高级应用实例(4)

    4.1操作web页面的滚动条 被测网页的网址: http://v.sogou.com Java语言版本的API实例代码 import org.testng.annotations.Test; impo ...

  7. WebDriver高级应用实例(3)

    3.1自动化下载某个文件 被测网页的网址: https://pypi.org/project/selenium/#files Java语言版本的API实例代码 import java.util.Has ...

  8. WebDriver高级应用实例(2)

    2.1在日期选择器上进行日期选择 被测网页的网址: https://www.html5tricks.com/demo/Kalendae/index.html Java语言版本的API实例代码 impo ...

  9. WebDriver高级应用实例(1)

    1.1使用JavaScriptExecutor单击元素 被测网页的网址: http://www.baidu.com Java语言版本的API实例代码 import org.testng.annotat ...

随机推荐

  1. 2018.01.04 bzoj5291: [Bjoi2018]链上二次求和(线段树)

    传送门 线段树基础题. 题意:给出一个序列,要求支持区间加,查询序列中所有满足区间长度在[L,R][L,R][L,R]之间的区间的权值之和(区间的权值即区间内所有数的和). 想题555分钟,写题202 ...

  2. EF对应null的处理

    原来的代码是 if (string.IsNullOrWhiteSpace(seal)) seal = null; ctx.Terminal.FirstOrDefault(ent=>ent.Sea ...

  3. keras backend的修改

    方法一: vim .keras/keras.json 修改“backend”:"tensorflow" 方法二: 每次在python文档中输入, import os os.envi ...

  4. bind函数(c++11)

    1.概念 1)c++11使用bind()函数可以向函数传递参数,一般调用形式为: 返回的newCallable是一个可调用的对象,callable是需要传参的函数,arg_list是参数列表 2)bi ...

  5. BZOJ 1444 [Jsoi2009]有趣的游戏 (AC自动机 + 概率DP + Gauss)

    1444: [Jsoi2009]有趣的游戏 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1382  Solved: 498[Submit][Statu ...

  6. ckeditor粘帖上传图片控件-更新-2.0.15版本

    泽优Word图片上传产品测试 泽优Word图片上传控件WordPaster2,基于php开发环境测试. 泽优软件官网Word图片上传产品介绍页面: http://www.ncmem.com/webap ...

  7. Ubuntu下安装并配置TexStudio

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7069715.html Ubuntu下安装并配置TexStudio Ubunt ...

  8. codeblocks+SDCC开发51单片机

    说到51,大部分人都是用的是KEIL开发环境,但是KEIL是商业软件,我们一般人都用的是破解版的,如果用于商业就会收到法律诉讼.然而有一款很好的编译器专为51内核而存在.SDCC最大的有点就是开源免费 ...

  9. Mysql逻辑分层、存储引擎

    Mysql的逻辑分层: 连接层 服务层 引擎层 存储层 常见的数据库引擎有InnorDB和MylSAM. InnorDB:事物优先,(适合高并发操作:行锁,顾名思义一次锁一行数据) MylSAM:性能 ...

  10. jvm虚拟机---执行引擎子系统

    Java虚拟机只与Class文件相关联,它规定了Class文件应该具有的格式,而不论该文件是由什么语言编写并编译而来.所以,任何语言只要能够最终编译成符合Java虚拟机要求的Class文件,就可以运行 ...