(options: BrowserTestOptions = {})
| 11 | } |
| 12 | |
| 13 | export async function executeBrowserTest(options: BrowserTestOptions = {}) { |
| 14 | let url = options.baseUrl; |
| 15 | let hasStartedServer = false; |
| 16 | |
| 17 | try { |
| 18 | if (!url) { |
| 19 | // Start serving and find address (1 - Webpack; 2 - Vite) |
| 20 | const match = /(?:open your browser on|Local:)\s+(http:\/\/localhost:\d+\/)/; |
| 21 | const serveArgs = ['serve', '--port=0', '--no-watch', '--no-live-reload']; |
| 22 | if (options.project) { |
| 23 | serveArgs.push(options.project); |
| 24 | } |
| 25 | if (options.configuration) { |
| 26 | serveArgs.push(`--configuration=${options.configuration}`); |
| 27 | } |
| 28 | |
| 29 | const { stdout } = await execAndWaitForOutputToMatch('ng', serveArgs, match, { |
| 30 | ...process.env, |
| 31 | 'NO_COLOR': '1', |
| 32 | }); |
| 33 | url = stripVTControlCharacters(stdout).match(match)?.[1]; |
| 34 | if (!url) { |
| 35 | throw new Error('Could not find serving URL'); |
| 36 | } |
| 37 | hasStartedServer = true; |
| 38 | } |
| 39 | |
| 40 | const browser = await launch({ |
| 41 | executablePath: process.env['CHROME_BIN'], |
| 42 | headless: true, |
| 43 | args: ['--no-sandbox'], |
| 44 | }); |
| 45 | try { |
| 46 | const page = await browser.newPage(); |
| 47 | |
| 48 | // Capture errors |
| 49 | const errors: string[] = []; |
| 50 | page.on('console', (msg) => { |
| 51 | if (msg.type() === 'error') { |
| 52 | errors.push(msg.text()); |
| 53 | } |
| 54 | }); |
| 55 | page.on('pageerror', (err) => { |
| 56 | const error = `${err}`; |
| 57 | if (!error.includes('Hot Module Replacement is disabled')) { |
| 58 | errors.push(error); |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | await page.goto(url); |
| 63 | |
| 64 | if (options.checkFn) { |
| 65 | await options.checkFn(page); |
| 66 | } else { |
| 67 | // Default check: verify h1 content and no browser errors |
| 68 | const expectedText = options.expectedTitleText || 'Hello, test-project'; |
| 69 | |
| 70 | // Wait for the h1 element to appear and contain the expected text |
no test coverage detected