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 ...
随机推荐
- WebSocket 长连接 及超时问题解决
<?phpset_time_limit(0); class SocketService { private $address = 'localhost'; private $port = 80; ...
- android检测手机是否安装某个app
public static boolean isAvilible(Context context, String packageName){ //获取packagemanager final Pack ...
- PLSQL Developer对oracle中的数据进行备份恢复
1.备份数据结构 --进入 工具-->导出用户对象 如图所示 把包括所有者的√去掉,到时候我们就可以随便建一个用户导入数据了,不一定非要scott用户 2.备份数据 工具-->导出 ...
- linux学习--查看cpu及内存信息
查看物理cpu个数: cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l 查看每个cpu核数 cat /proc/cp ...
- C语言三种方法调用数组
#include <stdio.h> /********************************* * 方法1: 第一维的长度可以不指定 * * 但必须指定第二维的长度 * *** ...
- 第15章:MongoDB-聚合操作--聚合管道--$match
①$match 用于对文档集合进行筛选,里面可以使用所有常规的查询操作符. 通常会放置在管道最前面的位置,理由如下: 1:快速将不需要的文档过滤,减少后续操作的数据量 2:在投影和分组之前做筛选,查询 ...
- flex 布局 计算器
flex布局计算器 <!doctype html> <html> <head> <style> .box{ display: flex; flex-di ...
- js点击空白处触发事件
我们经常会出现点击空白处关闭弹出框或触发事件 <div class="aa" style="width: 200px;height: 200px;backgroun ...
- c 语言申明头文件和实现分开简单例子
很多时候,看到很多c函数的声明和实现是分开的.声明放在头文件,实现却放在另一个文件,最后函数被其他文件调用. 下面以简单例子说明. 一.声明部分 /* test.h */ #include <s ...
- Installing Apache Hadoop Single Node
转载请注明出处:http://www.cnblogs.com/wubdut/p/4681286.html platform: Ubuntu 14.04 LTS hadoop 1.2.1 1. inst ...