/ tests / fixtures.mts
fixtures.mts
  1  import * as base from '@playwright/test'
  2  import { _electron as electron, Page, ElectronApplication } from 'playwright'
  3  import { join } from 'path'
  4  import { main } from '../package.json'
  5  import TestUtil from './testUtil.mjs'
  6  
  7  interface CustomFixtures {
  8    util: TestUtil
  9  }
 10  
 11  let appElectron: ElectronApplication
 12  let page: Page
 13  
 14  const __cwd = process.cwd()
 15  const __isCiProcess = process.env.CI === 'true'
 16  const __testPath = join(__cwd, 'tests')
 17  const __testResultPath = join(__testPath, 'results')
 18  const __testScreenshotPath = join(__testResultPath, 'screenshots')
 19  
 20  export const beforeAll = async () => {
 21    // Open Electron app from build directory
 22    appElectron = await electron.launch({
 23      args: [
 24        main,
 25        ...(__isCiProcess ? ['--no-sandbox'] : []),
 26        '--enable-logging',
 27        '--ignore-certificate-errors',
 28        '--ignore-ssl-errors',
 29        '--ignore-blocklist',
 30        '--ignore-gpu-blocklist'
 31      ],
 32      locale: 'en-US',
 33      colorScheme: 'light',
 34      env: {
 35        ...process.env,
 36        NODE_ENV: 'production'
 37      }
 38    })
 39    const splashWindow = await appElectron.firstWindow({
 40      timeout: 60000
 41    })
 42  
 43    const secondWindow = await appElectron.waitForEvent('window', {
 44      predicate: (window) => window !== splashWindow,
 45      timeout: 60000
 46    })
 47  
 48    page = secondWindow
 49  
 50    await page.waitForEvent('load')
 51  
 52    page.on('console', console.log)
 53    page.on('pageerror', console.log)
 54  
 55    const windows = await appElectron.windows()
 56    console.log(windows)
 57  
 58    const evaluateResult = await appElectron.evaluate(async ({ app, BrowserWindow }) => {
 59      try {
 60        const currentWindow = BrowserWindow.getFocusedWindow()
 61        if (!currentWindow) {
 62          throw new Error('No focused window found')
 63        }
 64  
 65        // Fix window position for testing
 66        currentWindow.setPosition(50, 50)
 67        currentWindow.setSize(1080, 560)
 68  
 69        return {
 70          packaged: app.isPackaged,
 71          dataPath: app.getPath('userData')
 72        }
 73      } catch (error) {
 74        console.error('Error in evaluate callback:', error)
 75        throw error
 76      }
 77    })
 78  
 79    base.expect(evaluateResult.packaged, 'app is not packaged').toBe(false)
 80  }
 81  
 82  export const afterAll = async () => {
 83    await appElectron.close()
 84  }
 85  
 86  export const test = base.test.extend<CustomFixtures>({
 87    page: async ({}, use) => {
 88      await use(page)
 89    },
 90    util: async ({ page }, use, testInfo) => {
 91      await use(new TestUtil(page, testInfo, __testScreenshotPath))
 92    }
 93  })
 94  
 95  export const expect = base.expect
 96  
 97  export default {
 98    test,
 99    expect,
100    beforeAll,
101    afterAll
102  }