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 ...
随机推荐
- 2018.12.05 codeforces 948C. Producing Snow(堆)
传送门 维护一个堆. 每次先算出一个都不弹掉的总贡献. 然后把要弹掉的弹掉,并减去它们对应的贡献. 代码: #include<bits/stdc++.h> #define ri regis ...
- SPRING 事务管理说明
spring 事务管理是通过AOP拦截指定的方法,进行事务管理. 事务配置 <aop:config proxy-target-class="true"> <aop ...
- 介绍用C#和VS2015开发基于Unity架构的2D、3D游戏的技术
[Unity]13.3 Realtime GI示例 摘要: 分类:Unity.C#.VS2015 创建日期:2016-04-19 一.简介 使用简单示例而不是使用实际示例的好处是能让你快速理解光照贴图 ...
- java常用设计模式九:桥接模式
一.概述 将抽象部分与它的实现部分分离,使它们都可以独立地变化.它是一种对象结构型模式.比如存在2个维度,第一个维度有一个抽象类A和对应的子类A1和A2:第二个维度有另一个接口B和对应的子类B1和B2 ...
- 行转列group_concat()函数妙用
测试环境准备: mysql> create table test01(name varchar(20),address varchar(20)); Query OK, 0 rows affect ...
- 2.2.9静态同步synchronized方法与synchronized(class)代码块
关键字synchronized还可以应用在static静态方法上,这样写那是对当前的*.java文件对应的class类进行持锁, 测试如下 package com.cky.bean; /** * Cr ...
- Matlab作图
修改线宽 plot(x,y,'LineWidth',1.5) 在xlabel,ylabel,title中使用latex语言 xlabel('$x$','interpreter','latex') yl ...
- day21(Listener监听器)
监听器只要分为监听web对象创建与销毁,监听属性变化,感知监听器. 1.监听web对象的创建与销毁 servletContextListener 监听ServletContext对象的创建和销毁 ...
- 你可能不知道的Shell
Shell也叫做命令行界面,它是*nix操作系统下用户和计算机的交互界面.Shell这个词是指操作系统中提供访问内核服务的程序. 这篇文章向大家介绍Shell一些非广为人知.但却实用有趣的知识,权当品 ...
- Codeforces822 B. Crossword solving
B. Crossword solving time limit per test 1 second memory limit per test 256 megabytes input standard ...