Appium结合OCR功能实现强力元素定位
2018-02-28 12:18
453 查看
Appium 也不是万能的,对待复杂元素,嵌套元素等等,无计可施,定不了位置的时候,当你的自动化不能获得元素坐标的等问题,你会不会抓狂,这里有一个工具类,可以帮助你度过难关!
其原理就是利用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);
}
}
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);
}
}
相关文章推荐
- 浅谈百度地图的简单开发之结合方向传感器实现定位功能(三)
- jQuery实现ToolTip元素定位显示功能示例
- 浅谈百度地图的简单开发之结合方向传感器实现定位功能(三)
- APPIUM+JAVA实现对安卓APK的自动化测试----第十一篇【定位元素】
- 用百度AI的OCR文字识别结合PHP实现了图片的文字识别功能
- 用百度AI的OCR文字识别结合JAVA实现了图片的文字识别功能
- jQuery实现ToolTip元素定位显示功能示例
- 给图片各个位置做标记,实现类似百度地图的标记功能(动态创建页面元素,各种定位,Ajax,MVC)
- Adb-For-Test 使用 adb 命令实现元素定位等功能,用来辅助 android 测试
- RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第七篇【元素定位介绍】
- C语言双向链表实现根据使用频率安排元素位置的功能实例代码
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- Python 结合 Zabbix API实现监控主机的增删改功能
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- Android 端天气预报APP的实现(七)实现城市定位功能
- EXTJS学习系列提高篇:第二篇(转载)作者殷良胜,结合EXT2.2+C#.net实现将数据导入Excel的功能
- Flex结合java实现一个登录功能
- php结合redis实现高并发下的抢购、秒杀功能
- Android应用程序实现定位功能(使用百度定位SDK)
- 实例:SSH结合Easyui实现Datagrid的批量删除功能