51Testing软件测试论坛
标题:
Sikuli元素异常处理(截图及打印日志)
[打印本页]
作者:
测试积点老人
时间:
2019-1-9 15:33
标题:
Sikuli元素异常处理(截图及打印日志)
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);
}
}
}
复制代码
作者:
Miss_love
时间:
2021-1-5 13:44
支持分享
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2