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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix comments
  • Loading branch information
KazuCocoa committed Mar 12, 2019
commit 07c246aced424b1426a921123fd23828c7923ada
18 changes: 11 additions & 7 deletions lib/basedriver/commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const commands = {}, helpers = {}, extensions = {};
const IMAGE_STRATEGY = '-image';
const CUSTOM_STRATEGY = '-custom';

// Used to compar ratio and screen width
// Used to compare ratio and screen width
// Pixel is basically under 1080 for example. 100K is probably enough fo a while.
const FLOAT_PRECISION = 100000;

Expand Down Expand Up @@ -374,21 +374,25 @@ helpers.getScreenshotForImageFind = async function (screenWidth, screenHeight) {
const screenAR = screenWidth / screenHeight;
const shotAR = shotWidth / shotHeight;
if (Math.round(screenAR * FLOAT_PRECISION) === Math.round(shotAR * FLOAT_PRECISION)) {
log.info('Screenshot aspect ratio matched screen aspect ratio');
log.info(`Screenshot aspect ratio '${shotAR}' (${shotWidth}x${shotHeight}) matched ` +
`screen aspect ratio '${screenAR}' (${screenWidth}x${screenHeight})`);
} 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}.`);

// Select smaller ratio
// In the case where the x-scale and y-scale are different, we need to decide
// which one to respect, otherwise the screenshot and template will end up
// being resized in a way that changes its aspect ratio (distorts it). For example, let's say:
// this.getScreenshot(shotWidth, shotHeight) is 540x397,
// this.getDeviceSize(screenWidth, screenHeight) is 1080x1920.
// The ratio is {xScale: 0.5, yScale: 0.2}.
// In this case, we must choose `yScale: 0.2` as scaleFactor.
// Because if Appium selects the both ratio, the screenshot will be distorted.
// If Appium selects the xScale, height will be bigger than real screenshot size
// The ratio would then be {xScale: 0.5, yScale: 0.2}.
// In this case, we must should `yScale: 0.2` as scaleFactor, because
// if we select the xScale, the height will be bigger than real screenshot size
// which is used to image comparison by OpenCV as a base image.
// All of this is primarily useful when the screenshot is a horizontal slice taken out of the
// screen (for example not including top/bottom nav bars)
const xScale = (1.0 * shotWidth) / screenWidth;
const yScale = (1.0 * shotHeight) / screenHeight;
const scaleFactor = xScale >= yScale ? yScale : xScale;
Expand Down
6 changes: 3 additions & 3 deletions lib/basedriver/device-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const GLOBAL_DEFAULT_SETTINGS = {

// Users might have scaled template image to reduce their storage size.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would be nice to have this also described in some doc

Copy link
Member Author

Choose a reason for hiding this comment

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

// This setting allows users to scale a template image they send to Appium server
// so that Appium server compare actual scale users orginaly have.
// e.g. A user have `270 × 32 pixels` image oridinally `1080 × 126 pixels`
// The user set {defaultImageTemplateScale: 4.0} to scale the small image
// so that the Appium server compares the actual scale users originally had.
// e.g. If a user has an image of 270 x 32 pixels which was originally 1080 x 126 pixels,
// the user can set {defaultImageTemplateScale: 4.0} to scale the small image
// to the original one so that Appium can compare it as the original one.
defaultImageTemplateScale: DEFAULT_TEMPLATE_IMAGE_SCALE,

Expand Down