Created
April 12, 2024 17:14
-
-
Save AlexcastroDev/bc7b5321bcf98fc0500db72f9a227d9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Browser, Page } from '@playwright/test' | |
import path from 'node:path'; | |
interface IBaseTestController { | |
browser?: Browser | |
page: Page | |
baseURL: string | |
path?: string | |
har: { | |
title: string | |
skip: boolean | |
titlePath: string[] | |
} | |
} | |
export default class BaseTestController implements IBaseTestController { | |
browser: Browser | |
page: Page | |
baseURL: string = process.env.BASE_URL | |
path?: string | |
har = { | |
title: '', | |
skip: false, | |
titlePath: [] | |
} | |
constructor({ browser, page, baseURL, path, har }: IBaseTestController) { | |
this.page = page | |
this.baseURL = baseURL | |
if (browser) { | |
this.browser = browser | |
} | |
if (path) { | |
this.path = path | |
} | |
this.har = har | |
const { title, skip } = har | |
!skip && page.routeFromHAR(this.getTestPath(title, path), this.getTestOptions()); | |
} | |
generateHarName() { | |
const [specName, ...parts] = this.har.titlePath; | |
return path.join(__dirname, `${specName.replace(/\.(test|spec)\.ts/, '-hars')}`, parts.join('-').replace(/ /g, '-'), 'har.har'); | |
} | |
goToPage = async () => { | |
await this.page.goto(`${this.baseURL}${this.path}`) | |
} | |
sleep = async (ms: number) => { | |
return new Promise((resolve) => setTimeout(resolve, ms)) | |
} | |
getTestPath(input: string, group: string): string { | |
const words = input.match(/[A-Za-z][a-z]*|[A-Z][a-z]*/g) || []; | |
const namespace = words.map(word => word.toLowerCase()).join('_') | |
return `./hars/${group}/${namespace}/${namespace}.har` | |
} | |
getTestOptions() { | |
return { | |
url: new RegExp(/(herokuapp|imagedelivery)/), | |
update: Boolean(process.env.PW_UPDATE_HAR), | |
updateContent: 'embed' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment