您的位置:首页 > 移动开发

Appium结合OCR功能实现强力元素定位

2018-02-28 12:18 453 查看
Appium 也不是万能的,对待复杂元素,嵌套元素等等,无计可施,定不了位置的时候,当你的自动化不能获得元素坐标的等问题,你会不会抓狂,这里有一个工具类,可以帮助你度过难关!

sikuli 它就可以帮你轻松度过难关!

下面我们用Appium集合sikuli来实现工具类的封装 !

其原理就是利用SIKULI 的api图形处理能力,获得xy坐标,然后返回给appium进行处理!
这里我用了Spring的注解,是为了防止空指针,和不需要new对象!来减少不必要的代码!看不懂的同学,可以随时在评论区留下的问题描述~

@Component
public class Ocr {

public final double DEFAULT_MIN_SIMILARITY = 0.8;

@Autowired

public AppiumDriver driver;
@Autowired
Action action;

public Ocr(AppiumDriver driver) {
this.driver = driver;
Settings.MinSimilarity = DEFAULT_MIN_SIMILARITY;
System.out.println(Settings.MinSimilarity);
}
/**
* 用于使用图像在屏幕上点击元素的主要方法。
*/
public void clickByImage(String targetImgPath) {
Point2D coords = getCoords(takeScreenshot(), targetImgPath);
if ((coords.getX() >= 0) && (coords.getY() >= 0)) {
action.tap(1, (int) coords.getX(), (int) coords.getY());
} else {
throw new ElementNotVisibleException("Element not found - " + targetImgPath);
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  appium ocr sikuli
相关文章推荐