Skip to content

Commit

Permalink
(#8) Added getTextFromRegion
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Nov 23, 2020
1 parent 577235d commit 9915eac
Show file tree
Hide file tree
Showing 12 changed files with 1,467 additions and 557 deletions.
6 changes: 6 additions & 0 deletions jest.config.base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: "ts-jest",
collectCoverage: true,
coveragePathIgnorePatterns: ["(**/*.spec).(tsx?|ts?)$"],
verbose: true,
};
10 changes: 6 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const base = require("./jest.config.base");

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
projects: ['<rootDir>/packages/*'],
};
...base,
projects: ["<rootDir>/packages/*/jest.config.js"],
coverageDirectory: "<rootDir>/coverage/unit/",
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"clean": "lerna clean -y",
"postinstall": "lerna bootstrap",
"build": "lerna run build",
"test": "jest",
"lerna:publish": "lerna publish from-package --yes ",
"lerna:publish-next": "lerna publish --canary --include-merged-tags --preid next --dist-tag next --pre-dist-tag next --no-push --no-git-tag-version --no-git-reset --force-publish --yes"
},
Expand Down
Binary file modified packages/e2e/e2e-suite/case1/assets/lets-fry-text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 7 additions & 8 deletions packages/e2e/e2e-suite/case1/check.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
(async () => {
const testCase = new TestCase("Fry a typed egg");
const url = "https://sakuli.io/e2e-pages/fryed-egg";
const url = "https://sakuli.io/e2e-pages/fryed-egg/license.html";
const env = new Environment();
const screen = new Region()
try {
await _navigateTo(url);
await testCase.endOfStep("Navigate to sakuli website");

const heading = await _fetch(_heading1(/LET'S FRY AN EGG/));
const rect = await heading.getRect()
const headingRegion = new Region(rect.x, rect.y+150, rect.width, rect.height)

await headingRegion.highlight(1)

await _assertEqual(await _getTextFromRegion(headingRegion), "LET'S FRY AN EGG")
await env.setSimilarity(.97)
const region = await screen.find("assets/lets-fry-text.png");
await region.highlight(1)
await _assertEqual(await _getTextFromRegion(region), "License Information\n\f")

} catch (e) {
await testCase.handleException(e);
Expand Down
11 changes: 11 additions & 0 deletions packages/sakuli-ocr/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
name: "sakuli-ocr",
displayName: "sakuli-ocr",
rootDir: "src/",
testMatch: [
`**/!(*.+(aix|it).*|*.+(darwin|it).*|*.+(freebsd|it).*|*.+(linux|it).*|*.+(openbsd|it).*|*.+(sunos|it).*|*.+(win32|it).*)+(spec|test).[jt]s?(x)`,
`**/?(*.${process.platform}!(.it).*)+(spec|test).[jt]s?(x)`,
],
};
129 changes: 124 additions & 5 deletions packages/sakuli-ocr/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions packages/sakuli-ocr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
],
"dependencies": {
"@sakuli/core": "2.4.0",
"@sakuli/legacy": "2.4.0"
"@sakuli/legacy": "2.4.0",
"@nut-tree/nut-js": "next",
"tesseract.js": "^2.1.4"
},
"devDependencies": {
"@types/node": "12.12.34",
Expand All @@ -41,6 +43,7 @@
"typescript": "3.8.3",
"jest": "26.6.3",
"ts-jest": "26.4.4",
"fs-extra": "9.0.1"
"fs-extra": "9.0.1",
"sneer": "1.0.1"
}
}
5 changes: 2 additions & 3 deletions packages/sakuli-ocr/src/OcrContext.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { TestExecutionLifecycleHooks } from "@sakuli/core/dist/runner/context-provider.interface";
import { ThenableRegion } from "@sakuli/legacy";
import { Region } from "@sakuli/legacy";
import { getTextFromRegion } from "./getTextFromRegion";

export interface OcrContext{
_getTextFromRegion: (region: ThenableRegion) => Promise<string>
_getTextFromRegion: (region: Region) => Promise<string>
}
export class OcrContextProvider implements TestExecutionLifecycleHooks<OcrContext>{
requestContext(): Promise<OcrContext> {

return Promise.resolve({
_getTextFromRegion: getTextFromRegion
})
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 16 additions & 3 deletions packages/sakuli-ocr/src/getTextFromRegion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { ThenableRegion } from "@sakuli/legacy";
import { Region } from "@sakuli/legacy";
import { screen, Region as NutRegion } from "@nut-tree/nut-js";
import { execSync } from "child_process";
import { join } from "path";
import fs from "fs"

export async function getTextFromRegion(region: ThenableRegion): Promise<string>{
return Promise.resolve("foo")
export async function getTextFromRegion(region: Region): Promise<string>{

const regionCapture = async (selectedRegion: NutRegion, outputFilename: string) => {
const regionImage = await (<any>screen).vision.grabScreenRegion(selectedRegion);
await (<any>screen).vision.saveImage(regionImage, outputFilename);
};

await regionCapture(new NutRegion(await region.getX() || 0, await region.getY() || 0, await region.getW() || 0, await region.getH() || 0),"ocr-screenshot.png");
const text = execSync(`tesseract ${join(process.cwd(), "ocr-screenshot.png")} stdout`)
fs.unlinkSync(join(process.cwd(), "ocr-screenshot.png"))
return Promise.resolve(text.toString())
}
Loading

0 comments on commit 9915eac

Please sign in to comment.