|
| 1 | +import { assert, Builder, By, isEmpty, Kia } from '../../deps.ts' |
| 2 | +import type { |
| 3 | + Data, |
| 4 | + DriverParams, |
| 5 | + DrowserDriverResponse, |
| 6 | + DrowserServiceCase, |
| 7 | + DrowserThenableWebDriver, |
| 8 | +} from '../types.ts' |
| 9 | +import { isValidHttpUrl } from '../utils.ts' |
| 10 | +import { driverBrowsers, seleniumExceptions } from '../constants.ts' |
| 11 | +import { exportGeneratedLog, exportJSONReport } from './export.ts' |
| 12 | +import { getConfig } from './config.ts' |
| 13 | +import { validateParams } from './validate.ts' |
| 14 | +import { processServiceCase } from './process.ts' |
| 15 | + |
| 16 | +const driver = async ({ |
| 17 | + browser, |
| 18 | +}: DriverParams): Promise<DrowserDriverResponse> => { |
| 19 | + const data: Data = { url: '', results: [] } |
| 20 | + |
| 21 | + const config = await getConfig() |
| 22 | + data.url = config.url |
| 23 | + |
| 24 | + validateParams({ browser }) |
| 25 | + |
| 26 | + return new Promise<DrowserDriverResponse>((resolve, reject) => { |
| 27 | + if (isEmpty(data.url) || !isValidHttpUrl({ url: data.url })) reject() |
| 28 | + |
| 29 | + const builder = new Builder() |
| 30 | + .forBrowser(driverBrowsers[browser]) |
| 31 | + .build() as DrowserThenableWebDriver |
| 32 | + |
| 33 | + const service = { cases: [] } |
| 34 | + |
| 35 | + const kia = new Kia('Processing your tests') |
| 36 | + kia.start() |
| 37 | + |
| 38 | + builder |
| 39 | + .get(data.url) |
| 40 | + .then(() => resolve({ service })) |
| 41 | + .catch((error: Record<string, string>) => { |
| 42 | + kia.fail('An error occurred while running tests') |
| 43 | + reject(seleniumExceptions[error.name]) |
| 44 | + }) |
| 45 | + .finally(() => { |
| 46 | + const methodPromises: Promise<void>[] = [] |
| 47 | + |
| 48 | + service.cases.forEach((serviceCase: DrowserServiceCase) => { |
| 49 | + if (typeof serviceCase === 'object') { |
| 50 | + const methodPromise = processServiceCase( |
| 51 | + serviceCase, |
| 52 | + builder, |
| 53 | + assert, |
| 54 | + By, |
| 55 | + browser, |
| 56 | + data, |
| 57 | + ) |
| 58 | + methodPromises.push(methodPromise) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + const exportGeneratedFiles = () => { |
| 63 | + exportGeneratedLog({ results: data.results }) |
| 64 | + exportJSONReport({ |
| 65 | + results: data.results, |
| 66 | + browser, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + Promise.allSettled(methodPromises) |
| 71 | + .catch((error) => reject(error)) |
| 72 | + .finally(() => { |
| 73 | + exportGeneratedFiles() |
| 74 | + kia.succeed(`All tests completed on ${browser}`) |
| 75 | + builder.quit() |
| 76 | + }) |
| 77 | + }) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +export default driver |
0 commit comments