Skip to content

Commit d1ea780

Browse files
committed
realsolution
1 parent 9e0f788 commit d1ea780

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

.changeset/spotty-bananas-glow.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@wdio/ocr-service": minor
3+
---
4+
5+
\- Inside browser command, the OCR functions are called directly with secured context instead of calling the browser function itself (to avoir loop from issue 657)
6+
7+
\- Inside browser command, the OCR functions are first add to the Multi Remote browser with a loop and another version in Single Remote still is added to each instances (similar than the issue 983)

packages/ocr-service/src/service.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,29 @@ import ocrClickOnText from './commands/ocrClickOnText.js'
1111
import ocrSetValue from './commands/ocrSetValue.js'
1212

1313
const log = logger('@wdio/ocr-service')
14-
const ocrCommands = {
15-
ocrGetText,
16-
ocrGetElementPositionByText,
17-
ocrWaitForTextDisplayed,
18-
ocrClickOnText,
19-
ocrSetValue,
14+
const ocrCommands: {
15+
[key: string]: (options: any) => Promise<any>
16+
} = {
17+
'ocrGetText': ocrGetText,
18+
'ocrGetElementPositionByText': ocrGetElementPositionByText,
19+
'ocrWaitForTextDisplayed': ocrWaitForTextDisplayed,
20+
'ocrClickOnText': ocrClickOnText,
21+
'ocrSetValue': ocrSetValue,
2022
}
2123

2224
export default class WdioOcrService {
2325
private _browser?: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser
2426
private _ocrDir: string
2527
private _ocrLanguage: string
2628
private _ocrContrast: number
29+
private _isTesseractAvailable: boolean
2730

2831
constructor(options: OcrOptions) {
2932
this._ocrDir = createOcrDir(options?.imagesFolder || DEFAULT_IMAGES_FOLDER)
3033
this._ocrLanguage = options?.language || SUPPORTED_LANGUAGES.ENGLISH
3134
this._ocrContrast = options?.contrast || CONTRAST
35+
this._ocrLanguage = options?.language || SUPPORTED_LANGUAGES.ENGLISH
36+
this._isTesseractAvailable = isSystemTesseractAvailable()
3237
}
3338

3439
/**
@@ -58,45 +63,47 @@ export default class WdioOcrService {
5863
const browserNames = Object.keys(capabilities)
5964
const self = this
6065
log.info(`Adding commands to Multi Browser: ${browserNames.join(', ')}`)
61-
62-
for (const browserName of browserNames) {
63-
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
64-
const browserInstance = multiremoteBrowser.getInstance(browserName)
65-
await this.#addCommandsToBrowser(browserInstance)
66-
}
67-
6866
/**
6967
* Add all OCR commands to the global browser object that will execute
70-
* on each browser in the Multi Remote.
7168
*/
72-
for (const command of Object.keys(ocrCommands)) {
73-
browser.addCommand(command, async function (...args: unknown[]) {
69+
for (const commandName of Object.keys(ocrCommands)) {
70+
browser.addCommand(commandName, async function (...args: unknown[]) {
7471
const returnData: Record<string, any> = {}
7572

7673
if (typeof args[0] === 'object' && args[0] !== null) {
7774
const options = args[0] as Record<string, any>
75+
options.ocrImagesPath = options?.imagesFolder || self._ocrDir
7876
options.contrast = options?.contrast || self._ocrContrast
77+
options.language = options?.language || self._ocrLanguage
78+
options.isTesseractAvailable = self._isTesseractAvailable
7979
args[0] = options
8080
}
8181

8282
for (const browserName of browserNames) {
8383
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
8484
const browserInstance = multiremoteBrowser.getInstance(browserName) as WebdriverIO.Browser & Record<string, any>
8585

86-
if (typeof browserInstance[command] === 'function') {
87-
returnData[browserName] = await browserInstance[command].apply(browserInstance, args)
86+
if (typeof browserInstance[commandName] === 'function') {
87+
returnData[browserName] = await ocrCommands[commandName].call(browserInstance, args[0])
8888
} else {
89-
throw new Error(`Command ${command} is not a function on the browser instance ${browserName}`)
89+
throw new Error(`Command ${commandName} is not a function on the browser instance ${browserName}`)
9090
}
9191
}
9292

9393
return returnData
9494
})
9595
}
96+
/**
97+
* Add all OCR commands to each instance (but Single Remote version)
98+
*/
99+
for (const browserName of browserNames) {
100+
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
101+
const browserInstance = multiremoteBrowser.getInstance(browserName)
102+
await this.#addCommandsToBrowser(browserInstance)
103+
}
96104
}
97105

98106
async #addCommandsToBrowser(currentBrowser: WebdriverIO.Browser) {
99-
const isTesseractAvailable = isSystemTesseractAvailable()
100107
const self = this
101108

102109
for (const [commandName, command] of Object.entries(ocrCommands)) {
@@ -106,10 +113,10 @@ export default class WdioOcrService {
106113
function (this: typeof currentBrowser, options) {
107114
return command.bind(this)({
108115
...options,
116+
ocrImagesPath: self._ocrDir,
109117
contrast: options?.contrast || self._ocrContrast,
110-
isTesseractAvailable,
111118
language: options?.language || self._ocrLanguage,
112-
ocrImagesPath: self._ocrDir,
119+
isTesseractAvailable: self._isTesseractAvailable
113120
})
114121
}
115122
)

0 commit comments

Comments
 (0)