logseq/e2e-tests/fixtures.ts

125 lines
3.4 KiB
TypeScript
Raw Normal View History

import * as fs from 'fs'
import * as path from 'path'
2021-12-24 06:49:02 +00:00
import { test as base, expect, ConsoleMessage } from '@playwright/test';
2021-11-29 07:15:45 +00:00
import { ElectronApplication, Page, BrowserContext, _electron as electron } from 'playwright'
import { loadLocalGraph, randomString } from './utils';
2021-11-29 07:15:45 +00:00
let electronApp: ElectronApplication
let context: BrowserContext
let page: Page
let repoName = randomString(10)
let testTmpDir = path.resolve(__dirname, '../tmp')
if (fs.existsSync(testTmpDir)) {
fs.rmdirSync(testTmpDir, { recursive: true })
}
export let graphDir = path.resolve(testTmpDir, "e2e-test", repoName)
2021-11-29 07:15:45 +00:00
// NOTE: This following is a console log watcher for error logs.
// Save and print all logs when error happens.
let logs: string
2021-12-24 06:49:02 +00:00
const consoleLogWatcher = (msg: ConsoleMessage) => {
// console.log(msg.text())
logs += msg.text() + '\n'
expect(msg.text(), logs).not.toMatch(/^(Failed to|Uncaught)/)
// youtube video
if (!logs.match(/^Error with Permissions-Policy header: Unrecognized feature/)) {
expect(logs).not.toMatch(/^Error/)
}
2021-12-24 06:49:02 +00:00
// NOTE: React warnings will be logged as error.
// expect(msg.type()).not.toBe('error')
}
2021-11-29 07:15:45 +00:00
base.beforeAll(async () => {
if (electronApp) {
2021-12-24 06:49:02 +00:00
return
2021-11-29 07:15:45 +00:00
}
console.log(`Creating test graph directory: ${graphDir}`)
fs.mkdirSync(graphDir, {
recursive: true,
});
2021-11-29 07:15:45 +00:00
electronApp = await electron.launch({
cwd: "./static",
args: ["electron.js"],
locale: 'en',
2021-11-29 07:15:45 +00:00
})
context = electronApp.context()
await context.tracing.start({ screenshots: true, snapshots: true });
// NOTE: The following ensures App first start with the correct path.
const info = await electronApp.evaluate(async ({ app }) => {
return {
"appPath": app.getAppPath(),
"appData": app.getPath("appData"),
"userData": app.getPath("userData"),
"appName": app.getName(),
}
2021-11-29 07:15:45 +00:00
})
console.log("Test start with:", info)
2021-11-29 07:15:45 +00:00
page = await electronApp.firstWindow()
2021-12-24 06:49:02 +00:00
// Direct Electron console to watcher
page.on('console', consoleLogWatcher)
page.on('crash', () => {
expect(false, "Page must not crash").toBeTruthy()
2021-12-24 06:49:02 +00:00
})
page.on('pageerror', (err) => {
console.log(err)
expect(false, 'Page must not have errors!').toBeTruthy()
2021-12-24 06:49:02 +00:00
})
2021-11-29 07:15:45 +00:00
await page.waitForLoadState('domcontentloaded')
// await page.waitForFunction(() => window.document.title != "Loading")
// NOTE: The following ensures first start.
// await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')
2021-11-29 07:15:45 +00:00
2022-01-17 05:48:32 +00:00
await page.waitForSelector(':has-text("Loading")', {
state: "hidden",
timeout: 1000 * 15,
});
2021-11-29 07:15:45 +00:00
page.once('load', async () => {
console.log('Page loaded!')
await page.screenshot({ path: 'startup.png' })
})
await loadLocalGraph(page, graphDir);
2021-11-29 07:15:45 +00:00
})
base.beforeEach(async () => {
// discard any dialog by ESC
if (page) {
await page.keyboard.press('Escape')
await page.keyboard.press('Escape')
}
})
base.afterAll(async () => {
// if (electronApp) {
// await electronApp.close()
//}
})
// hijack electron app into the test context
export const test = base.extend<{ page: Page, context: BrowserContext, app: ElectronApplication, graphDir: string }>({
2021-11-29 07:15:45 +00:00
page: async ({ }, use) => {
await use(page);
},
context: async ({ }, use) => {
await use(context);
},
app: async ({ }, use) => {
await use(electronApp);
},
graphDir: async ({ }, use) => {
await use(graphDir);
},
2021-11-29 07:15:45 +00:00
});