Skip to content

Commit f67f623

Browse files
committed
realsolution
1 parent 9e0f788 commit f67f623

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

.changeset/spotty-bananas-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wdio/ocr-service": minor
3+
---
4+
5+
\- For Multiremote addCommande, the OCR functions are first add to the browser object and then to each instances to avoid having the loop for each instance in the OCR command of a single instance (function with the loop no longer call itself like in the issue 657 and are correctly declared to the correct element to fix issue 983)

packages/ocr-service/src/service.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ export default class WdioOcrService {
2424
private _ocrDir: string
2525
private _ocrLanguage: string
2626
private _ocrContrast: number
27+
private _isTesseractAvailable: boolean
2728

2829
constructor(options: OcrOptions) {
2930
this._ocrDir = createOcrDir(options?.imagesFolder || DEFAULT_IMAGES_FOLDER)
3031
this._ocrLanguage = options?.language || SUPPORTED_LANGUAGES.ENGLISH
3132
this._ocrContrast = options?.contrast || CONTRAST
33+
this._ocrLanguage = options?.language || SUPPORTED_LANGUAGES.ENGLISH
34+
this._isTesseractAvailable = isSystemTesseractAvailable()
3235
}
3336

3437
/**
@@ -58,45 +61,47 @@ export default class WdioOcrService {
5861
const browserNames = Object.keys(capabilities)
5962
const self = this
6063
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-
6864
/**
6965
* Add all OCR commands to the global browser object that will execute
70-
* on each browser in the Multi Remote.
7166
*/
72-
for (const command of Object.keys(ocrCommands)) {
73-
browser.addCommand(command, async function (...args: unknown[]) {
67+
for (const commandName of Object.keys(ocrCommands)) {
68+
browser.addCommand(commandName, async function (...args: unknown[]) {
7469
const returnData: Record<string, any> = {}
7570

7671
if (typeof args[0] === 'object' && args[0] !== null) {
7772
const options = args[0] as Record<string, any>
73+
options.ocrImagesPath = options?.imagesFolder || self._ocrDir
7874
options.contrast = options?.contrast || self._ocrContrast
75+
options.language = options?.language || self._ocrLanguage
76+
options.isTesseractAvailable = self._isTesseractAvailable
7977
args[0] = options
8078
}
8179

8280
for (const browserName of browserNames) {
8381
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
8482
const browserInstance = multiremoteBrowser.getInstance(browserName) as WebdriverIO.Browser & Record<string, any>
8583

86-
if (typeof browserInstance[command] === 'function') {
87-
returnData[browserName] = await browserInstance[command].apply(browserInstance, args)
84+
if (typeof browserInstance[commandName] === 'function') {
85+
returnData[browserName] = await browserInstance[commandName].apply(browserInstance, args)
8886
} else {
89-
throw new Error(`Command ${command} is not a function on the browser instance ${browserName}`)
87+
throw new Error(`Command ${commandName} is not a function on the browser instance ${browserName}`)
9088
}
9189
}
9290

9391
return returnData
9492
})
9593
}
94+
/**
95+
* Add all OCR commands to each instance (but Single Remote version)
96+
*/
97+
for (const browserName of browserNames) {
98+
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
99+
const browserInstance = multiremoteBrowser.getInstance(browserName)
100+
await this.#addCommandsToBrowser(browserInstance)
101+
}
96102
}
97103

98104
async #addCommandsToBrowser(currentBrowser: WebdriverIO.Browser) {
99-
const isTesseractAvailable = isSystemTesseractAvailable()
100105
const self = this
101106

102107
for (const [commandName, command] of Object.entries(ocrCommands)) {
@@ -106,10 +111,10 @@ export default class WdioOcrService {
106111
function (this: typeof currentBrowser, options) {
107112
return command.bind(this)({
108113
...options,
114+
ocrImagesPath: self._ocrDir,
109115
contrast: options?.contrast || self._ocrContrast,
110-
isTesseractAvailable,
111116
language: options?.language || self._ocrLanguage,
112-
ocrImagesPath: self._ocrDir,
117+
isTesseractAvailable: self._isTesseractAvailable
113118
})
114119
}
115120
)

0 commit comments

Comments
 (0)