2022-04-02 13:37:38 +00:00
|
|
|
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'
|
2022-01-05 13:05:05 +00:00
|
|
|
import { loadLocalGraph, randomString } from './utils';
|
2021-11-29 07:15:45 +00:00
|
|
|
|
|
|
|
let electronApp: ElectronApplication
|
|
|
|
let context: BrowserContext
|
|
|
|
let page: Page
|
2022-01-17 05:30:59 +00:00
|
|
|
|
2022-01-18 02:37:31 +00:00
|
|
|
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
|
|
|
|
2022-03-31 19:41:49 +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) => {
|
2022-03-31 19:41:49 +00:00
|
|
|
// console.log(msg.text())
|
|
|
|
logs += msg.text() + '\n'
|
|
|
|
expect(msg.text(), logs).not.toMatch(/^(Failed to|Uncaught)/)
|
2022-04-02 13:37:38 +00:00
|
|
|
|
|
|
|
// youtube video
|
2022-03-31 19:41:49 +00:00
|
|
|
if (!logs.match(/^Error with Permissions-Policy header: Unrecognized feature/)) {
|
|
|
|
expect(logs).not.toMatch(/^Error/)
|
2022-04-02 13:37:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-31 14:52:42 +00:00
|
|
|
console.log(`Creating test graph directory: ${graphDir}`)
|
2022-01-05 13:05:05 +00:00
|
|
|
fs.mkdirSync(graphDir, {
|
|
|
|
recursive: true,
|
|
|
|
});
|
|
|
|
|
2021-11-29 07:15:45 +00:00
|
|
|
electronApp = await electron.launch({
|
|
|
|
cwd: "./static",
|
|
|
|
args: ["electron.js"],
|
2022-01-05 13:05:05 +00:00
|
|
|
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.
|
2022-01-17 05:30:59 +00:00
|
|
|
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
|
|
|
})
|
2022-01-17 05:30:59 +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', () => {
|
2022-03-31 14:52:42 +00:00
|
|
|
expect(false, "Page must not crash").toBeTruthy()
|
2021-12-24 06:49:02 +00:00
|
|
|
})
|
|
|
|
page.on('pageerror', (err) => {
|
2022-03-31 14:52:42 +00:00
|
|
|
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')
|
2022-03-31 14:52:42 +00:00
|
|
|
// await page.waitForFunction(() => window.document.title != "Loading")
|
2022-01-17 05:30:59 +00:00
|
|
|
// 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' })
|
|
|
|
})
|
2022-01-05 13:05:05 +00:00
|
|
|
|
|
|
|
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
|
2022-03-31 14:52:42 +00:00
|
|
|
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);
|
2022-03-31 14:52:42 +00:00
|
|
|
},
|
|
|
|
graphDir: async ({ }, use) => {
|
|
|
|
await use(graphDir);
|
|
|
|
},
|
2021-11-29 07:15:45 +00:00
|
|
|
});
|