@@ -11,24 +11,29 @@ import ocrClickOnText from './commands/ocrClickOnText.js'
11
11
import ocrSetValue from './commands/ocrSetValue.js'
12
12
13
13
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 ,
20
22
}
21
23
22
24
export default class WdioOcrService {
23
25
private _browser ?: WebdriverIO . Browser | WebdriverIO . MultiRemoteBrowser
24
26
private _ocrDir : string
25
27
private _ocrLanguage : string
26
28
private _ocrContrast : number
29
+ private _isTesseractAvailable : boolean
27
30
28
31
constructor ( options : OcrOptions ) {
29
32
this . _ocrDir = createOcrDir ( options ?. imagesFolder || DEFAULT_IMAGES_FOLDER )
30
33
this . _ocrLanguage = options ?. language || SUPPORTED_LANGUAGES . ENGLISH
31
34
this . _ocrContrast = options ?. contrast || CONTRAST
35
+ this . _ocrLanguage = options ?. language || SUPPORTED_LANGUAGES . ENGLISH
36
+ this . _isTesseractAvailable = isSystemTesseractAvailable ( )
32
37
}
33
38
34
39
/**
@@ -58,45 +63,47 @@ export default class WdioOcrService {
58
63
const browserNames = Object . keys ( capabilities )
59
64
const self = this
60
65
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
-
68
66
/**
69
67
* Add all OCR commands to the global browser object that will execute
70
- * on each browser in the Multi Remote.
71
68
*/
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 [ ] ) {
74
71
const returnData : Record < string , any > = { }
75
72
76
73
if ( typeof args [ 0 ] === 'object' && args [ 0 ] !== null ) {
77
74
const options = args [ 0 ] as Record < string , any >
75
+ options . ocrImagesPath = options ?. imagesFolder || self . _ocrDir
78
76
options . contrast = options ?. contrast || self . _ocrContrast
77
+ options . language = options ?. language || self . _ocrLanguage
78
+ options . isTesseractAvailable = self . _isTesseractAvailable
79
79
args [ 0 ] = options
80
80
}
81
81
82
82
for ( const browserName of browserNames ) {
83
83
const multiremoteBrowser = browser as WebdriverIO . MultiRemoteBrowser
84
84
const browserInstance = multiremoteBrowser . getInstance ( browserName ) as WebdriverIO . Browser & Record < string , any >
85
85
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 ] )
88
88
} 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 } ` )
90
90
}
91
91
}
92
92
93
93
return returnData
94
94
} )
95
95
}
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
+ }
96
104
}
97
105
98
106
async #addCommandsToBrowser( currentBrowser : WebdriverIO . Browser ) {
99
- const isTesseractAvailable = isSystemTesseractAvailable ( )
100
107
const self = this
101
108
102
109
for ( const [ commandName , command ] of Object . entries ( ocrCommands ) ) {
@@ -106,10 +113,10 @@ export default class WdioOcrService {
106
113
function ( this : typeof currentBrowser , options ) {
107
114
return command . bind ( this ) ( {
108
115
...options ,
116
+ ocrImagesPath : self . _ocrDir ,
109
117
contrast : options ?. contrast || self . _ocrContrast ,
110
- isTesseractAvailable,
111
118
language : options ?. language || self . _ocrLanguage ,
112
- ocrImagesPath : self . _ocrDir ,
119
+ isTesseractAvailable : self . _isTesseractAvailable
113
120
} )
114
121
}
115
122
)
0 commit comments