Sikulix工具类

简单写的一个Sikulix工具类,记录一下,使用起来还是比较方便,需要java8

public class SikuliUtil {

	private static Logger logger = LogFactory.getLogger(SikuliUtil.class);
	
	public static void type(Screen screen, String value) throws Exception{
		screen.type(value);
	}
	
	public static void type(Screen screen, String value, boolean isTab) throws Exception{
		screen.type(value);
		if(isTab)
			screen.type(Key.TAB);
	}
	
	public static void typeCN(Screen screen, String value) throws Exception{
		screen.paste(value);
	}
	
	public static void typeCN(Screen screen, String value, boolean isTab) throws Exception{
		screen.paste(value);
		if(isTab)
			screen.type(Key.TAB);
	}
	
	public static Result inputType(Screen screen, String resource, String value) throws Exception{
		return doInputType(screen, resource, value, 60, false, false);
	}

	public static Result inputType(Screen screen, String resource, String value, int offset) throws Exception{
		return doInputType(screen, resource, value, offset, false, false);
	}
	
	public static Result inputType(Screen screen, String resource, String value, boolean isTab) throws Exception{
		return doInputType(screen, resource, value, 60, isTab, false);
	}

	public static Result inputType(Screen screen, String resource, String value, int offset, boolean isTab) throws Exception{
		return doInputType(screen, resource, value, offset, isTab, false);
	}
	
	public static Result inputTypeCN(Screen screen, String resource, String value) throws Exception{
		return doInputType(screen, resource, value, 60, false, true);
	}

	public static Result inputTypeCN(Screen screen, String resource, String value, int offset) throws Exception{
		return doInputType(screen, resource, value, offset, false, true);
	}
	
	public static Result inputTypeCN(Screen screen, String resource, String value, boolean isTab) throws Exception{
		return doInputType(screen, resource, value, 60, isTab, true);
	}

	public static Result inputTypeCN(Screen screen, String resource, String value, int offset, boolean isTab) throws Exception{
		return doInputType(screen, resource, value, offset, isTab, true);
	}
	
	private static Result doInputType(Screen screen, String resource, String value, int offset, boolean isTab, boolean isCN) throws Exception {
		return Optional.ofNullable(Optional.ofNullable(screen).orElse(new Screen())
						   .exists(ImgUtil.getUrl(resource)))
					      .map(match -> {
					    	  try {
					    		  screen.click(match.right(offset));
						    	  screen.type(Key.END, Key.SHIFT);
						  		  screen.type("a", Key.CTRL);
						  		  screen.type(Key.BACKSPACE);
						  		  if(isCN)
									  screen.paste(match.right(offset), value);
						  		  else
						  			  screen.type(value);
						  		  if(isTab)
						  			  screen.type(Key.TAB);
								} catch (Exception e) {
									throw new RuntimeException();
								}
					    	  return ResultUtil.returnSuccessResult("输入成功!", null, m->{
					    		  logger.log(Level.INFO, m);
					    	  });
					      })
					      .orElseGet(() -> {
					    	  return ResultUtil.returnFailureResult("输入失败!", true, false, m->{
					    		  logger.log(Level.INFO, m);
					    	  });
					      });
	}
	
	public static Result click(Screen screen, String resource, int timeout) throws Exception {
		return doClick(screen, resource, timeout, null);
	}
	
	public static Result click(Screen screen, String resource, int timeout, Location offset) throws Exception {
		return doClick(screen, resource, timeout, offset);
	}
	
	public static Result click(Screen screen, Location location) throws Exception {
		String message = location+" click";
		return Optional.ofNullable(location)
				.map(r->{
					r.click();
					return ResultUtil.returnSuccessResult(message+"成功", null, m -> {
						logger.info(m);
					});
				})
				.orElseGet(()->{
					return ResultUtil.returnFailureResult(message+"失败", true, false, m -> {
						logger.info(m);
					});
				});
		
	}
	
	private static Result doClick(Screen screen, String resource, int timeout, Location offset) throws Exception {
		String message = resource+" click";
		return Optional.ofNullable(screen.exists(ImgUtil.getUrl(resource), timeout))
				   .map(r->{
					   
					   Optional.ofNullable(offset)
					   			  .ifPresent(o->{
					   				  r.setTargetOffset(o);
					   			  });
					   
					   r.click();
					   return ResultUtil.returnSuccessResult(message+"成功", null, m -> {
						   logger.info(m);
					   });
					   
				   })
				   .orElseGet(()->{
					   return ResultUtil.returnFailureResult(message+"失败", true, false, m -> {
						   logger.info(m);
					   });
				   });
				  
	}
	
