https://appium.io/docs/en/advanced-concepts/image-elements/index.html
opencv4nodejs安装
export OPENCV4NODEJS_DISABLE_AUTOBUILD=1
npm install -g opencv4nodejs
cd $(npm root -g)/opencv4nodejs/
export OPENCV4NODEJS_AUTOBUILD_FLAGS=-DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui,video,calib3d,features2d,objdetect,dnn,ml,flann,photo,stitching,gapi
npm i --save opencv-build
node install/install.js
常见错误信息
dbug Support Initializing opencv
dbug Support Loading local package 'opencv4nodejs'
dbug Support Failed to load local package 'opencv4nodejs': Cannot find module '/usr/local/lib/node_modules/opencv4nodejs/build/Release/opencv4nodejs'
dbug Support Require stack:
dbug Support - /usr/local/lib/node_modules/opencv4nodejs/lib/cv.js
dbug Support - /usr/local/lib/node_modules/opencv4nodejs/lib/opencv4nodejs.js
dbug Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/lib/node.js
dbug Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/lib/image-util.js
dbug Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/index.js
dbug Support - /usr/local/lib/node_modules/appium/build/lib/logsink.js
dbug Support - /usr/local/lib/node_modules/appium/build/lib/main.js
dbug Support Failed to load global package 'opencv4nodejs': The "path" argument must be of type string. Received undefined
dbug Support Linking package 'opencv4nodejs'
dbug Support Retrying load of linked package 'opencv4nodejs'
ERR! Support Unable to load package 'opencv4nodejs': Cannot find module '/usr/local/lib/node_modules/opencv4nodejs/build/Release/opencv4nodejs'
ERR! Support Require stack:
ERR! Support - /usr/local/lib/node_modules/opencv4nodejs/lib/cv.js
ERR! Support - /usr/local/lib/node_modules/opencv4nodejs/lib/opencv4nodejs.js
ERR! Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/lib/node.js
ERR! Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/lib/image-util.js
ERR! Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/index.js
ERR! Support - /usr/local/lib/node_modules/appium/build/lib/logsink.js
ERR! Support - /usr/local/lib/node_modules/appium/build/lib/main.js
WARN Support Unable to load 'opencv4nodejs': Unable to load package 'opencv4nodejs': Cannot find module '/usr/local/lib/node_modules/opencv4nodejs/build/Release/opencv4nodejs'
WARN Support Require stack:
WARN Support - /usr/local/lib/node_modules/opencv4nodejs/lib/cv.js
WARN Support - /usr/local/lib/node_modules/opencv4nodejs/lib/opencv4nodejs.js
WARN Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/lib/node.js
WARN Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/lib/image-util.js
WARN Support - /usr/local/lib/node_modules/appium/node_modules/appium-support/build/index.js
WARN Support - /usr/local/lib/node_modules/appium/build/lib/logsink.js
WARN Support - /usr/local/lib/node_modules/appium/build/lib/main.js
[debug] [W3C (958f2e60)] Encountered internal error running command: Error: 'opencv4nodejs' module is required to use OpenCV features. Please install it first ('npm i -g opencv4nodejs') and restart Appium. Read https://github.com/justadudewhohacks/opencv4nodejs#how-to-install for more details on this topic.
[debug] [W3C (958f2e60)] at initOpenCV (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/image-util.js:137:11)
[debug] [W3C (958f2e60)] at Object.getImageOccurrence (/usr/local/lib/node_modules/appium/node_modules/appium-support/lib/image-util.js:498:3)
[debug] [W3C (958f2e60)] at AndroidUiautomator2Driver.compareImages (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/images.js:62:16)
[debug] [W3C (958f2e60)] at condition (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/find.js:257:22)
[debug] [W3C (958f2e60)] at wrappedCondFn (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/timeout.js:151:12)
[debug] [W3C (958f2e60)] at spin (/usr/local/lib/node_modules/appium/node_modules/asyncbox/lib/asyncbox.js:138:20)
[debug] [W3C (958f2e60)] at waitForCondition (/usr/local/lib/node_modules/appium/node_modules/asyncbox/lib/asyncbox.js:155:10)
[debug] [W3C (958f2e60)] at AndroidUiautomator2Driver.implicitWaitForCondition (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/timeout.js:153:10)
[debug] [W3C (958f2e60)] at AndroidUiautomator2Driver.findByImage (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/find.js:277:5)
[debug] [W3C (958f2e60)] at AndroidUiautomator2Driver.findElement (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/find.js:48:12)
[HTTP] <-- POST /wd/hub/session/958f2e60-9977-4632-8844-150a840d5ac5/element 500 2857 ms - 961
基于图像对比的代码示例
@Test
public void testOpenCV() throws InterruptedException, IOException {
WebElement myPointView = driver.findElement(AppiumBy.image(getImageAsBase64String("myPointView")));
myPointView.click();
}