* @param comparator an image comparator.
*/
public void verifyImage(Comparator comparator) {
ZKSelenium zkSelenium = (ZKSelenium) getCurrent();
String browserName = zkSelenium.getBrowserName();
ConfigHelper configHelper = ConfigHelper.getInstance();
String resultDirStr = configHelper.getImageDest() + File.separator + format.format(new java.util.Date());
String baseDirStr = configHelper.getImageSrc();
if (resultDirStr == null || resultDirStr.isEmpty() ||
baseDirStr == null || baseDirStr.isEmpty()) {
verifyTrue("Incorrect setting of images' outputing path. Please check config.properties.", false);
return;
}
try {
File resultDir = new File(resultDirStr);
File baseDir = new File(baseDirStr);
if (!baseDir.exists()) {
baseDir.mkdirs();
}
String title = this.getEval("document.title");
byte[] imgByteArr = Base64.decode(zkSelenium.getCmdProcessor().getString("captureEntirePageScreenshotToString", new String[] {title, browserName}));
BufferedImage testBuffImg = ImageIO.read(new ByteArrayInputStream(imgByteArr));
final String postfix = "_" + recordCount++ + ".png";
if (configHelper.isComparable()) {
File basef = new File(baseDir + File.separator + caseID, caseID + "_" + browserName + postfix);
if (!basef.isFile()) {
super.assertTrue("The image source is not found. - " + basef, false);
return;
}
BufferedImage baseBuffImg = ImageIO.read(basef);
if (baseBuffImg.getWidth() != testBuffImg.getWidth() || baseBuffImg.getHeight() != testBuffImg.getHeight()) {
File subDir = new File(resultDirStr + File.separator + caseID);
if (!subDir.isDirectory())
subDir.mkdirs();
subDir = new File(subDir, caseID + "_" + browserName + "_result" + postfix);
ImageIO.write(testBuffImg, "png", subDir);
super.verifyTrue("The size of images are not the same. Please check result. - " + subDir, false);
return;
}
Map<String, Integer[]> frameSize = configHelper.getFrameSize();
Integer[] sizes= frameSize.get(browserName);
if (sizes == null)
throw new IllegalStateException("The [" + browserName + "-frame] setting is not found in the config.properties.");
BufferedImage base = baseBuffImg.getSubimage(sizes[1],
sizes[0], baseBuffImg.getWidth()
- (sizes[1] + sizes[3]),
baseBuffImg.getHeight() - (sizes[0] + sizes[2]));
BufferedImage test = testBuffImg.getSubimage(sizes[1],
sizes[0], testBuffImg.getWidth()
- (sizes[1] + sizes[3]),
testBuffImg.getHeight() - (sizes[0] + sizes[2]));
Comparator ic = comparator == null ? new DefaultComparator(base.getWidth()/configHelper.getGranularity(),
base.getHeight()/configHelper.getGranularity(), configHelper.getLeniency()):
comparator;
BufferedImage imgc = ic.compare(base, test);
if (imgc != null && checkMatrix(ic, configHelper.getLeniency())) {
File subDir = new File(resultDirStr + File.separator + caseID);
if (!subDir.isDirectory())
subDir.mkdirs();
subDir = new File(subDir, caseID + "_" + browserName + "_result" + postfix);
ImageIO.write(imgc, "png", subDir);