fixtures.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { test as base, expect, ConsoleMessage } from '@playwright/test';
  4. import { ElectronApplication, Page, BrowserContext, _electron as electron } from 'playwright'
  5. import { loadLocalGraph, randomString } from './utils';
  6. let electronApp: ElectronApplication
  7. let context: BrowserContext
  8. let page: Page
  9. let repoName = randomString(10)
  10. export let graphDir = path.resolve(__dirname, '../tmp/e2e-graph', repoName)
  11. // NOTE: This is a console log watcher for error logs.
  12. const consoleLogWatcher = (msg: ConsoleMessage) => {
  13. expect(msg.text()).not.toMatch(/^Failed to/)
  14. expect(msg.text()).not.toMatch(/^Error/)
  15. expect(msg.text()).not.toMatch(/^Uncaught/)
  16. // NOTE: React warnings will be logged as error.
  17. // expect(msg.type()).not.toBe('error')
  18. }
  19. base.beforeAll(async () => {
  20. if (electronApp) {
  21. return
  22. }
  23. fs.mkdirSync(graphDir, {
  24. recursive: true,
  25. });
  26. electronApp = await electron.launch({
  27. cwd: "./static",
  28. args: ["electron.js"],
  29. locale: 'en',
  30. })
  31. context = electronApp.context()
  32. await context.tracing.start({ screenshots: true, snapshots: true });
  33. // NOTE: The following ensures App first start with the correct path.
  34. const appPath = await electronApp.evaluate(async ({ app }) => {
  35. return app.getAppPath()
  36. })
  37. console.log("Test start with AppPath:", appPath)
  38. page = await electronApp.firstWindow()
  39. // Direct Electron console to watcher
  40. page.on('console', consoleLogWatcher)
  41. page.on('crash', () => {
  42. expect('page must not crash!').toBe('page crashed')
  43. })
  44. page.on('pageerror', (err) => {
  45. console.log(err)
  46. expect('page must not have errors!').toBe('page has some error')
  47. })
  48. await page.waitForLoadState('domcontentloaded')
  49. await page.waitForFunction('window.document.title != "Loading"')
  50. await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')
  51. page.once('load', async () => {
  52. console.log('Page loaded!')
  53. await page.screenshot({ path: 'startup.png' })
  54. })
  55. await loadLocalGraph(page, graphDir);
  56. })
  57. base.beforeEach(async () => {
  58. // discard any dialog by ESC
  59. if (page) {
  60. await page.keyboard.press('Escape')
  61. await page.keyboard.press('Escape')
  62. }
  63. })
  64. base.afterAll(async () => {
  65. // if (electronApp) {
  66. // await electronApp.close()
  67. //}
  68. })
  69. // hijack electron app into the test context
  70. export const test = base.extend<{ page: Page, context: BrowserContext, app: ElectronApplication }>({
  71. page: async ({ }, use) => {
  72. await use(page);
  73. },
  74. context: async ({ }, use) => {
  75. await use(context);
  76. },
  77. app: async ({ }, use) => {
  78. await use(electronApp);
  79. }
  80. });