-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathocrGetData.test.ts
More file actions
167 lines (151 loc) · 6.8 KB
/
ocrGetData.test.ts
File metadata and controls
167 lines (151 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { join } from 'node:path'
import logger from '@wdio/logger'
import { browser as wdioBrowser } from '@wdio/globals'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import getData from '../../src/utils/getData.js'
import * as Tesseract from '../../src/utils/tesseract.js'
import * as ImageProcessing from '../../src/utils/imageProcessing.js'
import { adjustElementBbox, getBase64ScreenshotSize, isRectanglesObject } from '../../src/utils/index.js'
const browser = wdioBrowser
const log = logger('test')
vi.mock('@wdio/logger', () => import(join(process.cwd(), '__mocks__', '@wdio/logger')))
vi.mock('../../src/utils/tesseract.js')
vi.mock('../../src/utils/imageProcessing.js')
vi.mock('../../src/utils/index.js')
vi.mock('webdriver-image-comparison/dist/helpers/utils', () => ({
getBase64ScreenshotSize: vi.fn(() => ({ width: 1200 })),
}))
vi.mock('../../src/utils/tesseract.js', () => ({
getNodeOcrData: vi.fn().mockResolvedValue({
text: 'Sample OCR text',
lines: [],
words: []
}),
getSystemOcrData: vi.fn().mockResolvedValue({
text: 'Sample OCR text',
lines: [{ text: 'Sample OCR text', bbox: { left: 1, right: 1, top: 1, bottom: 1 } }],
words: [
{ text: 'Sample', bbox: { left: 1, right: 1, top: 1, bottom: 1 } },
{ text: 'OCR', bbox: { left: 2, right: 2, top: 2, bottom: 2 } },
{ text: 'text', bbox: { left: 3, right: 3, top: 3, bottom: 3 } },
]
})
}))
vi.mock('../../src/utils/imageProcessing.js', () => ({
processImage: vi.fn().mockResolvedValue({ filePath: '/fake/path/image.png' }),
drawHighlightedWords: vi.fn().mockResolvedValue('')
}))
vi.mock('@wdio/globals', () => ({
browser: {
isAndroid: false,
isIOS: false,
getElementRect:vi.fn().mockResolvedValue({ x: 10, y: 20, width: 300, height: 400 }),
getBase64ScreenshotSize:vi.fn().mockReturnValue({ width: 1200 }),
getWindowSize: vi.fn().mockResolvedValue({ width: 1200, height: 800 }),
takeScreenshot:vi.fn().mockResolvedValue('screenshotData'),
}
}))
describe('getData', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('processes OCR data correctly using system Tesseract when available', async () => {
const logInfoMock = vi.spyOn(log, 'info')
const options = {
contrast: 1.0,
isTesseractAvailable: true,
language: 'ENG',
ocrImagesPath: '/fake/ocr/path'
}
vi.mocked(getBase64ScreenshotSize).mockReturnValue({ height: 1200, width: 1200 })
const result = await getData(browser, options)
expect(browser.getWindowSize).toHaveBeenCalled()
expect(browser.takeScreenshot).toHaveBeenCalled()
expect(getBase64ScreenshotSize).toHaveBeenCalledWith('screenshotData')
expect(isRectanglesObject).not.toHaveBeenCalled()
expect(ImageProcessing.processImage).toHaveBeenCalledTimes(1)
expect(Tesseract.getSystemOcrData).toHaveBeenCalledWith({
filePath: '/fake/path/image.png',
language: 'ENG'
})
expect(adjustElementBbox).not.toHaveBeenCalled()
expect(logInfoMock.mock.calls).toMatchSnapshot()
expect(result).toMatchSnapshot()
})
it('processes OCR data correctly using NodeJS Tesseract', async () => {
const logInfoMock = vi.spyOn(log, 'info')
const options = {
contrast: 1.0,
isTesseractAvailable: false,
language: 'ENG',
ocrImagesPath: '/fake/ocr/path'
}
vi.mocked(getBase64ScreenshotSize).mockReturnValue({ height: 1200, width: 1200 })
const result = await getData(browser, options)
expect(browser.getWindowSize).toHaveBeenCalled()
expect(browser.takeScreenshot).toHaveBeenCalled()
expect(getBase64ScreenshotSize).toHaveBeenCalledWith('screenshotData')
expect(isRectanglesObject).not.toHaveBeenCalled()
expect(ImageProcessing.processImage).toHaveBeenCalledTimes(1)
expect(Tesseract.getNodeOcrData).toHaveBeenCalledWith({
filePath: '/fake/path/image.png',
language: 'ENG'
})
expect(adjustElementBbox).not.toHaveBeenCalled()
expect(logInfoMock.mock.calls).toMatchSnapshot()
expect(result).toMatchSnapshot()
})
it('processes OCR data correctly using a haystack', async () => {
const logInfoMock = vi.spyOn(log, 'info')
const options = {
contrast: 1.0,
isTesseractAvailable: true,
haystack: { x: 5, y: 5, width: 50, height: 50 },
language: 'ENG',
ocrImagesPath: '/fake/ocr/path'
}
vi.mocked(getBase64ScreenshotSize).mockReturnValue({ height: 1200, width: 1200 })
const result = await getData(browser, options)
expect(browser.getWindowSize).toHaveBeenCalled()
expect(browser.takeScreenshot).toHaveBeenCalled()
expect(getBase64ScreenshotSize).toHaveBeenCalledWith('screenshotData')
expect(isRectanglesObject).toHaveBeenCalledTimes(1)
expect(ImageProcessing.processImage).toHaveBeenCalledTimes(2)
expect(Tesseract.getSystemOcrData).toHaveBeenCalledWith({
filePath: '/fake/path/image.png',
language: 'ENG'
})
expect(adjustElementBbox).toHaveBeenCalledTimes(4)
expect(logInfoMock.mock.calls).toMatchSnapshot()
expect(result).toMatchSnapshot()
})
it('should handle haystack that requires fetching RectReturn from browser.getElementRect', async () => {
const logInfoMock = vi.spyOn(log, 'info')
const options = {
contrast: 1.0,
isTesseractAvailable: true,
language: 'ENG',
ocrImagesPath: '/fake/ocr/path',
haystack: { elementId: 'element123' } as WebdriverIO.Element
}
// @ts-ignore
isRectanglesObject.mockReturnValue(false)
browser.getElementRect = vi.fn().mockResolvedValue({ x: 10, y: 20, width: 300, height: 400 })
const result = await getData(browser, options)
expect(isRectanglesObject).toHaveBeenCalledWith(options.haystack)
expect(browser.getElementRect).toHaveBeenCalledWith('element123')
expect(result).toHaveProperty('filePath', '/fake/path/image.png')
expect(logInfoMock.mock.calls).toMatchSnapshot()
})
it('handles errors when screenshot capture fails', async () => {
const options = {
contrast: 1.0,
haystack: { x: 5, y: 5, width: 50, height: 50 },
isTesseractAvailable: true,
language: 'ENG',
ocrImagesPath: '/fake/ocr/path'
}
browser.takeScreenshot = vi.fn().mockRejectedValue(new Error('Failed to take screenshot'))
await expect(getData(browser, options)).rejects.toThrow('Failed to take screenshot')
})
})