fixtures.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import * as fs from 'fs'
  2. import * as 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. let testTmpDir = path.resolve(__dirname, '../tmp')
  11. if (fs.existsSync(testTmpDir)) {
  12. fs.rmdirSync(testTmpDir, { recursive: true })
  13. }
  14. export let graphDir = path.resolve(testTmpDir, "e2e-test", repoName)
  15. // NOTE: This is a console log watcher for error logs.
  16. const consoleLogWatcher = (msg: ConsoleMessage) => {
  17. // console.log(msg.text())
  18. let msgText = msg.text()
  19. expect(msgText).not.toMatch(/^Failed to/)
  20. // youtube video
  21. if (!msgText.match(/^Error with Permissions-Policy header: Unrecognized feature/)) {
  22. expect(msgText).not.toMatch(/^Error/)
  23. }
  24. expect(msgText).not.toMatch(/^Uncaught/)
  25. // NOTE: React warnings will be logged as error.
  26. // expect(msg.type()).not.toBe('error')
  27. }
  28. base.beforeAll(async () => {
  29. if (electronApp) {
  30. return
  31. }
  32. fs.mkdirSync(graphDir, {
  33. recursive: true,
  34. });
  35. electronApp = await electron.launch({
  36. cwd: "./static",
  37. args: ["electron.js"],
  38. locale: 'en',
  39. })
  40. context = electronApp.context()
  41. await context.tracing.start({ screenshots: true, snapshots: true });
  42. // NOTE: The following ensures App first start with the correct path.
  43. const info = await electronApp.evaluate(async ({ app }) => {
  44. return {
  45. "appPath": app.getAppPath(),
  46. "appData": app.getPath("appData"),
  47. "userData": app.getPath("userData"),
  48. "appName": app.getName(),
  49. }
  50. })
  51. console.log("Test start with:", info)
  52. page = await electronApp.firstWindow()
  53. // Direct Electron console to watcher
  54. page.on('console', consoleLogWatcher)
  55. page.on('crash', () => {
  56. expect('page must not crash!').toBe('page crashed')
  57. })
  58. page.on('pageerror', (err) => {
  59. console.error("[pageerror]", err)
  60. expect('page must not have errors!').toBe('page has some error')
  61. })
  62. await page.waitForLoadState('domcontentloaded')
  63. await page.waitForFunction('window.document.title != "Loading"')
  64. // NOTE: The following ensures first start.
  65. // await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')
  66. await page.waitForSelector(':has-text("Loading")', {
  67. state: "hidden",
  68. timeout: 1000 * 15,
  69. });
  70. page.once('load', async () => {
  71. console.log('Page loaded!')
  72. await page.screenshot({ path: 'startup.png' })
  73. })
  74. await loadLocalGraph(page, graphDir);
  75. })
  76. base.beforeEach(async () => {
  77. // discard any dialog by ESC
  78. if (page) {
  79. await page.keyboard.press('Escape')
  80. await page.keyboard.press('Escape')
  81. }
  82. })
  83. base.afterAll(async () => {
  84. // if (electronApp) {
  85. // await electronApp.close()
  86. //}
  87. })
  88. // hijack electron app into the test context
  89. export const test = base.extend<{ page: Page, context: BrowserContext, app: ElectronApplication }>({
  90. page: async ({ }, use) => {
  91. await use(page);
  92. },
  93. context: async ({ }, use) => {
  94. await use(context);
  95. },
  96. app: async ({ }, use) => {
  97. await use(electronApp);
  98. }
  99. });