在测试过程中,常常需要对核心页面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比对。如果能精确匹配,则认为对比成功;如果页面发生任何细微的变化,都会认为不匹配。
具体实例如下:
package cn.om.webdriverapi;
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 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.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
public class TestCompareScreenShot {
WebDriver driver;
String url;
@Test
public void testImageComparison() throws InterruptedException, IOException {
driver.get(url);
//对baidu首页进行截屏,保存为baiduHomePage_actual.jpg
File screenshot=((Ta