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