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

Commit

Permalink
rename preference name, default value
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuCocoa committed Mar 1, 2019
1 parent f118c15 commit c69cc68
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
19 changes: 10 additions & 9 deletions lib/basedriver/commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ helpers.findByImage = async function (b64Template, {
const {
imageMatchThreshold: threshold,
fixImageTemplateSize,
fixTemplateImageScale
fixImageTemplateScale
} = this.settings.getSettings();

log.info(`Finding image element with match threshold ${threshold}`);
Expand All @@ -225,8 +225,8 @@ helpers.findByImage = async function (b64Template, {
try {
const {b64Screenshot, scale} = await this.getScreenshotForImageFind(screenWidth, screenHeight);

if (fixTemplateImageScale) {
b64Template = await this.fixTemplateImageScale(b64Template, scale);
if (fixImageTemplateScale) {
b64Template = await this.fixImageTemplateScale(b64Template, scale);
}

rect = (await this.compareImages(MATCH_TEMPLATE_MODE, b64Screenshot,
Expand Down Expand Up @@ -399,23 +399,24 @@ helpers.getScreenshotForImageFind = async function (screenWidth, screenHeight) {
*
* @param {string} b64Template - base64-encoded image used as a template to be
* matched in the screenshot
* @param {?ScreenshotScale} scale - scale of screen
* @param {ScreenshotScale} scale - scale of screen. Default to `{}`
*
* @returns {string} base64-encoded scaled template screenshot
*/
helpers.fixTemplateImageScale = async function (b64Template, scale) {
if (!scale || !_.isNumber(scale)) {
helpers.fixImageTemplateScale = async function (b64Template, scale = {}) {
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 * scale.xScale;
const scaledHeight = baseTempHeigh * scale.yScale;
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 ${scale.xScale} and ${scale.yScale}`);
log.info(`The ratio is ${xScale} and ${yScale}`);
imgTempObj = await imgTempObj.resize(scaledWidth, scaledHeight);
return (await imgTempObj.getBuffer(imageUtil.MIME_PNG)).toString('base64');
};
Expand Down
2 changes: 1 addition & 1 deletion lib/basedriver/device-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const GLOBAL_DEFAULT_SETTINGS = {
// e.g. iOS has `width=375, height=667` window rect, but its screenshot is
// `width=750 × height=1334` pixels. This setting help to adjust the scale
// if a user use `width=750 × height=1334` pixels's base template image.
fixTemplateImageScale: false,
fixImageTemplateScale: false,

// whether Appium should re-check that an image element can be matched
// against the current screenshot before clicking it
Expand Down
18 changes: 10 additions & 8 deletions test/basedriver/commands/find-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ describe('finding elements by image', function () {
const d = new TestDriver();
const newTemplate = 'iVBORbaz';
const {compareStub} = basicStub(d);
await d.settings.update({fixTemplateImageScale: true});
sinon.stub(d, 'fixTemplateImageScale').returns(newTemplate);
await d.settings.update({fixImageTemplateScale: true});
sinon.stub(d, 'fixImageTemplateScale').returns(newTemplate);
const imgElProto = await d.findByImage(template, {multiple: false});
const imgEl = basicImgElVerify(imgElProto, d);
imgEl.template.should.eql(newTemplate);
Expand All @@ -110,20 +110,22 @@ describe('finding elements by image', function () {
const newTemplate = 'iVBORbaz';
basicStub(d);
await d.settings.update({});
sinon.stub(d, 'fixTemplateImageScale').returns(newTemplate);
d.fixTemplateImageScale.callCount.should.eql(0);
sinon.stub(d, 'fixImageTemplateScale').returns(newTemplate);
d.fixImageTemplateScale.callCount.should.eql(0);
});
it('should not fix template size scale if no scale value', async function () {
const newTemplate = 'iVBORbaz';
await helpers.fixTemplateImageScale(newTemplate).should.eventually.eql(newTemplate);
await helpers.fixImageTemplateScale(newTemplate).should.eventually.eql(newTemplate);
});
it('should not fix template size scale if it is not number', async function () {
const newTemplate = 'iVBORbaz';
await helpers.fixTemplateImageScale(newTemplate, 'wrong-scale').should.eventually.eql(newTemplate);
await helpers.fixImageTemplateScale(newTemplate, 'wrong-scale')
.should.eventually.eql(newTemplate);
});
it('should fix template size scale', async function () {
const newTemplate = 'iVBORbaz';
await helpers.fixTemplateImageScale(newTemplate, { xScale: 1.5, yScale: 1.5 }).should.eventually.eql(newTemplate);
const actual = 'iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAWElEQVR4AU3BQRWAQAhAwa/PGBsEgrC16AFBKEIPXW7OXO+Rmey9iQjMjHFzrLUwM7qbqmLcHKpKRFBVuDvj4agq3B1VRUQYT2bS3QwRQVUZF/CaGRHB3wc1vSZbHO5+BgAAAABJRU5ErkJggg==';
await helpers.fixImageTemplateScale(TINY_PNG, { xScale: 1.5, yScale: 1.5 })
.should.eventually.eql(actual);
});

it('should throw an error if template match fails', async function () {
Expand Down

0 comments on commit c69cc68

Please sign in to comment.