Skip to content

Issue 657 #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions packages/ocr-service/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ export default class WdioOcrService {
private _ocrDir: string
private _ocrLanguage: string
private _ocrContrast: number
private _isTesseractAvailable: boolean

constructor(options: OcrOptions) {
this._ocrDir = createOcrDir(options?.imagesFolder || DEFAULT_IMAGES_FOLDER)
this._ocrLanguage = options?.language || SUPPORTED_LANGUAGES.ENGLISH
this._ocrContrast = options?.contrast || CONTRAST
this._ocrLanguage = options?.language || SUPPORTED_LANGUAGES.ENGLISH
this._isTesseractAvailable = isSystemTesseractAvailable()
}

/**
Expand Down Expand Up @@ -58,45 +61,47 @@ export default class WdioOcrService {
const browserNames = Object.keys(capabilities)
const self = this
log.info(`Adding commands to Multi Browser: ${browserNames.join(', ')}`)

for (const browserName of browserNames) {
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
const browserInstance = multiremoteBrowser.getInstance(browserName)
await this.#addCommandsToBrowser(browserInstance)
}

/**
* Add all OCR commands to the global browser object that will execute
* on each browser in the Multi Remote.
* Add all commands to the global browser object that will execute on each browser in the Multi Remote.
*/
for (const command of Object.keys(ocrCommands)) {
browser.addCommand(command, async function (...args: unknown[]) {
for (const commandName of Object.keys(ocrCommands)) {
browser.addCommand(commandName, async function (...args: unknown[]) {
const returnData: Record<string, any> = {}

if (typeof args[0] === 'object' && args[0] !== null) {
const options = args[0] as Record<string, any>
options.ocrImagesPath = options?.imagesFolder || self._ocrDir
options.contrast = options?.contrast || self._ocrContrast
options.language = options?.language || self._ocrLanguage
options.isTesseractAvailable = self._isTesseractAvailable
args[0] = options
}

for (const browserName of browserNames) {
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
const browserInstance = multiremoteBrowser.getInstance(browserName) as WebdriverIO.Browser & Record<string, any>

if (typeof browserInstance[command] === 'function') {
returnData[browserName] = await browserInstance[command].apply(browserInstance, args)
if (typeof browserInstance[commandName] === 'function') {
returnData[browserName] = await browserInstance[commandName].apply(browserInstance, args)
} else {
throw new Error(`Command ${command} is not a function on the browser instance ${browserName}`)
throw new Error(`Command ${commandName} is not a function on the browser instance ${browserName}`)
}
}

return returnData
})
}
/**
* Add all commands to each instance (but Single Remote version)
*/
for (const browserName of browserNames) {
const multiremoteBrowser = browser as WebdriverIO.MultiRemoteBrowser
const browserInstance = multiremoteBrowser.getInstance(browserName)
await this.#addCommandsToBrowser(browserInstance)
}
}

async #addCommandsToBrowser(currentBrowser: WebdriverIO.Browser) {
const isTesseractAvailable = isSystemTesseractAvailable()
const self = this

for (const [commandName, command] of Object.entries(ocrCommands)) {
Expand All @@ -106,13 +111,13 @@ export default class WdioOcrService {
function (this: typeof currentBrowser, options) {
return command.bind(this)({
...options,
ocrImagesPath: self._ocrDir,
contrast: options?.contrast || self._ocrContrast,
isTesseractAvailable,
language: options?.language || self._ocrLanguage,
ocrImagesPath: self._ocrDir,
isTesseractAvailable: self._isTesseractAvailable
})
}
)
}
}
}
}