什么时候我们需要用到图像识别?

在我们做 App 自动化测试的时候,会发现很多场景下元素没有 id、content-desc、text 等等属性,并且有可能也会碰到由于开发采用的是自定义 View,View 中的元素也无法识别到,很多的自动化测试框架对此类场景束手无策。Appium 在 V1.9.0 中有给我们带来了针对于图像识别的全新图片元素定位的方法:-image

728 x 485 829 x 553

安装

在 Appium 中用到的图像识别库是 OpenCV,因为在 Appium V1.9.0 中 -image 查找元素方式才被引入进来,所以需要保证你有安装 V1.9.0 或以上版本的 Appium。

这里需要注意,Appium 图像识别的使用只能适用于通过 npm 安装 Appium 的方式,通过 Appium-desktop 安装 Appium 的方式是不适用的。

Step1:通过 npm 安装 Appium

安装配置 Node.js

  1. 下载对应系统和位数的 node.js 安装包 https://nodejs.org/en/download/
  2. 选择安装路径,我这里选择安装到 D 盘
  3. 进入 cmd,输入 node -v 以及 npm -v 检测(npm 是 node.js 自带的包管理器)

  1. 指定 npm 安装的全局模块和缓存的路径,如果不指定默认会安装到 C 盘中【C:\Users\用户名 AppData\Roaming\npm】

    • 在 nodejs 目录下新建【node_cache】和【node_global】目录
    • 输入如下命令指定:

npm config set prefix "D:\software\nodejs\node_global"
npm config set cache "D:\software\nodejs\node_cache"

  1. 配置 node.js 环境变量

    • 在系统变量中新建【NODE_PATH】变量名,变量值为【D:\software\nodejs\node_global\node_modules】
    • 在系统变量【Path】中追加【%NODE_PATH%】
    • 将用户变量【Path】中原来的【C:\Users\用户名\AppData\Roaming\npm】修改为【D:\software\nodejs\node_global】

安装 Appium

1.设置 npm 淘宝镜像地址

npm config set registry https://registry.npm.taobao.org

2.npm 通过全局的方式安装 Appium(默认下载安装最新版本 Appium)

npm install -g appium 
Step2:安装 opencv4nodejs 所需要的依赖

安装 Cmake

  1. 进入到 https://cmake.org/download/下载对应软件包,解压到任意目录
  2. 找到系统变量 Path,追加【D:\software\cmake-3.18.1-win64-x64\bin】,其中 cmake-3.18.1-win64-x64 是为 Cmake 解压之后的目录名

安装 building tools

npm install --global windows-build-tools

Step3:Appium 中安装图像识别模块 opencv4nodejs

由于网络和各种环境问题的影响,通过官方推荐的【npm i -g opencv4nodejs】命令直接安装 opencv4nodejs 模块的话,会发现存在很多的问题。

按照 https://www.npmjs.com/package/opencv4nodejs#how-to-install 里面的说明,这里使用的是手动安装 OpenCV 的方式

在系统环境变量中,添加【OPENCV4NODEJS_DISABLE_AUTOBUILD】变量名,变量值为【1】

安装 choco(windows 上面的软件管理方案,可以解决部署软件复杂、容易出错问题),官网安装说明地址:https://chocolatey.org/install,打不开的同学给大家搬运过来:

  • 在系统环境变量中,添加【OPENCV4NODEJS_DISABLE_AUTOBUILD】变量名,变量值为【1】

  • 安装 choco(windows 上面的软件管理方案,可以解决部署软件复杂、容易出错问题),官网安装说明地址:https://chocolatey.org/install,打不开的同学给大家搬运过来:

  • 在系统环境变量中,添加【OPENCV4NODEJS_DISABLE_AUTOBUILD】变量名,变量值为【1】

