Skip to content
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

feat: improve types export and name #113

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import driver from './src/driver.ts'
import types from './src/types.ts'
import type Types from './src/types.ts'

export { driver, types }
export { driver }
export type { Types }
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const driverBrowsers: Record<string, string> = {
const seleniumExceptions: Record<string, string> = {
WebDriverError: 'General WebDriver error.',
NoSuchElementError: 'The requested element could not be found in the DOM.',
TimeoutError: 'The operation did not complete within the specified timeout.',
TimeoutError:
'The operation did not complete within the specified timeout.',
SessionNotCreatedError: 'A new session could not be created.',
ScriptTimeoutError: 'A script execution timeout occurred.',
StaleElementReferenceError:
Expand Down
40 changes: 22 additions & 18 deletions src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { assert, Builder, By, isEmpty, join, Kia } from '../deps.ts'
import type {
TCaseFn,
TConfigJSON,
TData,
TDriverParams,
TDriverServiceCaseParamsBuilder,
TDrowserDriverResponse,
TDrowserServiceCase,
TDrowserThenableWebDriver,
CaseFn,
ConfigJSON,
Data,
DriverParams,
DriverServiceCaseParamsBuilder,
DrowserDriverResponse,
DrowserServiceCase,
DrowserThenableWebDriver,
} from './types.ts'
import { isValidHttpUrl, result as resultData } from './utils.ts'
import {
Expand All @@ -20,13 +20,13 @@ import { exportGeneratedLog, exportJSONReport } from './export.ts'

const driver = async ({
browser,
}: TDriverParams): Promise<TDrowserDriverResponse> => {
const data: TData = { url: '', results: [] }
}: DriverParams): Promise<DrowserDriverResponse> => {
const data: Data = { url: '', results: [] }
const configPath = join(Deno.cwd(), 'drowser.json')

try {
await Deno.stat(configPath)
const { url }: TConfigJSON = JSON.parse(
const { url }: ConfigJSON = JSON.parse(
await Deno.readTextFile(configPath),
)

Expand All @@ -39,7 +39,9 @@ const driver = async ({
data.url = url
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
throw new Error('An error occurred, please create drowser.json file.')
throw new Error(
'An error occurred, please create drowser.json file.',
)
}

if (!(error instanceof Deno.errors.NotFound)) {
Expand All @@ -50,15 +52,17 @@ const driver = async ({
}

if (isEmpty(browser) || !driverBrowserList.includes(browser)) {
throw new Error('An error occurred, please provide a valid browser driver')
throw new Error(
'An error occurred, please provide a valid browser driver',
)
}

return new Promise<TDrowserDriverResponse>((resolve, reject) => {
return new Promise<DrowserDriverResponse>((resolve, reject) => {
if (isEmpty(data.url) || !isValidHttpUrl({ url: data.url })) reject()

const builder = new Builder()
.forBrowser(driverBrowsers[browser])
.build() as TDrowserThenableWebDriver
.build() as DrowserThenableWebDriver

const service = { cases: [] }

Expand All @@ -75,16 +79,16 @@ const driver = async ({
.finally(() => {
const methodPromises: Promise<void>[] = []

service.cases.forEach((c: TDrowserServiceCase) => {
service.cases.forEach((c: DrowserServiceCase) => {
if (typeof c === 'object') {
const omitedBuilder =
builder as unknown as TDriverServiceCaseParamsBuilder
builder as unknown as DriverServiceCaseParamsBuilder
const megaBuilder = {
builder: omitedBuilder,
assert,
by: By,
}
const method = c.fn as TCaseFn
const method = c.fn as CaseFn
const methodPromise = method(megaBuilder)

const start = performance.now()
Expand Down
50 changes: 27 additions & 23 deletions src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import {
} from './utils.ts'
import {
DataPoint,
DataResult,
DriverBrowser,
MonthCount,
MonthValue,
TDataResult,
TDriverBrowser,
TJSON,
ReportSchema,
} from './types.ts'

const exportGeneratedLog = (
{ results }: { results: Array<TDataResult> },
{ results }: { results: Array<DataResult> },
): void => {
const dirPath = join(Deno.cwd(), 'drowser/logs')
const hasDir = existsSync(dirPath)
Expand All @@ -40,7 +40,8 @@ const exportGeneratedLog = (

const writeResult = () =>
results.forEach((r) => {
const logRow = `[${r.timestamp}] - Test with ${r.name} is ${r.status}`
const logRow =
`[${r.timestamp}] - Test with ${r.name} is ${r.status}`
Deno.writeTextFile(logFilePath, `${logRow}\n`, { append: true })
})

Expand All @@ -55,8 +56,8 @@ const exportGeneratedLog = (

const exportJSONReport = (
{ results, browser }: {
results: Array<TDataResult>
browser: TDriverBrowser
results: Array<DataResult>
browser: DriverBrowser
},
): void => {
const filePath = join(Deno.cwd(), 'drowser-reports.json')
Expand All @@ -73,7 +74,7 @@ const exportJSONReport = (
}

if (Array.isArray(results) && results.length > 0) {
const jsonData = readJsonSync(filePath) as TJSON
const jsonData = readJsonSync(filePath) as ReportSchema

const month = getCurrentMonth({ type: 'short' })

Expand All @@ -83,11 +84,12 @@ const exportJSONReport = (
}
}

const flatedTotalTests = jsonData.drowser.metadata.current_month === month
? jsonData.drowser.cases.flatMap((item) => item.cases).filter((c) =>
c.month_of_test === month
)
: []
const flatedTotalTests =
jsonData.drowser.metadata.current_month === month
? jsonData.drowser.cases.flatMap((item) => item.cases).filter((
c,
) => c.month_of_test === month)
: []
const totalTests = [
...flatedTotalTests,
...results,
Expand Down Expand Up @@ -120,7 +122,9 @@ const exportJSONReport = (
({ status }: { status: string }) => status,
)

const CombinedTestCoverage = getCoverage({ results: CombinedTotalTests })
const CombinedTestCoverage = getCoverage({
results: CombinedTotalTests,
})

const CombinedAvgTestDuration = getAverageDuration({
results: CombinedTotalTests,
Expand Down Expand Up @@ -149,15 +153,15 @@ const exportJSONReport = (
.data as DataPoint[] ?? [],
},
],
passing_tests:
jsonData?.drowser?.metrics?.graphs?.passing_tests as MonthCount[] ??
[],
failed_tests:
jsonData?.drowser?.metrics?.graphs?.failed_tests as MonthCount[] ??
[],
test_coverage:
jsonData?.drowser?.metrics?.graphs?.test_coverage as MonthCount[] ??
[],
passing_tests: jsonData?.drowser?.metrics?.graphs
?.passing_tests as MonthCount[] ??
[],
failed_tests: jsonData?.drowser?.metrics?.graphs
?.failed_tests as MonthCount[] ??
[],
test_coverage: jsonData?.drowser?.metrics?.graphs
?.test_coverage as MonthCount[] ??
[],
avg_test_duration: jsonData?.drowser?.metrics?.graphs
?.avg_test_duration as MonthCount[] ?? [],
flaky_tests: jsonData?.drowser?.metrics?.graphs
Expand Down
101 changes: 51 additions & 50 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
import { assert } from '../deps.ts'
import type { By, ThenableWebDriver } from '../deps.ts'

export type TDriverParams = {
browser: TDriverBrowser
export type DriverParams = {
browser: DriverBrowser
}

export type TDriverBrowser = 'chrome' | 'firefox' | 'safari' | 'edge'
export type DriverBrowser = 'chrome' | 'firefox' | 'safari' | 'edge'

export type TConfigJSON = {
export type ConfigJSON = {
url: string
exportPdf: boolean
}

export type TDataResult = {
export type DataResult = {
id?: string
name: string
status: string
timestamp?: Date
duration: number
month_of_test?: string
browser: TDriverBrowser
browser: DriverBrowser
}

export type TData = {
export type Data = {
url: string
results: Array<TDataResult>
results: Array<DataResult>
}

export type TDrowserThenableWebDriver = ThenableWebDriver
export type DrowserThenableWebDriver = ThenableWebDriver

export type TDrowserBuilder = Omit<
export type DrowserBuilder = Omit<
ThenableWebDriver,
'get'
>

export type TDriverServiceCaseParamsBuilder = Omit<
export type DriverServiceCaseParamsBuilder = Omit<
ThenableWebDriver,
'get' | 'quit' | 'then' | 'catch' | 'close' | 'finally'
>

export type TDriverServiceCaseParamsAssert = typeof assert
export type DriverServiceCaseParamsAssert = typeof assert

export type TDriverServiceCaseParamsBy = typeof By
export type DriverServiceCaseParamsBy = typeof By

export type TDriverBrowserCaseParams = {
builder: TDriverServiceCaseParamsBuilder
assert: TDriverServiceCaseParamsAssert
by: TDriverServiceCaseParamsBy
export type DriverBrowserCaseParams = {
builder: DriverServiceCaseParamsBuilder
assert: DriverServiceCaseParamsAssert
by: DriverServiceCaseParamsBy
}

export type TDrowserServiceCase = {
export type DrowserServiceCase = {
name: string
fn: (
params: TDriverBrowserCaseParams,
params: DriverBrowserCaseParams,
) => void
}

export type TDrowserService = {
cases: Array<TDrowserServiceCase>
export type DrowserService = {
cases: Array<DrowserServiceCase>
}

export type TCaseFn = (
params: TDriverBrowserCaseParams,
export type CaseFn = (
params: DriverBrowserCaseParams,
) => Promise<void>

export type TDrowserDriverResponse = {
service: TDrowserService
export type DrowserDriverResponse = {
service: DrowserService
}

export type TAssertFunction = (
export type AssertFunction = (
actual: unknown,
expected: unknown,
msg?: string,
) => void

export type TAssertError = {
export type AssertError = {
name: string
}

export type TIsValidHttpUrlParams = {
export type IsValidHttpUrlParams = {
url: string
}

Expand All @@ -102,7 +102,7 @@ export type MonthValue = {
value: number
}

export type TJSON = {
export type ReportSchema = {
drowser: {
metadata: {
current_month: string
Expand Down Expand Up @@ -131,35 +131,36 @@ export type TJSON = {
coverage: number
flaky: number
month_of_test: string
browser: TDriverBrowser
cases: Array<TDataResult>
browser: DriverBrowser
cases: Array<DataResult>
},
]
}
}

const types = {
TDriverParams: {} as TDriverParams,
TDriverBrowser: {} as TDriverBrowser,
TConfigJSON: {} as TConfigJSON,
TData: {} as TData,
TDrowserThenableWebDriver: {} as TDrowserThenableWebDriver,
TDrowserBuilder: {} as TDrowserBuilder,
TDriverServiceCaseParamsBuilder: {} as TDriverServiceCaseParamsBuilder,
TDriverServiceCaseParamsAssert: {} as TDriverServiceCaseParamsAssert,
TDriverServiceCaseParamsBy: {} as TDriverServiceCaseParamsBy,
TDriverBrowserCaseParams: {} as TDriverBrowserCaseParams,
TDrowserServiceCase: {} as TDrowserServiceCase,
TDrowserService: {} as TDrowserService,
TCaseFn: {} as TCaseFn,
TDrowserDriverResponse: {} as TDrowserDriverResponse,
TAssertFunction: {} as TAssertFunction,
TAssertError: {} as TAssertError,
TIsValidHttpUrlParams: {} as TIsValidHttpUrlParams,
const Types = {
TDriverParams: {} as DriverParams,
andostronaut marked this conversation as resolved.
Show resolved Hide resolved
DriverBrowser: {} as DriverBrowser,
ConfigJSON: {} as ConfigJSON,
Data: {} as Data,
DrowserThenableWebDriver: {} as DrowserThenableWebDriver,
DrowserBuilder: {} as DrowserBuilder,
DriverServiceCaseParamsBuilder: {} as DriverServiceCaseParamsBuilder,
DriverServiceCaseParamsAssert: {} as DriverServiceCaseParamsAssert,
DriverServiceCaseParamsBy: {} as DriverServiceCaseParamsBy,
DriverBrowserCaseParams: {} as DriverBrowserCaseParams,
DrowserServiceCase: {} as DrowserServiceCase,
DrowserService: {} as DrowserService,
CaseFn: {} as CaseFn,
DrowserDriverResponse: {} as DrowserDriverResponse,
AssertFunction: {} as AssertFunction,
AssertError: {} as AssertError,
IsValidHttpUrlParams: {} as IsValidHttpUrlParams,
DataPoint: {} as DataPoint,
DataSet: {} as DataSet,
MonthCount: {} as MonthCount,
MonthValue: {} as MonthValue,
ReportSchema: {} as ReportSchema,
}

export default types
export default Types
Loading