diff --git a/package.json b/package.json index 872fbfb..3d949d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "macaca-macos", - "version": "0.2.30", + "version": "0.2.31", "description": "Macaca MacOS driver", "keywords": [ "macos", diff --git a/src/driver/screen.ts b/src/driver/screen.ts index f51e48e..2ca5a98 100644 --- a/src/driver/screen.ts +++ b/src/driver/screen.ts @@ -7,6 +7,17 @@ export default class ScreenDriver { hidpi = Helper.isHdpiDisplay(); + /** + * 暴露ocr方法,支持通过重写使用三方能力替代 + */ + fileOcr(imgFile: string): { + rect: { left, top, height, width }; + word: string; + }[] { + const resStr = shell.exec(`${Helper.getResourcePath()}/swift/ocr ${imgFile}`, { silent: true }).stdout; + return JSON.parse(resStr); + } + /** * 使用系统ocr能力 */ @@ -22,8 +33,7 @@ export default class ScreenDriver { } const ocrRes = []; for (let i = 0; i < count; i++) { - const resStr = shell.exec(`${Helper.getResourcePath()}/swift/ocr ${saveFile}`, { silent: true }).stdout; - ocrRes.push(...JSON.parse(resStr)); + ocrRes.push(...this.fileOcr(saveFile)); } return { imgFile: saveFile, @@ -31,6 +41,22 @@ export default class ScreenDriver { }; } + /** + * 检查文案存在 + */ + checkTextExist(opts: { + text: string; + picFile?: string; // 可直接指定图片 + rectangle?: string; // 截图目标区域 通过矩形框 x,y,width,height 默认全屏 + }): boolean { + const { text, picFile, rectangle } = opts; + const res = this.getTextsPosition({ + texts: [ text ], + picFile, rectangle, + }); + return !!res.length; + } + /** * 获取文案在截图区域的位置 * @param opts @@ -39,15 +65,16 @@ export default class ScreenDriver { texts: string[]; // 目标文案 index?: number; // 重复项指针 contains?: boolean; // 包含即可 + picFile?: string; // 可直接指定图片 rectangle?: string; // 截图目标区域 通过矩形框 x,y,width,height 默认全屏 }) { const { - texts, rectangle, + texts, rectangle, picFile, index = 0, contains = true, } = opts; // 获取文案位置 - const { ocrRes } = this.screenOcr({ rectangle }); + const { ocrRes } = this.screenOcr({ picFile, rectangle }); const resultList = []; // 找多个目标 for (const text of texts) { diff --git a/test/macaca-macos.test.ts b/test/macaca-macos.test.ts index cf8a989..3116502 100644 --- a/test/macaca-macos.test.ts +++ b/test/macaca-macos.test.ts @@ -33,6 +33,36 @@ describe('macaca-macos unit testing', function() { assert(res); }); + it.skip('checkTextExist should be ok', async () => { + this.timeout(0); + const res = driver.checkTextExist({ + text: '文案存在', + }); + assert(res); + }); + + it.skip('overwrite should be ok', async () => { + this.timeout(0); + driver.fileOcr = (imgFile) => { + console.log(imgFile); + return [ + { + word: '哈哈', + rect: { + left: 0, + top: 1, + height: 100, + width: 100, + }, + }, + ]; + }; + const res = driver.checkTextExist({ + text: '哈哈', + }); + assert(res); + }); + it.skip('mouse drag should be ok', async function() { this.timeout(0); await Helper.sleep(3E3);