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: use passed or failed for case status #64

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 31 additions & 1 deletion examples/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,37 @@ driver({ browser: 'chrome' }).then(({ service }) => {
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Todo App',
except: 'Drowser',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowser',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowsers',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowser',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowsers',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowsers',
},
{
method: 'getTitle',
operator: 'assertEquals',
except: 'Drowser',
},
]
}).catch((error) => {
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const seleniumExceptions: Record<string, string> = {
}

const caseStatus: Record<string, string> = {
ok: 'ok',
ko: 'ko',
passed: 'passed',
failed: 'failed',
}

export { caseStatus, driverBrowserList, driverBrowsers, seleniumExceptions }
21 changes: 17 additions & 4 deletions src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,23 @@ const driver = async (
)
const methodPromises: Promise<void>[] = []
const result = (
{ id, name, actual, exceptation, status }: TDataResult,
{ id, name, actual, exceptation, status, duration, timestamp }:
TDataResult,
) => {
return {
id,
name,
actual,
exceptation,
status,
timestamp: new Date(),
timestamp,
duration,
}
}

service.cases.forEach((c: TDrowserServiceCase) => {
const start = performance.now()

if (typeof c === 'object') {
const method =
(builder as unknown as Record<string, Function>)[c.method]
Expand All @@ -110,24 +114,33 @@ const driver = async (
const assertFunction = assert[c.operator] as TAssertFunction
actualValue = v
assertFunction(actualValue, c.except)

const end = performance.now()

data.results.push(
result({
id: nanoid(),
name: c.method,
actual: actualValue,
exceptation: c.except,
status: caseStatus.ok,
status: caseStatus.passed,
timestamp: new Date(),
duration: end - start,
}),
)
})
.catch(() => {
const end = performance.now()

data.results.push(
result({
id: nanoid(),
name: c.method,
actual: actualValue,
exceptation: c.except,
status: caseStatus.ko,
status: caseStatus.failed,
timestamp: new Date(),
duration: end - start,
}),
)
})
Expand Down
5 changes: 4 additions & 1 deletion src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
writeJson,
writeJsonSync,
} from '@deps'
import { generateFileName } from '@pkg/utils.ts'
import { generateFileName, humanizeDuration } from '@pkg/utils.ts'
import { TDataResult, TJSON } from '@pkg/types.ts'

const exportGeneratedLog = (
Expand Down Expand Up @@ -96,10 +96,13 @@ const exportJSONReport = (

if (Array.isArray(results) && results.length > 0) {
const jsonData = readJsonSync(filePath) as TJSON
const totalDuration = results.reduce((sum, r) => sum + r.duration, 0)
const averageDuration = totalDuration / results.length

jsonData.drowser.cases.push({
id: nanoid(),
time: new Date().toISOString(),
avg_duration: humanizeDuration(averageDuration),
cases: results,
})

Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export type TDataResult = {
actual: unknown
exceptation: unknown
status: string
timestamp?: Date
timestamp: Date
duration: number
}

export type TData = {
Expand Down Expand Up @@ -71,6 +72,7 @@ export type TJSON = {
{
id: string
time: string
avg_duration: string
cases: Array<TDataResult>
},
]
Expand Down
22 changes: 21 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,24 @@ const generateFileName = (prefix: string, ext: 'log' | 'pdf'): string => {
return `${prefix}_${timestamp}.${ext}`
}

export { generateFileName, getTimestamp, isValidHttpUrl }
const humanizeDuration = (durationMs: number): string => {
const secondsTotal = Math.round(durationMs / 1000)
const seconds = secondsTotal % 60
const minutesTotal = Math.floor(secondsTotal / 60)
const minutes = minutesTotal % 60
const hoursTotal = Math.floor(minutesTotal / 60)
const hours = hoursTotal % 24
const days = Math.floor(hoursTotal / 24)

let humanized = ''
if (days > 0) humanized += `${days}d `
if (hours > 0) humanized += `${hours}h `
if (minutes > 0) humanized += `${minutes}m `
if (seconds > 0 || (days === 0 && hours === 0 && minutes === 0)) {
humanized += `${seconds}s`
}

return humanized.trim()
}

export { generateFileName, getTimestamp, humanizeDuration, isValidHttpUrl }
Loading