Skip to content

Commit

Permalink
Move chromium setup into runner module
Browse files Browse the repository at this point in the history
  • Loading branch information
jbranchaud committed Jun 20, 2023
1 parent da8938e commit 66570dc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 51 deletions.
32 changes: 12 additions & 20 deletions playwright-egghead.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { chromium as playwrightChromium } from 'playwright-core';
import {retry} from './src/retry'
import {runHealthChecks} from './src/runner'

const chromium = (() => {
const launch = async () => {
return playwrightChromium.launch({headless: true})
}

return { launch }
})()
import {runHealthChecks, Step} from './src/runner'

const baseUrl = 'https://egghead.io'

export const testEgghead = async ({ event, step }: {event: any; step: any}) => {
await step.run('Test Price Display', async () => {

export const testEgghead = async ({ event, step }: {event: any; step: Step}) => {
await step.run('Test Price Display', async ({ chromium }) => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
Expand Down Expand Up @@ -50,7 +40,7 @@ export const testEgghead = async ({ event, step }: {event: any; step: any}) => {
return { event, body };
})

await step.run('Test View First Video', async () => {
await step.run('Test View First Video', async ({ chromium }) => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
Expand Down Expand Up @@ -107,13 +97,15 @@ export const testEgghead = async ({ event, step }: {event: any; step: any}) => {
.filter({ hasText: "A Beginners Guide to React Introduction" })
.isVisible()

const videoVisible = await page.locator('video').isVisible()
await retry(async () => {
const videoVisible = await page.locator('video').isVisible()

if(videoVisible) {
body.videoTagIsVisible = true
} else {
throw new Error("Video tag not visible on first exercise of Beginner's Guide to React");
}
if(videoVisible) {
body.videoTagIsVisible = true
} else {
throw new Error("Video tag not visible on first exercise of Beginner's Guide to React");
}
}, options)

return { event, body }
})
Expand Down
19 changes: 5 additions & 14 deletions playwright-pro-tailwind.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { chromium as playwrightChromium } from 'playwright-core';
import {retry} from './src/retry'
import {runHealthChecks} from './src/runner'

const chromium = (() => {
const launch = async () => {
return playwrightChromium.launch({headless: true})
}

return { launch }
})()
import {runHealthChecks, Step} from './src/runner'

const baseUrl = 'https://protailwind.com'

Expand All @@ -17,8 +8,8 @@ function isValidMonetaryValue(value: string) {
return re.test(value);
}

export const testProTailwind = async ({ event, step }: {event: any; step: any}) => {
await step.run('Test Core CTAs', async () => {
export const testProTailwind = async ({ event, step }: {event: any; step: Step}) => {
await step.run('Test Core CTAs', async ({ chromium }) => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
Expand Down Expand Up @@ -56,7 +47,7 @@ export const testProTailwind = async ({ event, step }: {event: any; step: any})
return { event, body }
})

await step.run('Test Price Display', async () => {
await step.run('Test Price Display', async ({ chromium }) => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
Expand Down Expand Up @@ -109,7 +100,7 @@ export const testProTailwind = async ({ event, step }: {event: any; step: any})
return { event, body };
})

await step.run('Test View a Tutorial Video', async () => {
await step.run('Test View a Tutorial Video', async ({ chromium }) => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
Expand Down
17 changes: 4 additions & 13 deletions playwright-total-typescript.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { chromium as playwrightChromium } from 'playwright-core';
import {retry} from './src/retry'
import {runHealthChecks} from './src/runner'

const chromium = (() => {
const launch = async () => {
return playwrightChromium.launch({headless: true})
}

return { launch }
})()
import {runHealthChecks, Step} from './src/runner'

const baseUrl = 'https://totaltypescript.com'

Expand All @@ -17,8 +8,8 @@ function isValidMonetaryValue(value: string) {
return re.test(value);
}

export const testTotalTypeScript = async ({ event, step }: {event: any; step: any}) => {
await step.run('Test Price Display', async () => {
export const testTotalTypeScript = async ({ event, step }: {event: any; step: Step}) => {
await step.run('Test Price Display', async ({ chromium }) => {

const browser = await chromium.launch();
const context = await browser.newContext();
Expand All @@ -42,7 +33,7 @@ export const testTotalTypeScript = async ({ event, step }: {event: any; step: an
}
})

await step.run('Test View First Video', async () => {
await step.run('Test View First Video', async ({ chromium }) => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
Expand Down
26 changes: 22 additions & 4 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import { chromium as playwrightChromium } from 'playwright-core';

const chromium = (() => {
const launch = async () => {
return playwrightChromium.launch({headless: true})
}

return { launch }
})()

type Chromium = typeof chromium
type TestFunction = ({ chromium }: { chromium: Chromium }) => Promise<any>
type CheckFunction = ({event, step}: { event: any; step: any}) => Promise<void>

type RunFunction = (testDescription: string, testFunction: TestFunction) => Promise<void>
export type Step = {
run: RunFunction;
xrun: RunFunction;
}

export const runHealthChecks = async (checksFn: CheckFunction) => {
const event = { time: new Date() }
const run = async (testDescription: string, testFunction: () => Promise<any>) => {
const run = async (testDescription: string, testFunction: TestFunction) => {
console.log(testDescription);
const result = await testFunction()
const result = await testFunction({ chromium })
console.log(result)
}
const xrun = async (testDescription: string, _testFunction: () => Promise<any>) => {
const xrun = async (testDescription: string, _testFunction: TestFunction) => {
console.log(`Skipping "${testDescription}"`)
}
const step = {
const step: Step = {
run,
xrun
}
Expand Down

0 comments on commit 66570dc

Please sign in to comment.