Files
modeling-app/e2e/playwright/zoo-test.ts

117 lines
4.3 KiB
TypeScript
Raw Normal View History

import { test as playwrightTestFn } from '@playwright/test'
2024-11-21 10:07:47 -05:00
import {
fixtures,
Fixtures,
AuthenticatedTronApp,
AuthenticatedApp,
2024-11-21 10:07:47 -05:00
} from './fixtures/fixtureSetup'
export { expect, Page, BrowserContext, TestInfo } from '@playwright/test'
// Our custom decorated Zoo test object. Makes it easier to add fixtures, and
// switch between web and electron if needed.
const pwTestFnWithFixtures = playwrightTestFn.extend<Fixtures>(fixtures)
// In JavaScript you cannot replace a function's body only (despite functions
// are themselves objects, which you'd expect a body property or something...)
// So we must redefine the function and then re-attach properties.
export function test(desc, objOrFn, fnMaybe) {
const hasTestConf = typeof objOrFn === 'object'
const fn = hasTestConf ? fnMaybe : objOrFn
2024-11-21 10:07:47 -05:00
return pwTestFnWithFixtures(
desc,
hasTestConf ? objOrFn : {},
async (
{ page, context, cmdBar, editor, toolbar, scene, homePage },
testInfo
2024-11-21 10:07:47 -05:00
) => {
// To switch to web, use PLATFORM=web environment variable.
// Only use this for debugging, since the playwright tracer is busted
// for electron.
let tronApp;
if (process.env.PLATFORM === 'web') {
tronApp = new AuthenticatedApp(context, page, testInfo)
} else {
tronApp = new AuthenticatedTronApp(context, page, testInfo)
}
2024-11-21 10:07:47 -05:00
const fixtures: Fixtures = { cmdBar, editor, toolbar, scene, homePage }
2024-11-27 11:22:36 -05:00
const options = {
fixtures,
appSettings: objOrFn?.appSettings,
cleanProjectDir: objOrFn?.cleanProjectDir,
}
await tronApp.initialise(options)
2024-11-21 10:07:47 -05:00
// We need to patch this because addInitScript will bind too late in our
// electron tests, never running. We need to call reload() after each call
// to guarantee it runs.
const oldContextAddInitScript = tronApp.context.addInitScript
tronApp.context.addInitScript = async function (a, b) {
await oldContextAddInitScript.apply(this, [a, b])
await tronApp.page.reload()
}
2024-11-21 10:07:47 -05:00
// No idea why we mix and match page and context's addInitScript but we do
const oldPageAddInitScript = tronApp.page.addInitScript
tronApp.page.addInitScript = async function (a, b) {
await oldPageAddInitScript.apply(this, [a, b])
await tronApp.page.reload()
}
if (tronApp instanceof AuthenticatedTronApp) {
// Create a consistent way to resize the page across electron and web.
// (lee) I had to do everyhting in the book to make electron change its
// damn window size. I succeded in making it consistently and reliably
// do it after a whole afternoon.
tronApp.page.setBodyDimensions = async function (dims: {
width: number
height: number
}) {
await tronApp.electronApp.evaluateHandle(async ({ app }, dims) => {
await app.resizeWindow(dims.width, dims.height)
}, dims)
await tronApp.page.setViewportSize(dims)
return tronApp.page.evaluate(async (dims) => {
await window.electron.resizeWindow(dims.width, dims.height)
window.document.body.style.width = dims.width + 'px'
window.document.body.style.height = dims.height + 'px'
window.document.documentElement.style.width = dims.width + 'px'
window.document.documentElement.style.height = dims.height + 'px'
}, dims)
}
2024-11-21 10:07:47 -05:00
// We need to expose this in order for some tests that require folder
// creation. Before they used to do this by their own electronSetup({...})
// calls.
tronApp.context.folderSetupFn = function (fn) {
return fn(tronApp.dir).then(() => ({ dir: tronApp.dir }))
}
}
2024-11-21 10:07:47 -05:00
await fn(
{
context: tronApp.context,
page: tronApp.page,
electronApp: tronApp.electronApp,
2024-11-21 10:07:47 -05:00
...fixtures,
},
testInfo
)
testInfo.tronApp = tronApp
}
)
}
test.describe = pwTestFnWithFixtures.describe
test.beforeEach = pwTestFnWithFixtures.beforeEach
test.afterEach = pwTestFnWithFixtures.afterEach
test.step = pwTestFnWithFixtures.step
test.skip = pwTestFnWithFixtures.skip
2024-11-21 10:07:47 -05:00
test.setTimeout = pwTestFnWithFixtures.setTimeout
test.fixme = pwTestFnWithFixtures.fixme