	public static Result doubleClick(Screen screen, String resource,  int timeout) throws Exception {
		return doDoubleClick(screen, resource, timeout, null);
	}
	
	public static Result doubleClick(Screen screen, String resource,  int timeout, Location offset) throws Exception {
		return doDoubleClick(screen, resource, timeout, offset);
	}
	
	public static Result doubleClick(Screen screen, Location location) throws Exception {
		String message = location+" doubleClick";
		return Optional.ofNullable(location)
				.map(r->{
					r.doubleClick();
					return ResultUtil.returnSuccessResult(message+"成功", null, m -> {
						logger.info(m);
					});
				})
				.orElseGet(()->{
					return ResultUtil.returnFailureResult(message+"失败", true, false, m -> {
						logger.info(m);
					});
				});
	}
	
	private static Result doDoubleClick(Screen screen, String resource,  int timeout, Location offset) throws Exception {
		String message = resource+" doubleClick";
		return Optional.ofNullable(Optional.ofNullable(screen).orElse(new Screen())
						   .exists(ImgUtil.getUrl(resource), timeout))
				   .map(r->{
					   
					   Optional.ofNullable(offset)
			   			  .ifPresent(o->{
			   				  r.setTargetOffset(o);
			   			  });
					   
					   r.doubleClick();
					   return ResultUtil.returnSuccessResult(message+"成功", null, m -> {
						   logger.info(m);
					   });
					   
				   })
				   .orElseGet(()->{
					   return ResultUtil.returnFailureResult(message+"失败", true, false, m -> {
						   logger.info(m);
					   });
				   });
	}
	
	public static Result selectRadio(Screen screen, String noSelectResource, String selectResource, float similar) throws Exception {
		return doSelectRadio(screen, noSelectResource, selectResource, null, similar, 1);
	}
	
	public static Result selectRadio(Screen screen, String noSelectResource, String selectResource, float similar, int overtime) throws Exception {
		return doSelectRadio(screen, noSelectResource, selectResource, null, similar, overtime);
	}
	
	public static Result selectRadio(Screen screen, String noSelectResource, String selectResource, Location offset, float similar) throws Exception {
		return doSelectRadio(screen, noSelectResource, selectResource, offset, similar, 1);
	}
	
	public static Result selectRadio(Screen screen, String noSelectResource, String selectResource, Location offset, float similar, int overtime) throws Exception {
		return doSelectRadio(screen, noSelectResource, selectResource, offset, similar, overtime);
	}
	
	private static Result doSelectRadio(Screen screen, String noSelectResource, String selectResource, Location offset, float similar, int overtime) throws Exception {
		return Optional.ofNullable(Optional.ofNullable(screen).orElse(new Screen())
						   .exists(new Pattern(ImgUtil.getUrl(selectResource)).similar(similar),overtime))
				   .map(m->{
					   return ResultUtil.returnFailureResult("单选框已经选中", true, false,  msg -> {
						   logger.info(msg);
					   });
				   })
				   .orElseGet(()->{
					   return Optional.ofNullable(screen.exists(new Pattern(ImgUtil.getUrl(noSelectResource)).similar(similar),overtime))
							   	  .map(r->{
							   		  Optional.ofNullable(offset)
							   		  			  .ifPresent(local->{
							   		  				  r.setTargetOffset(local);
							   		  			  });
							   		  r.click();
							   		  return ResultUtil.returnSuccessResult("选中单选框", r, msg -> {
										   logger.info(msg);
									   });
							   	  })
							   	  .orElseGet(()->{
							   		  return ResultUtil.returnFailureResult("选中框没找到", true, false,  msg -> {
										   logger.info(msg);
									   });
							   	  });
				   });
	}
	
	public static Result selectItem(Screen screen, String resource, String itemResource) throws Exception {
		return doSelectItem(screen, resource, itemResource,  60);
	}
	
	public static Result selectItem(Screen screen, String resource, String itemResource,  int offset) throws Exception {
		return doSelectItem(screen, resource, itemResource,  offset);
	}
	
