Sikuli 框架在出现异常时,没有做出一些好的处理,比如截图,打印日志,还有对程序的回收,我们可以通过 java 中的方法对 Sikuli 进行封装一下,实现这些功能,好了,看如下代码
package com.sikuli;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.sikuli.script.Screen;
public class SikuliElWait {
/****************************************************************
* 对屏幕进行拍照
*
****************************************************************/
public static void snapShot(String path) throws Exception {
String imaFormat ="png";
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
String fName = path +"eImage/";
BufferedImage screenshot = (new Robot()).createScreenCapture(new
Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
File folder =new File(fName);
if (!folder .exists() && !folder .isDirectory()) {
folder.mkdir();
}
String name=fName+"err."+imaFormat;
File f = new File(name);
//将screenshot对象写入图像文件
ImageIO.write(screenshot, imaFormat, f);
}
/****************************************************************
* 等待元素出现,如果元素有异常,进行当前页面截图并关闭应用程序
*
****************************************************************/
public static void waitEl(Screen scr,String className,String path,String ima,double time,Process app) throws Exception{
Logger log = LogManager.getLogger(className);
DOMConfigurator.configure("log4j.xml");
try {
scr.wait(path+ima, time);
log.info("元素存在");
} catch (Exception e) {
SikuliElWait.snapShot(path);
log.info("元素不存在");
app.destroy();
throw(e);
}
}
}