Skip to content

Commit

Permalink
feat: add checkTextExist
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuineng authored and yihuineng committed Apr 13, 2023
1 parent 570a302 commit eecf2c0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "macaca-macos",
"version": "0.2.30",
"version": "0.2.31",
"description": "Macaca MacOS driver",
"keywords": [
"macos",
Expand Down
35 changes: 31 additions & 4 deletions src/driver/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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能力
*/
Expand All @@ -22,15 +33,30 @@ 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,
ocrRes,
};
}

/**
* 检查文案存在
*/
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
Expand All @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions test/macaca-macos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit eecf2c0

Please sign in to comment.