fixtures.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { test as base } 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. base.beforeAll(async () => {
  7. if (electronApp) {
  8. return ;
  9. }
  10. electronApp = await electron.launch({
  11. cwd: "./static",
  12. args: ["electron.js"],
  13. })
  14. context = electronApp.context()
  15. await context.tracing.start({ screenshots: true, snapshots: true });
  16. // NOTE: The following ensures App first start with the correct path.
  17. const appPath = await electronApp.evaluate(async ({ app }) => {
  18. return app.getAppPath()
  19. })
  20. console.log("Test start with AppPath:", appPath)
  21. page = await electronApp.firstWindow()
  22. await page.waitForLoadState('domcontentloaded')
  23. await page.waitForFunction('window.document.title != "Loading"')
  24. await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')
  25. page.once('load', async () => {
  26. console.log('Page loaded!')
  27. await page.screenshot({ path: 'startup.png' })
  28. })
  29. })
  30. base.beforeEach(async () => {
  31. // discard any dialog by ESC
  32. if (page) {
  33. await page.keyboard.press('Escape')
  34. await page.keyboard.press('Escape')
  35. }
  36. })
  37. base.afterAll(async () => {
  38. // if (electronApp) {
  39. // await electronApp.close()
  40. //}
  41. })
  42. // hijack electron app into the test context
  43. export const test = base.extend<{ page: Page, context: BrowserContext, app: ElectronApplication }>({
  44. page: async ({ }, use) => {
  45. await use(page);
  46. },
  47. context: async ({ }, use) => {
  48. await use(context);
  49. },
  50. app: async ({ }, use) => {
  51. await use(electronApp);
  52. }
  53. });