1
1
import logger from '@wdio/logger'
2
- import type { Capabilities } from '@wdio/types'
3
2
import { isSystemTesseractAvailable } from './utils/tesseract.js'
4
3
import { CONTRAST , DEFAULT_IMAGES_FOLDER , SUPPORTED_LANGUAGES } from './utils/constants.js'
5
4
import { createOcrDir } from './utils/index.js'
@@ -10,25 +9,29 @@ import ocrWaitForTextDisplayed from './commands/ocrWaitForTextDisplayed.js'
10
9
import ocrClickOnText from './commands/ocrClickOnText.js'
11
10
import ocrSetValue from './commands/ocrSetValue.js'
12
11
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 ,
20
20
}
21
21
22
+ const log = logger ( '@wdio/ocr-service' )
23
+
22
24
export default class WdioOcrService {
23
- private _browser ?: WebdriverIO . Browser | WebdriverIO . MultiRemoteBrowser
24
25
private _ocrDir : string
25
26
private _ocrLanguage : string
26
27
private _ocrContrast : number
28
+ private _isTesseractAvailable : boolean
27
29
28
30
constructor ( options : OcrOptions ) {
29
31
this . _ocrDir = createOcrDir ( options ?. imagesFolder || DEFAULT_IMAGES_FOLDER )
30
- 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
/**
@@ -43,76 +46,51 @@ export default class WdioOcrService {
43
46
_specs : string [ ] ,
44
47
browser : WebdriverIO . Browser | WebdriverIO . MultiRemoteBrowser
45
48
) {
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 )
59
49
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 )
68
51
/**
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
71
53
*/
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
+ ) {
76
60
if ( typeof args [ 0 ] === 'object' && args [ 0 ] !== null ) {
77
61
const options = args [ 0 ] as Record < string , any >
62
+ options . ocrImagesPath = options ?. imagesFolder || self . _ocrDir
78
63
options . contrast = options ?. contrast || self . _ocrContrast
64
+ options . language = options ?. language || self . _ocrLanguage
65
+ options . isTesseractAvailable = self . _isTesseractAvailable
79
66
args [ 0 ] = options
80
67
}
81
68
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
+ ]
85
80
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 ] )
88
85
} 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 } ` )
90
87
}
91
88
}
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
114
91
}
115
- )
92
+ return Object . values ( returnData ) [ 0 ]
93
+ } )
116
94
}
117
95
}
118
96
}
0 commit comments