Skip to content

Commit ebedcb6

Browse files
committed
issue656_fix_attempt_1
1 parent 9e0f788 commit ebedcb6

File tree

9 files changed

+2336
-2313
lines changed

9 files changed

+2336
-2313
lines changed

.changeset/spotty-bananas-glow.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@wdio/ocr-service": minor
3+
---
4+
5+
\- No more distinction between multiremote and single remote during the addCommand step
6+
7+
\- Distinction between multiremote and single remote inside the commande function (loop for each instance or directly call the function)
8+
9+
- Inside browser command, call OCR functions directly with secured context instead of the browser function (to avoir loop from issue 657)

packages/ocr-service/src/commands/ocrClickOnText.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { OcrClickOnTextOptions } from '../types.js'
44
import { drawTarget } from '../utils/imageProcessing.js'
55

66
export default async function ocrClickOnText(this: WebdriverIO.Browser, options: OcrClickOnTextOptions): Promise<void> {
7-
const element = await ocrGetElementPositionByText.bind(this)(options)
7+
const element = await ocrGetElementPositionByText.call(this, options)
88
let { x, y } = determineClickPoint({ rectangles: element.dprPosition })
99
const { relativePosition } = options
1010

@@ -30,4 +30,4 @@ export default async function ocrClickOnText(this: WebdriverIO.Browser, options:
3030
.pause(clickDuration as number)
3131
.up({ button: 0 })
3232
.perform()
33-
}
33+
}

packages/ocr-service/src/commands/ocrGetElementPositionByText.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ export default async function ocrGetElementPositionByText(
6363
score,
6464
searchValue: text,
6565
}
66-
}
66+
}

packages/ocr-service/src/commands/ocrGetText.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export default async function ocrGetText(this: WebdriverIO.Browser, options: Ocr
55
const { text } = await getData(this, options)
66

77
return text.replace(/\n\s*\n/g, '\n')
8-
}
8+
}

packages/ocr-service/src/commands/ocrSetValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ export default async function ocrSetValue(this: WebdriverIO.Browser, options: Oc
5353
// Keyboard is not present or not hidden
5454
}
5555
}
56-
}
56+
}

packages/ocr-service/src/commands/ocrWaitForTextDisplayed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ export default async function ocrWaitForTextDisplayed(
1414
timeoutMsg: timeoutMsg || `Could not find the text "${options.text}" within the requested time.`,
1515
}
1616
)
17-
}
17+
}

packages/ocr-service/src/service.ts

Lines changed: 44 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logger from '@wdio/logger'
2-
import type { Capabilities } from '@wdio/types'
32
import { isSystemTesseractAvailable } from './utils/tesseract.js'
43
import { CONTRAST, DEFAULT_IMAGES_FOLDER, SUPPORTED_LANGUAGES } from './utils/constants.js'
54
import { createOcrDir } from './utils/index.js'
@@ -10,25 +9,29 @@ import ocrWaitForTextDisplayed from './commands/ocrWaitForTextDisplayed.js'
109
import ocrClickOnText from './commands/ocrClickOnText.js'
1110
import ocrSetValue from './commands/ocrSetValue.js'
1211

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

