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();
} }

最新文章

  1. BootLoader(2440)核心初始化代码
  2. 在linux 服务器上用.htaccess文件实现二级域名访问子目录
  3. 【原创】.NET之我见
  4. [HDOJ5445]Food Problem(优先队列优化多重背包)
  5. nginx yii2环境配置
  6. jquery1.9+获取append后的动态元素
  7. How to wipe silicon to CPU 如何给CPU正确涂抹硅脂
  8. Silverlight之OOB模式下的一些事
  9. 容斥原理——uva 10325 The Lottery
  10. ABI Management
  11. eclipse alt + &#39;/&#39; not working.
  12. iOS 搜索框控件 最简单的dome
  13. CodeForces 702D Road to Post Office
  14. popupwindow那些坑
  15. getElementById 用法的一个技巧
  16. 阿里云MySQL远程连接不上问题
  17. [Swift]LeetCode740. 删除与获得点数 | Delete and Earn
  18. memcached、cookie、session
  19. wireshark相关知识
  20. C#实现CRC校验

热门文章

  1. DOM3级的变化
  2. matchesSelector()方法
  3. 实现两个sym转一个sym
  4. referraluserid推广ID号跟踪JS处理A标签
  5. 屏幕抓取程序 (位图DDB的例子)
  6. UIActivityIndicatorView 的使用
  7. (转)ASP.NET MVC 3和Razor中的@helper 语法
  8. MV人物抹去
  9. 有趣的CSS3背景 斜条纹
  10. linux 环境安装及学习