fixtures.ts 3.1 KB

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