22+
const log = logger('@wdio/ocr-service')
23+
2224
export default class WdioOcrService {
23-
private _browser?: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser
2425
private _ocrDir: string
2526
private _ocrLanguage: string
2627
private _ocrContrast: number
28+
private _isTesseractAvailable: boolean
2729

2830
constructor(options: OcrOptions) {
2931
this._ocrDir = createOcrDir(options?.imagesFolder || DEFAULT_IMAGES_FOLDER)
30-
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
/**
@@ -43,76 +46,51 @@ export default class WdioOcrService {
4346
_specs: string[],
4447
browser: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser
4548
) {
46-
this._browser = browser
47-
48-
if (!this._browser.isMultiremote) {
49-
log.info('Adding commands to global browser')
50-
await this.#addCommandsToBrowser(this._browser)
51-
} else {
52-
await this.#extendMultiremoteBrowser(capabilities as Capabilities.RequestedMultiremoteCapabilities)
53-
}
54-
}
55-
56-
async #extendMultiremoteBrowser (capabilities: Capabilities.RequestedMultiremoteCapabilities) {
57-
const browser = this._browser as WebdriverIO.MultiRemoteBrowser
58-
const browserNames = Object.keys(capabilities)
5949
const self = this
60-
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-
50+
const browserNames = Object.keys(capabilities)
6851
/**
69-
* Add all OCR commands to the global browser object that will execute
70-
* on each browser in the Multi Remote.
52+
* Add all OCR commands to the browser object and instances
7153
*/
72-
for (const command of Object.keys(ocrCommands)) {
73-
browser.addCommand(command, async function (...args: unknown[]) {
74-
const returnData: Record<string, any> = {}
75-
54+
for (const commandName of Object.keys(ocrCommands)) {
55+
log.info(`Adding browser command "${commandName}" to browser object`)
56+
browser.addCommand(commandName, async function (
57+
this: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser,
58+
...args: unknown[]
59+
) {
7660
if (typeof args[0] === 'object' && args[0] !== null) {
7761
const options = args[0] as Record<string, any>
62+
options.ocrImagesPath = options?.imagesFolder || self._ocrDir
7863
options.contrast = options?.contrast || self._ocrContrast
64+
options.language = options?.language || self._ocrLanguage
65+
options.isTesseractAvailable = self._isTesseractAvailable
7966
args[0] = options
8067
}
8168

82-
for (const browserName of browserNames) {
83-
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
84-
const browserInstance = multiremoteBrowser.getInstance(browserName) as WebdriverIO.Browser & Record<string, any>
69+
const instancesToLoop: { 'browserName': string; 'browserInstance': WebdriverIO.Browser & Record<string, any> }[] = this.isMultiremote
70+
? Object.values(browserNames).map(browserName => ({
71+
'browserName': browserName,
72+
'browserInstance': this.getInstance(browserName),
73+
}))
74+
: [
75+
{
76+
'browserName': this.capabilities.browserName || 'browser',
77+
'browserInstance': this as WebdriverIO.Browser & Record<string, any>,
78+
},
79+
]
8580

86-
if (typeof browserInstance[command] === 'function') {
87-
returnData[browserName] = await browserInstance[command].apply(browserInstance, args)
81+
const returnData: Record<string, any> = {}
82+
for (const { browserName, browserInstance } of instancesToLoop) {
83+
if (typeof browserInstance[commandName] === 'function') {
84+
returnData[browserName] = await ocrCommands[commandName].call(browserInstance, args[0])
8885
} else {
89-
throw new Error(`Command ${command} is not a function on the browser instance ${browserName}`)
86+
throw new Error(`Command ${commandName} is not a function on the browser instance ${browserName}`)
9087
}
9188
}
92-
93-
return returnData
94-
})
95-
}
96-
}
97-
98-
async #addCommandsToBrowser(currentBrowser: WebdriverIO.Browser) {
99-
const isTesseractAvailable = isSystemTesseractAvailable()
100-
const self = this
101-
102-
for (const [commandName, command] of Object.entries(ocrCommands)) {
103-
log.info(`Adding browser command "${commandName}" to browser object`)
104-
currentBrowser.addCommand(
105-
commandName,
106-
function (this: typeof currentBrowser, options) {
107-
return command.bind(this)({
108-
...options,
109-
contrast: options?.contrast || self._ocrContrast,
110-
isTesseractAvailable,
111-
language: options?.language || self._ocrLanguage,
112-
ocrImagesPath: self._ocrDir,
113-
})
89+
if (this.isMultiremote) {
90+
return returnData
11491
}
115-
)
92+
return Object.values(returnData)[0]
93+
})
11694
}
11795
}
11896
}

0 commit comments

Comments
 (0)