fixtures.ts 2.3 KB

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