WebDriver高级应用实例(6)
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)的更多相关文章
- WebDriver高级应用实例(10)
10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/ ...
- WebDriver高级应用实例(9)
9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...
- WebDriver高级应用实例(8)
8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...
- WebDriver高级应用实例(7)
7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...
- WebDriver高级应用实例(5)
5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...
- WebDriver高级应用实例(4)
4.1操作web页面的滚动条 被测网页的网址: http://v.sogou.com Java语言版本的API实例代码 import org.testng.annotations.Test; impo ...
- WebDriver高级应用实例(3)
3.1自动化下载某个文件 被测网页的网址: https://pypi.org/project/selenium/#files Java语言版本的API实例代码 import java.util.Has ...
- WebDriver高级应用实例(2)
2.1在日期选择器上进行日期选择 被测网页的网址: https://www.html5tricks.com/demo/Kalendae/index.html Java语言版本的API实例代码 impo ...
- WebDriver高级应用实例(1)
1.1使用JavaScriptExecutor单击元素 被测网页的网址: http://www.baidu.com Java语言版本的API实例代码 import org.testng.annotat ...
随机推荐
- 2019.01.01 bzoj3625:小朋友和二叉树(生成函数+多项式求逆+多项式开方)
传送门 codeforces传送门codeforces传送门codeforces传送门 生成函数好题. 卡场差评至今未过 题意简述:nnn个点的二叉树,每个点的权值KaTeX parse error: ...
- static关键字的功能
转载:https://blog.csdn.net/guotianqing/article/details/79828100 C语言&C++ 1.局部变量 如果在一个函数内部定义了一个静态变量, ...
- JAVA遇上HTML-----JSP 篇基本概念
Java Web简介 1.什么是WEB应用程序: Web应用程序是一种可以通过Web访问的应用程序.Web应用程序的一个最大好处是用户很容易访问应用程序.用户只需要有浏览器即可,不需要再安装其他软件. ...
- Android 从相机或相册或获取图片(转)
参考: https://github.com/ASDbobo/GetPhotoDemo Android 8.0 调取系统摄像头和相册选择图片 9.3 使用Camera拍照
- mybatis xml中的大于、小于等符号写法
xml特殊符号转义写法 < < > > <> <> & & ' ...
- 实现数组(java)
一,数组 java中有对数组的数据结构:数组就是一个存放固定数据的结构. 数组的声明举例:int [] array=new int [3],与之相同的是private in [ ] array; a ...
- new命令简化的内部流程
构造函数返回对象的一些问题: function fn(name,age){ this.name = name; this.age = age; //return 23; 忽略数字,直接返回原有对象 / ...
- 学以致用七---Centos7.2+python3.6.2+django2.1.1 --搭建一个网站(补充)
补充:上一节出现的报错提示 可在settings.py 里,改成 ‘*’ ,这样所有的主机都可以访问了. 打开网页 注意红色框出来的 hello 是和 urls.py里的hello对应 urls.p ...
- mysql主从复制Error1205
主从架构.今天发现从库SQL线程报错,主从复制停止了.查看错误发现: Last_SQL_Errno: 1205 Last_SQL_Error: Slave SQL thread ...
- wget批量下载http文件
eg:http://hgdownload.soe.ucsc.edu/goldenPath/hg19/encodeDCC/wgEncodeAwgDnaseUniform/ 下载该路径下的所有文件 wge ...