	private static Result doSelectItem(Screen screen, String resource, String itemResource,  int offset) throws Exception {
		return Optional.ofNullable(Optional.ofNullable(screen).orElse(new Screen())
				 		   .exists(ImgUtil.getUrl(resource)))
					.map(match -> {
						match.setTargetOffset(offset, 0);
						match.click();
						try {
							TimeUnit.SECONDS.sleep(1);
						} catch (InterruptedException e) {
							throw new RuntimeException();
						}
						
						// 选择需要选择的item
						Result itemResult = Optional.ofNullable(screen.exists(ImgUtil.getUrl(itemResource)))
						   .map(item -> {
							   item.click();
							   return ResultUtil.returnSuccessResult("点击所选的item", null, m -> {
									logger.info(m);
								});
						   })
						   .orElseGet(()->{
							   return ResultUtil.returnFailureResult("点击所选的item失败", true, false, m -> {
									logger.info(m);
								});
						   });
						
						  if(!itemResult.isSuccess()) 
							  return itemResult;
						  
						  return ResultUtil.returnSuccessResult("选择框选择成功", null, m -> {
								logger.info(m);
							});
					})
					.orElseGet(()->{
						return ResultUtil.returnFailureResult("选择框选择失败", true, false, m -> {
							logger.info(m);
						});
					});
	}
	
	public static Result wait(Screen screen, String resource,  int overtime) throws Exception {
		return doWait(screen, resource, overtime);
	}
	
	public static Result wait(Screen screen, String resource) throws Exception {
		return doWait(screen, resource, 3);
	}
	
	private static Result doWait(Screen screen, String resource, int overtime) throws Exception {
		
		return Optional.ofNullable(Optional.ofNullable(screen).orElse(new Screen())
						   .exists(ImgUtil.getUrl(resource),overtime))
				   .map(m->{
					   return ResultUtil.returnSuccessResult("找到指定控件!", m, msg -> {
						   logger.info(msg);
					   });
				   })
				   .orElseGet(()->{
					   return ResultUtil.returnFailureResult("没有找到指定控件!", true, false, msg -> {
						   logger.info(msg);
					   });
				   });
		
	}
	
}

public class Result<T> implements Serializable {

	private static final long serialVersionUID = -142064247790373577L;

	private T data;
	
	private boolean success;
	
	private String message;
	
	private boolean manu;
	
	private boolean retry = true;
	
	private boolean shortage;
	
	private boolean bossShortage;
	
	private boolean overtime;
	
	private String ip;
	
	private String hostName;
	
	public Result() {
		try {
			this.hostName = HostUtil.getHostName();
			this.ip = HostUtil.getIP();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Result(boolean success, String message) {
		this.success = success;
		this.message = message;
		try {
			this.hostName = HostUtil.getHostName();
			this.ip = HostUtil.getIP();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Result(String ip, String hostName) {
		this.ip = ip;
		this.hostName = hostName;
	}
	
	public Result(boolean success, String message, String ip, String hostName) {
		this.success = success;
		this.message = message;
		this.ip = ip;
		this.hostName = hostName;
	}
	
	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isManu() {
		return manu;
	}

	public void setManu(boolean manu) {
		this.manu = manu;
	}

	public boolean isRetry() {
		return retry;
	}

	public void setRetry(boolean retry) {
		this.retry = retry;
	}

	public boolean isShortage() {
		return shortage;
	}

	public void setShortage(boolean shortage) {
		this.shortage = shortage;
	}
	
	public boolean isBossShortage() {
		return bossShortage;
	}

	public void setBossShortage(boolean bossShortage) {
		this.bossShortage = bossShortage;
	}

	public boolean isOvertime() {
		return overtime;
	}

	public void setOvertime(boolean overtime) {
		this.overtime = overtime;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getHostName() {
		return hostName;
	}

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}
	
}

public class ResultUtil {

	public static Result returnSuccessResult(String message, Object data) {
		Result result = new Result();
		result.setMessage(message);
		result.setData(data);
		return result;
	}
	
	public static Result returnSuccessResult(String message, Object data, Consumer<String> consumer) {
		consumer.accept(message);
		Result result = new Result();
		result.setSuccess(true);
		result.setMessage(message);
		result.setData(data);
		return result;
	}
	
	public static Result returnFailureResult(String message, boolean isRetry, boolean isManu) {
		Result result = new Result();
		result.setMessage(message);
		result.setRetry(isRetry);
		result.setManu(isManu);
		return result;
	}
	
	public static Result returnFailureResult(String message, boolean isRetry, boolean isManu, Consumer<String> consumer) {
		consumer.accept(message);
		Result result = new Result();
		result.setMessage(message);
		result.setRetry(isRetry);
		result.setManu(isManu);
		return result;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值