Skip to content

Commit

Permalink
Merge pull request #64 from iamando/develop
Browse files Browse the repository at this point in the history
feat: use passed or failed for case status
  • Loading branch information
Ando authored May 17, 2024
2 parents 4fc720f + 4f03dee commit cc90469
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
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 }

0 comments on commit cc90469

Please sign in to comment.