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'
@@ -9,26 +8,31 @@ import ocrGetElementPositionByText from './commands/ocrGetElementPositionByText.
9
8
import ocrWaitForTextDisplayed from './commands/ocrWaitForTextDisplayed.js'
10
9
import ocrClickOnText from './commands/ocrClickOnText.js'
11
10
import ocrSetValue from './commands/ocrSetValue.js'
11
+ import type { OcrGetTextOptions , OcrGetElementPositionByTextOptions , OcrWaitForTextDisplayedOptions , OcrClickOnTextOptions , OcrSetValueOptions } from './types.js'
12
12
13
- const log = logger ( '@wdio/ocr-service' )
14
- const ocrCommands = {
15
- ocrGetText,
16
- ocrGetElementPositionByText,
17
- ocrWaitForTextDisplayed,
18
- ocrClickOnText,
19
- ocrSetValue,
13
+ const ocrCommands : {
14
+ [ key : string ] : ( context : any , options : any ) => Promise < any >
15
+ } = {
16
+ 'ocrGetText' : async ( context , options ) => ocrGetText ( context , options as OcrGetTextOptions ) ,
17
+ 'ocrGetElementPositionByText' : async ( context , options ) => ocrGetElementPositionByText ( context , options as OcrGetElementPositionByTextOptions ) ,
18
+ 'ocrWaitForTextDisplayed' : async ( context , options ) => ocrWaitForTextDisplayed ( context , options as OcrWaitForTextDisplayedOptions ) ,
19
+ 'ocrClickOnText' : async ( context , options ) => ocrClickOnText ( context , options as OcrClickOnTextOptions ) ,
20
+ 'ocrSetValue' : async ( context , options ) => ocrSetValue ( context , options as OcrSetValueOptions ) ,
20
21
}
21
22
23
+ const log = logger ( '@wdio/ocr-service' )
24
+
22
25
export default class WdioOcrService {
23
- 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
- this . _ocrLanguage = options ?. language || SUPPORTED_LANGUAGES . ENGLISH
31
33
this . _ocrContrast = options ?. contrast || CONTRAST
34
+ this . _ocrLanguage = options ?. language || SUPPORTED_LANGUAGES . ENGLISH
35
+ this . _isTesseractAvailable = isSystemTesseractAvailable ( )
32
36
}
33
37
34
38
/**
@@ -43,76 +47,44 @@ export default class WdioOcrService {
43
47
_specs : string [ ] ,
44
48
browser : WebdriverIO . Browser | WebdriverIO . MultiRemoteBrowser
45
49
) {
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
50
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
-
51
+ const browserNames = Object . keys ( capabilities )
68
52
/**
69
- * Add all OCR commands to the global browser object that will execute
70
- * on each browser in the Multi Remote.
53
+ * Add all OCR commands to the browser object and instances
71
54
*/
72
- for ( const command of Object . keys ( ocrCommands ) ) {
73
- browser . addCommand ( command , async function ( ...args : unknown [ ] ) {
74
- const returnData : Record < string , any > = { }
75
-
55
+ for ( const commandName of Object . keys ( ocrCommands ) ) {
56
+ log . info ( `Adding browser command "${ commandName } " to browser object` )
57
+ browser . addCommand ( commandName , async function (
58
+ this : WebdriverIO . Browser | WebdriverIO . MultiRemoteBrowser ,
59
+ ...args : unknown [ ]
60
+ ) {
76
61
if ( typeof args [ 0 ] === 'object' && args [ 0 ] !== null ) {
77
62
const options = args [ 0 ] as Record < string , any >
63
+ options . ocrImagesPath = options ?. imagesFolder || self . _ocrDir
78
64
options . contrast = options ?. contrast || self . _ocrContrast
65
+ options . language = options ?. language || self . _ocrLanguage
66
+ options . isTesseractAvailable = self . _isTesseractAvailable
79
67
args [ 0 ] = options
80
68
}
81
-
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 >
85
-
86
- if ( typeof browserInstance [ command ] === 'function' ) {
87
- returnData [ browserName ] = await browserInstance [ command ] . apply ( browserInstance , args )
88
- } else {
89
- throw new Error ( `Command ${ command } is not a function on the browser instance ${ browserName } ` )
69
+ if ( this . isMultiremote ) {
70
+ const returnData : Record < string , any > = { }
71
+ for ( const browserName of browserNames ) {
72
+ const multiremoteBrowser = browser as WebdriverIO . MultiRemoteBrowser
73
+ const browserInstance = multiremoteBrowser . getInstance ( browserName ) as WebdriverIO . Browser & Record < string , any >
74
+ if ( typeof browserInstance [ commandName ] === 'function' ) {
75
+ returnData [ browserName ] = await browserInstance [ commandName ] . call ( browserInstance , args [ 0 ] )
76
+ } else {
77
+ throw new Error ( `Command ${ commandName } is not a function on the browser instance ${ browserName } ` )
78
+ }
90
79
}
80
+ return returnData
91
81
}
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
- } )
82
+ const handler = ocrCommands [ commandName ]
83
+ if ( handler ) {
84
+ return await handler ( this , args [ 0 ] )
114
85
}
115
- )
86
+ throw new Error ( `Command ${ commandName } is not a function of browser.` )
87
+ } )
116
88
}
117
89
}
118
90
}
0 commit comments