安装 choco(windows 上面的软件管理方案,可以解决部署软件复杂、容易出错问题),官网安装说明地址:https://chocolatey.org/install,打不开的同学给大家搬运过来:

  • 使用管理员运行 powershell,检查执行策略:执行指令【Get-ExecutionPolicy】
  • 如果结果为 Restricted,那么执行指令【Set-ExecutionPolicy AllSigned】或者【Set-ExecutionPolicy Bypass -Scope Process】,直到结果为【RemoteSigned】
  • 执行下面指令安装
    iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
    
  • 通过 choco 安装 OpenCV
    执行下面的安装指令即可

    choco install OpenCV -y -version 4.1.0
    

    完毕后 choco 默认安装在 C:\tools 目录中

  • 设置 OpenCV 环境变量

    • 添加系统环境变量,变量名【OPENCV_BIN_DIR】,变量值为【C:\tools\opencv\build\x64\vc15\bin】
    • 添加系统环境变量,变量名【OPENCV_DIR】,变量值为【C:\tools\opencv\build\x64\vc15】
    • 添加系统环境变量,变量名【OPENCV_INCLUDE_DIR】,变量值为【C:\tools\opencv\build\include】
    • 添加系统环境变量,变量名【OPENCV_LIB_DIR】,变量值为【C:\tools\opencv\build\x64\vc15\lib】
    • 找到系统变量 Path,追加【%OPENCV_BIN_DIR%】
  • 安装 opencv4nodejs
    进入 cmd,执行命令

  • npm i -g opencv4nodejs
    命令执行过程中,会对之前通过 choco 方式下载好的包进入编译,编译过程中可能会出现卡住,关掉 cmd 窗口重新再执行命令即可。

使用

Step1:截取所需进行图像识别的元素

  1. 将需要进行图像识别元素所在页面截屏
  2. 通过 adb pull 命令拉取截屏图片出来
  3. 用画图工具打开--划重点,一定要保证图片是原大小的,不能放大或者缩小图片
  4. 截取其中的元素保存为图片

Step2:代码实现

java-client 库中有提供了“MobileBy.image”以及“findElementByImage”方法,需要注意的是参数类型是 String 类型(以 MobileBy.image 为例)

public static By image(String b64Template) {
    return new MobileBy.ByImage(b64Template);
}

这里我们需要先将图片转换为 Base64 编码格式

转换代码如下:

protected String getImageAsBase64(String imageName) throws Exception {
    //在项目的根目录下有matchImages目录,该目录下存放所有需要进行图像识别的元素对应的截图
    File file = new File(System.getProperty("user.dir") + "\\matchImages\\" + imageName + ".png");
    return Base64.getEncoder().encodeToString(Files.readAllBytes(file.toPath()));
}

测试代码如下:

driver.findElement(MobileBy.image(getImageAsBase64("mylemon"))).click();
Thread.sleep(1000);
driver.findElement(MobileBy.image(getImageAsBase64("logoArea"))).click();
Thread.sleep(2000);
driver.findElement(MobileBy.id("com.lemon.lemonban:id/et_mobile")).sendKeys("13323234545");
driver.findElement(MobileBy.id("com.lemon.lemonban:id/et_password")).sendKeys("123456");
driver.findElement(MobileBy.image(getImageAsBase64("loginButton"))).click();

Step3:执行

  1. 开启 Appium Server,通过 appium -a 127.0.0.1 -p 4723 命令启动
  2. 启动设备,通过 adb devices 检测设备
  3. 执行 case

效果如下:

图像识别相关设置

Appium 图像识别的准确率是跟 Appium 的截图功能以及我们所提供的元素截图相关的,有时候我们会发现更换一台不同分辨率的手机之后脚本就会执行失败。Appium 提供了一些设置可以提高图像识别的准确率或加快图像识别的速度。

使用示例:

driver.setSetting(Setting.IMAGE_MATCH_THRESHOLD, 0.5); 

以下设置项来自官方说明

