Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Fix template scale in find element by image #307

Merged
merged 17 commits into from
Mar 20, 2019
Merged
Prev Previous commit
Next Next commit
use math.round to compare two float
  • Loading branch information
KazuCocoa committed Mar 2, 2019
commit ce71874b23e5abdb9b028799ee905d585e90d4da
11 changes: 8 additions & 3 deletions lib/basedriver/commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const commands = {}, helpers = {}, extensions = {};
const IMAGE_STRATEGY = '-image';
const CUSTOM_STRATEGY = '-custom';

// Used to compar ratio and screen width
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare

// Pixel is basically under 1080 for example. 100K is probably enough fo a while.
const DEFAULT_ROUND_FLOAT = 100000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather rename it to FLOAT_PRECISION


// Override the following function for your own driver, and the rest is taken
// care of!

Expand Down Expand Up @@ -369,7 +373,7 @@ helpers.getScreenshotForImageFind = async function (screenWidth, screenHeight) {

const screenAR = screenWidth / screenHeight;
const shotAR = shotWidth / shotHeight;
if (screenAR === shotAR) {
if (Math.round(screenAR * DEFAULT_ROUND_FLOAT) === Math.round(shotAR * DEFAULT_ROUND_FLOAT)) {
log.info('Screenshot aspect ratio matched screen aspect ratio');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to do something like 'Screenshot aspect ratio (${shotAR}) matched screen aspect ratio (${screenAR})'

} else {
log.warn(`When trying to find an element, determined that the screen ` +
Expand Down Expand Up @@ -473,8 +477,9 @@ helpers.fixImageTemplateScale = async function (b64Template, opts = {}) {
return b64Template;
}

// Return if the scale is default (1.0) value
if ((xScale - DEFAULT_FIX_IMAGE_TEMPLATE_SCALE) <= 0 && (yScale - DEFAULT_FIX_IMAGE_TEMPLATE_SCALE) === 0) {
// Return if the scale is default, 1, value
if (Math.round(xScale * DEFAULT_ROUND_FLOAT) === Math.round(DEFAULT_FIX_IMAGE_TEMPLATE_SCALE * DEFAULT_ROUND_FLOAT)
&& Math.round(yScale * DEFAULT_ROUND_FLOAT === Math.round(DEFAULT_FIX_IMAGE_TEMPLATE_SCALE * DEFAULT_ROUND_FLOAT))) {
return b64Template;
}

Expand Down