This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 130
Fix find element wrong coordinate #306
Merged
KazuCocoa
merged 10 commits into
appium:master
from
KazuCocoa:fix-find-element-wrong-coordinate
Mar 1, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
40460c2
fix wrong coordinate for android and ios
KazuCocoa b6b4c14
separate fixScaleTemplateImage
KazuCocoa abbefd8
add fixScaleTemplateImage as a settings
KazuCocoa a8de7ea
take care of both landscape and portrate
KazuCocoa 3e45369
rename to fixTemplateImageScale
KazuCocoa 0f4980c
rename ratio to scale
KazuCocoa f118c15
does not scale if the arg is non number
KazuCocoa c69cc68
rename preference name, default value
KazuCocoa 4fb91b9
remove comment outted lines
KazuCocoa 0a36930
add null filter as safer way
KazuCocoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,7 +201,8 @@ helpers.findByImage = async function (b64Template, { | |
}) { | ||
const { | ||
imageMatchThreshold: threshold, | ||
fixImageTemplateSize | ||
fixImageTemplateSize, | ||
fixImageTemplateScale | ||
} = this.settings.getSettings(); | ||
|
||
log.info(`Finding image element with match threshold ${threshold}`); | ||
|
@@ -222,7 +223,12 @@ helpers.findByImage = async function (b64Template, { | |
let rect = null; | ||
const condition = async () => { | ||
try { | ||
let b64Screenshot = await this.getScreenshotForImageFind(screenWidth, screenHeight); | ||
const {b64Screenshot, scale} = await this.getScreenshotForImageFind(screenWidth, screenHeight); | ||
|
||
if (fixImageTemplateScale) { | ||
b64Template = await this.fixImageTemplateScale(b64Template, scale); | ||
} | ||
|
||
rect = (await this.compareImages(MATCH_TEMPLATE_MODE, b64Screenshot, | ||
b64Template, {threshold})).rect; | ||
return true; | ||
|
@@ -288,24 +294,36 @@ helpers.ensureTemplateSize = async function (b64Template, screenWidth, screenHei | |
let imgObj = await imageUtil.getJimpImage(b64Template); | ||
let {width: tplWidth, height: tplHeight} = imgObj.bitmap; | ||
|
||
log.info(`Template image is ${tplWidth}x${tplHeight}. Screen size is ${screenWidth}x${screenHeight}`); | ||
// if the template fits inside the screen dimensions, we're good | ||
if (tplWidth <= screenWidth && tplHeight <= screenHeight) { | ||
return b64Template; | ||
} | ||
|
||
log.info(`Scaling template image from ${tplWidth}x${tplHeight} to match ` + | ||
`screen at ${screenWidth}x${screenHeight}`); | ||
// otherwise, scale it to fit inside the screen dimensions | ||
imgObj = imgObj.scaleToFit(screenWidth, screenHeight); | ||
return (await imgObj.getBuffer(imageUtil.MIME_PNG)).toString('base64'); | ||
}; | ||
|
||
/** | ||
* @typedef {Object} Screenshot | ||
* @property {string} b64Screenshot - base64 based screenshot string | ||
*/ | ||
/** | ||
* @typedef {Object} ScreenshotScale | ||
* @property {float} xScale - Scale ratio for width | ||
* @property {float} yScale - Scale ratio for height | ||
*/ | ||
/** | ||
* Get the screenshot image that will be used for find by element, potentially | ||
* altering it in various ways based on user-requested settings | ||
* | ||
* @param {int} screenWidth - width of screen | ||
* @param {int} screenHeight - height of screen | ||
* | ||
* @returns {string} base64-encoded screenshot | ||
* @returns {Screenshot, ?ScreenshotScale} base64-encoded screenshot and ScreenshotScale | ||
*/ | ||
helpers.getScreenshotForImageFind = async function (screenWidth, screenHeight) { | ||
if (!this.getScreenshot) { | ||
|
@@ -318,7 +336,7 @@ helpers.getScreenshotForImageFind = async function (screenWidth, screenHeight) { | |
// between the screenshot and the screen, just return the screenshot now | ||
if (!this.settings.getSettings().fixImageFindScreenshotDims) { | ||
log.info(`Not verifying screenshot dimensions match screen`); | ||
return b64Screenshot; | ||
return {b64Screenshot}; | ||
} | ||
|
||
// otherwise, do some verification on the screenshot to make sure it matches | ||
|
@@ -332,43 +350,63 @@ helpers.getScreenshotForImageFind = async function (screenWidth, screenHeight) { | |
// the height and width of the screenshot and the device screen match, which | ||
// means we should be safe when doing template matches | ||
log.info('Screenshot size matched screen size'); | ||
return b64Screenshot; | ||
return {b64Screenshot}; | ||
} | ||
|
||
// otherwise, if they don't match, it could spell problems for the accuracy | ||
// of coordinates returned by the image match algorithm, since we match based | ||
// on the screenshot coordinates not the device coordinates themselves. There | ||
// are two potential types of mismatch: aspect ratio mismatch and scale | ||
// mismatch. we need to detect and fix both | ||
|
||
const screenAR = screenWidth / screenHeight; | ||
const shotAR = shotWidth / shotHeight; | ||
|
||
if (screenAR === shotAR) { | ||
log.info('Screenshot aspect ratio matched screen aspect ratio'); | ||
} else { | ||
log.warn(`When trying to find an element, determined that the screen ` + | ||
`aspect ratio and screenshot aspect ratio are different. Screen ` + | ||
`is ${screenWidth}x${screenHeight} whereas screenshot is ` + | ||
`${shotWidth}x${shotHeight}.`); | ||
shotWidth = shotWidth / (shotAR / screenAR); | ||
log.warn(`Resizing screenshot to ${shotWidth}x${shotHeight} to match ` + | ||
`screen aspect ratio so that image element coordinates have a ` + | ||
`greater chance of being correct.`); | ||
imgObj = imgObj.resize(shotWidth, shotHeight); | ||
} | ||
|
||
// now we know the aspect ratios match, but there might still be a scale | ||
// mismatch, so just resize based on the screen dimensions | ||
if (screenWidth !== shotWidth) { | ||
// mismatch. | ||
|
||
// Resize based on the screen dimensions only if both width and height are mismatched | ||
// since except for that, it might be a situation which is different window rect and | ||
// screenshot size like `@driver.window_rect #=>x=0, y=0, width=1080, height=1794` and | ||
// `"deviceScreenSize"=>"1080x1920"` | ||
let scale; | ||
if (screenWidth !== shotWidth && screenHeight !== shotHeight) { | ||
log.info(`Scaling screenshot from ${shotWidth}x${shotHeight} to match ` + | ||
`screen at ${screenWidth}x${screenHeight}`); | ||
imgObj = imgObj.resize(screenWidth, screenHeight); | ||
|
||
scale = {xScale: (1.0 * screenWidth) / shotWidth, yScale: (1.0 * screenHeight) / shotHeight}; | ||
} | ||
|
||
return (await imgObj.getBuffer(imageUtil.MIME_PNG)).toString('base64'); | ||
b64Screenshot = (await imgObj.getBuffer(imageUtil.MIME_PNG)).toString('base64'); | ||
return {b64Screenshot, scale}; | ||
}; | ||
|
||
/** | ||
* Get a image that will be used for template maching. | ||
* Returns scaled image if scale ratio is provided. | ||
* | ||
* @param {string} b64Template - base64-encoded image used as a template to be | ||
* matched in the screenshot | ||
* @param {ScreenshotScale} scale - scale of screen. Default to `{}` | ||
* | ||
* @returns {string} base64-encoded scaled template screenshot | ||
*/ | ||
helpers.fixImageTemplateScale = async function (b64Template, scale = {}) { | ||
if (!scale) { | ||
return b64Template; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean |
||
|
||
const {xScale, yScale} = scale; | ||
if (!xScale || !yScale) { | ||
return b64Template; | ||
} | ||
|
||
let imgTempObj = await imageUtil.getJimpImage(b64Template); | ||
let {width: baseTempWidth, height: baseTempHeigh} = imgTempObj.bitmap; | ||
|
||
const scaledWidth = baseTempWidth * xScale; | ||
const scaledHeight = baseTempHeigh * yScale; | ||
log.info(`Scaling template image from ${baseTempWidth}x${baseTempHeigh}` + | ||
` to ${scaledWidth}x${scaledHeight}`); | ||
log.info(`The ratio is ${xScale} and ${yScale}`); | ||
imgTempObj = await imgTempObj.resize(scaledWidth, scaledHeight); | ||
return (await imgTempObj.getBuffer(imageUtil.MIME_PNG)).toString('base64'); | ||
}; | ||
|
||
Object.assign(extensions, commands, helpers); | ||
export { commands, helpers, IMAGE_STRATEGY, CUSTOM_STRATEGY }; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will consider different ratio case more (and create a PR)