Setting Name Description Possible Values Default Value
imageMatchThreshold The OpenCV match threshold below which to consider the find a failure. Basically the range of possibilities is between 0 (which means no threshold should be used) and 1 (which means that the reference image must be an exact pixel-for-pixel match). The exact values in between have no absolute meaning. For example a match that requires drastic resizing of a reference image will come out as a lower match strength than otherwise. It's recommended you try the default setting, and then incrementally lower the threshold if you're not finding matching elements. If you're matching the wrong element, try increasing the threshold value. Numbers from 0 to 1 0.4
fixImageFindScreenshotDims Appium knows the screen dimensions, and ultimately these are the dimensions which are relevant for deciding where to tap on the screen. If the screenshot retrieved (via Appium's native methods, or an external source) does not match the screen dimensions, this setting dictates that Appium will adjust the size of the screenshot to match, ensuring that matched elements are found at the correct coordinates. Turn this setting off if you know it's not necessary, and Appium will forego the check, potentially speeding things up a bit. true or false true
fixImageTemplateSize OpenCV will not allow the matching of a reference image / template if that image is larger than the base image to match against. It can happen that the reference image you send in has dimensions which are larger than the screenshot Appium retrieves. In this case the match will automatically fail. If you set this setting to true, Appium will resize the template to ensure it is at least smaller than the size of the screenshot. true or false false
fixImageTemplateScale Appium resizes a base image to fit its window size before matching them with OpenCV. If you set this setting to true, Appium scales a reference image you send in as the same scale Appium scales the base image to fit the window size. e.g. iOS screenshot is 750 × 1334 pixels base image. The window size is 375 x 667. Appium rescale the base image to window size scaling it with 0.5. A reference image is based on the screenshot size, never the image matches with the window size scale. This settings allow Appium to scale the reference image with 0.5. appium-base-driver#306
 
true or false false
defaultImageTemplateScale Appium does not resize template images by default (the value of 1.0). Although, storing scaled template images might help to save size of the storage. E.g. One has could represent 1080 × 126 pixels area by 270 × 32 pixels template image (the value of defaultImageTemplateScale is expected to be set to 4.0). Checkappium-base-driver#307 for more details. e.g.,0.510.0100 1.0
checkForImageElementStaleness It can happen that, in between the time you have matched an image element and the time you choose to tap on it, the element is no longer present. The only way for Appium to determine this is to attempt to re-match the template immediately before tapping. If that re-match fails, you will get a StaleElementException, as you would expect. Turn this to false to skip the check, potentially speeding things up, but potentially running into stale element issues without the benefit of an exception to let you know you did.
 
true or false true
autoUpdateImageElementPosition It can happen that a matched image changes position in between the time it is found and the time you tap on it. As with the previous setting, Appium can automatically adjust its position if it determines in a re-match that the position changed. true or false false
imageElementTapStrategy In order to tap on a found image element, Appium has to use one of its touch action strategies. The available strategies are the W3C Actions API, or the older MJSONWP TouchActions API. Stick to the default unless the driver you are using does not support the W3C Actions API for some reason. "w3cActions" or "touchActions" "w3cActions"
getMatchedImageResult Appium does not store the matched image result. Although, storing the result in memory might help for debugging whether which area is matched by find by image. Appium returns the image againstattribute API as visual. true or false false

本文由柠檬班歪歪老师原创,转载需注明出处!

【软件测试架构师】从小白到高手,免费赠送学习资料

Appium图像识别技术OpenCV相关推荐

  1. appium通过图像识别技术opencv解决密码键盘的模拟输入密码问题

    appium自动化测试框架,具体信息不便我重复,网上能够查找的资料也很多 我项目中在做自动化测试的过程中,遇到密码键盘,需要进行模拟点击输入的问题,使用appium提供的sent_keys方法不管用, ...

  2. Python实战案例,图像识别技术OpenCV,Python实现猫脸检测

    开发工具 Python版本:3.6.4 相关模块: cv2模块: 以及一些Python自带的模块. 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可. 原理简介 简单地讲一讲H ...

  3. 利用图像识别技术获取安卓手机游戏的坐标

    安卓利用图像识别技术定位游戏人物坐标 使用技术 opencv,安卓辅助点击,录屏技术 实现目标 通过点击就可以直接识别到王者荣耀游戏中的人物坐标,并进行点击.辅助自己在游戏时获取点击视野. APP使用 ...

  4. 免费下载 | 某平台2980元「图像识别、OpenCV、神经网络」课程,仅此1次

    当下,人工智能已然跃居为全球话题的焦点,来自开发者.业界领袖.政策制定者乃至大众的关注正与日俱增. 深度学习的出现突破了过去机器学习领域浅层学习算法的局限,颠覆了语音识别.计算机视觉.图像识别.神经网 ...

  5. 读“基于深度学习的图像识别技术研究综述”有感

    "基于深度学习的图像识别技术研究综述"总结 现在流行的图像识别技术都是基于深度学习的算法,经过前辈们的探索改进,图像识别技术经历很多阶段,现如今图像识别技术已经广泛的应用于生活的方 ...

  6. 国美零售引入AI图像识别技术 线下自动识别用户数据

    https://www.toutiao.com/a6677155404637536781/ 4月3日消息,近日国美零售与云从科技签署战略合作协议,双方将共同开启人工智能技术在零售领域的深度应用,基于A ...

  7. AI+时代,谈谈产品经理对图像识别技术的阈值控制

    产品满足用户的需求有一个阈值,产品值低于阈值用户会觉得了无生趣,即产品一般般,也即产品经理做了功能经理.产品值等于阈值产品功能基本满足了用户的需求,而只有产品经理驾驭了需求,把产品做成作品,产品值才有 ...

  8. 用计算机研究甲骨文,基于图像识别技术的甲骨文数据系统

    基于图像识别技术的甲骨文数据系统 作者:白钰卓 计算机系 指导老师:刘知远 计算机系 关键词:甲骨文 数据库 图像识别 摘要 本项目致力于使用计算机技术构建甲骨文数据库与甲骨文单字识别系统,并通过公开 ...

  9. 网络信息传播中图像识别技术的意义

    电子科技大学 格拉斯哥学院 2017级柯入语 前言: 网络在人们日常生活中的地位越来越重要,而网络信息中有很大一部分为图像信息,人们可以通过图像识别技术解决多种社会问题. 时间:2018.11.10 ...

最新文章

  1. oralce 增加表字段命令|oralce 增加表字段类型命令
  2. iOS音频播放(一):概述
  3. 将已有项目转为se项目_威海将再添国家级非遗项目
  4. CUDA C编程权威指南 第四章 全局内存
  5. JS对于JSON的增删改查操作
  6. “形象代言人”与“抽风式管理”
  7. 爬虫实现对于百度文库内容的爬取
  8. 唤客猫SCRM功能详解(二)
  9. 博主的EMNLP2021合作工作已公开:TransPrompt——基于Prompt和迁移学习的小样本文本分类,欢迎阅读与引用
  10. (数据结构)线性表(总结)——链式存储结构与顺序储存结构的优缺点
  11. COMSOL求解常微分方程
  12. UTONMOS数字藏品走红之后,为艺术打开了另一条路
  13. system32下 exe文件作用
  14. 安科瑞企业微电网能效管理平台在某食品加工厂35kV变电站应用分析
  15. 交流纯电容电路中电容的容抗、容量和频率以及电压与电流的关系
  16. Python必会的12道面试题,看看你会几题?
  17. kvm安装Windows7旗舰版
  18. 无障碍键盘输入-搜狗输入法写数学公式
  19. 视频剪辑工具,教你批量分割视频并提取原音频单独保存
  20. 变形金刚3 部分台词

热门文章

  1. R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用xscale函数指定X轴坐标轴度量调整方式、设置x轴坐标为sqrt显示平方根坐标值
  2. 知乎大神用Python爬取高颜值美女(爬虫+人脸检测+颜值检测)!
  3. 现在是时候认真看看医疗保健云安全了
  4. 【思考】我为钱工作 OR 钱为我工作?
  5. java关闭inputstream_Java:浅谈InputStream的close方法
  6. 【Python】蒙特卡罗算法求圆面积
  7. XLNet学习:究极总结
  8. 一分钟了解nohup和的功效
  9. 每月一书(202209)遥远的救世主
  10. 关于视频编码器的作用详细介绍