Skip to content

Commit

Permalink
feat: use promise all settled to handle non blocking on running process
Browse files Browse the repository at this point in the history
  • Loading branch information
Ando committed May 30, 2024
1 parent 9c139a4 commit 7a49e39
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 88 deletions.
58 changes: 40 additions & 18 deletions examples/basic.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
import { driver } from '../mod.ts'

driver({ browser: 'chrome' }).then(({ service }) => {
service.cases = [
async ({ builder, assert }) => {
service.case_name = 'Verify Title'
const title = await builder.getTitle()
assert.assertEquals(title, 'Drowser')
},
]
}).catch((error) => console.log(error))
driver({ browser: 'chrome' })
.then(({ service }) => {
service.cases = [
{
name: 'Verify Failed Title',
case: async ({ builder, assert }) => {
const title = await builder.getTitle()
assert.assertEquals(title, 'Drowsers')
},
},
{
name: 'Verify Title',
case: async ({ builder, assert }) => {
const title = await builder.getTitle()
assert.assertEquals(title, 'Drowser')
},
},
]
})
.catch((error) => console.log(error))

driver({ browser: 'firefox' }).then(({ service }) => {
service.cases = [
async ({ builder, assert }) => {
service.case_name = 'Verify Title'
const title = await builder.getTitle()
assert.assertEquals(title, 'Drowser')
},
]
}).catch((error) => console.log(error))
driver({ browser: 'firefox' })
.then(({ service }) => {
service.cases = [
{
name: 'Verify Failed Title',
case: async ({ builder, assert }) => {
const title = await builder.getTitle()
assert.assertEquals(title, 'Drowsers')
},
},
{
name: 'Verify Title',
case: async ({ builder, assert }) => {
const title = await builder.getTitle()
assert.assertEquals(title, 'Drowser')
},
},
]
})
.catch((error) => console.log(error))
87 changes: 37 additions & 50 deletions src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, Builder, By, isEmpty, join, Kia, nanoid } from '@deps'
import { assert, Builder, By, isEmpty, join, Kia } from '@deps'
import type {
TCaseFn,
TConfigJSON,
Expand All @@ -9,11 +9,7 @@ import type {
TDrowserServiceCase,
TDrowserThenableWebDriver,
} from '@pkg/types.ts'
import {
getCurrentMonth,
isValidHttpUrl,
result as resultData,
} from '@pkg/utils.ts'
import { isValidHttpUrl, result as resultData } from '@pkg/utils.ts'
import {
caseStatus,
driverBrowserList,
Expand All @@ -26,9 +22,9 @@ import {
exportJSONReport,
} from '@pkg/export.ts'

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

Expand All @@ -47,9 +43,7 @@ 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 @@ -60,27 +54,24 @@ 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) => {
if (isEmpty(data.url) || !isValidHttpUrl({ url: data.url })) reject()

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

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

const service = { case_name: null, cases: [] }
const service = { cases: [] }

const kia = new Kia('Processing your tests')
kia.start()

builder.get(data.url).then(() => resolve({ service }))
builder
.get(data.url)
.then(() => resolve({ service }))
.catch((err) => {
kia.fail('An error occurred while running tests')
reject(seleniumExceptions[err.name])
Expand All @@ -92,62 +83,58 @@ const driver = async (
const methodPromises: Promise<void>[] = []

service.cases.forEach((c: TDrowserServiceCase) => {
if (typeof c === 'function') {
if (typeof c === 'object') {
const omitedBuilder =
builder as unknown as TDriverServiceCaseParamsBuilder
const megaBuilder = {
builder: omitedBuilder,
assert,
by: By,
}
const method = c as TCaseFn
const method = c.case as TCaseFn
const methodPromise = method(megaBuilder)
methodPromises.push(methodPromise)
}
})

const exportGeneratedFiles = () => {
if (exportPdf) exportGeneratedPdf({ results: data.results })
exportGeneratedLog({ results: data.results })
exportJSONReport({
results: data.results,
browser,
})
}

Promise.allSettled(methodPromises)
.then((results) => {
const start = performance.now()
results.forEach((result) => {
if (result.status === 'fulfilled') {

methodPromise
.then(() => {
const end = performance.now()
data.results.push(
resultData({
id: nanoid(),
name: service.case_name,
name: c.name,
status: caseStatus.passed,
timestamp: new Date(),
duration: end - start,
month_of_test: month,
browser,
}),
)
} else {
})
.catch(() => {
const end = performance.now()
data.results.push(
resultData({
id: nanoid(),
name: service.case_name,
name: c.name,
status: caseStatus.failed,
timestamp: new Date(),
duration: end - start,
month_of_test: month,
browser,
}),
)
}
})
})

methodPromises.push(methodPromise)
}
})

const exportGeneratedFiles = () => {
if (exportPdf) exportGeneratedPdf({ results: data.results })
exportGeneratedLog({ results: data.results })
exportJSONReport({
results: data.results,
browser,
})
}

Promise.allSettled(methodPromises)
.catch((error) => reject(error))
.finally(() => {
exportGeneratedFiles()
Expand Down
25 changes: 11 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export type TConfigJSON = {
}

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

Expand Down Expand Up @@ -49,18 +49,15 @@ type TDriverBrowserCaseParams = {
by: TDriverServiceCaseParamsBy
}

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

export type TDrowserService = {
case_name: string | null
cases: Array<
| TDrowserServiceCase
| ((
params: TDriverBrowserCaseParams,
) => void)
>
cases: Array<TDrowserServiceCase>
}

export type TCaseFn = (
Expand Down
10 changes: 4 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { nanoid } from '@deps'
import { TDataResult, TIsValidHttpUrlParams } from '@pkg/types.ts'
import { caseStatus } from '@pkg/constants.ts'

Expand Down Expand Up @@ -115,22 +116,19 @@ const getCurrentMonth = ({ type = 'long' }: { type: 'long' | 'short' }) => {

const result = (
{
id,
name,
status,
duration,
timestamp,
month_of_test,
browser,
}: TDataResult,
) => {
return {
id,
id: nanoid(),
name,
status,
timestamp,
timestamp: new Date(),
duration,
month_of_test,
month_of_test: getCurrentMonth({ type: 'short' }),
browser,
}
}
Expand Down

0 comments on commit 7a49e39

Please sign in to comment.