enso/app/ide-desktop/client/tests/electronTest.ts
Adam Obuchowicz 736134e491
Add traces to integration tests + suppress one flaky assertion. (#11595)
Fixes #11604

Most issues were caused by a problem with Project List flooding the network with its requests - this was fixed on develop.
But one assertion was flaky - it assumed we will see the "real" run result on `write` node, but sometimes it is immediately overwritten by dry run.

But the most important part of this PR is adding traces to Electron packages - it's should be much easier now to debug E2E test failures.

Also renamed the previously misnamed "E2E tests" to "[GUI] integration tests".
2024-11-27 14:09:59 +00:00

77 lines
2.9 KiB
TypeScript

/** @file Commonly used functions for electron tests */
import { _electron, ElectronApplication, expect, type Page, test } from '@playwright/test'
import { TEXTS } from 'enso-common/src/text'
import * as random from 'lib0/random'
import os from 'node:os'
import pathModule from 'node:path'
const LOADING_TIMEOUT = 10000
const TEXT = TEXTS.english
export const CONTROL_KEY = os.platform() === 'darwin' ? 'Meta' : 'Control'
/**
* Tests run on electron executable.
*
* Similar to playwright's test, but launches electron, and passes Page of the main window.
*/
export function electronTest(
name: string,
body: (args: {
page: Page
app: ElectronApplication
projectsDir: string
}) => Promise<void> | void,
) {
test(name, async () => {
const uuid = random.uuidv4()
const projectsDir = pathModule.join(os.tmpdir(), 'enso-test-projects', `${name}-${uuid}`)
console.log('Running Application; projects dir is', projectsDir)
const app = await _electron.launch({
executablePath: process.env.ENSO_TEST_EXEC_PATH ?? '',
args: process.env.ENSO_TEST_APP_ARGS != null ? process.env.ENSO_TEST_APP_ARGS.split(',') : [],
env: { ...process.env, ENSO_TEST: name, ENSO_TEST_PROJECTS_DIR: projectsDir },
})
const page = await app.firstWindow()
await app.context().tracing.start({ screenshots: true, snapshots: true, sources: true })
// Wait until page will be finally loaded: we expect login screen.
// There's bigger timeout, because the page may load longer on CI machines.
await expect(page.getByText('Login to your account')).toBeVisible({ timeout: LOADING_TIMEOUT })
try {
await body({ page, app, projectsDir })
} finally {
await app.context().tracing.stop({ path: `test-traces/${name}.zip` })
await app.close()
}
})
}
/**
* Login as test user. This function asserts that page is the login page, and uses
* credentials from ENSO_TEST_USER and ENSO_TEST_USER_PASSWORD env variables.
*/
export async function loginAsTestUser(page: Page) {
// Login screen
await expect(page.getByRole('textbox', { name: 'email' })).toBeVisible()
await expect(page.getByRole('textbox', { name: 'password' })).toBeVisible()
if (process.env.ENSO_TEST_USER == null || process.env.ENSO_TEST_USER_PASSWORD == null) {
throw Error(
'Cannot log in; `ENSO_TEST_USER` and `ENSO_TEST_USER_PASSWORD` env variables are not provided',
)
}
await page.getByRole('textbox', { name: 'email' }).fill(process.env.ENSO_TEST_USER)
await page.getByRole('textbox', { name: 'password' }).fill(process.env.ENSO_TEST_USER_PASSWORD)
await page.getByTestId('form-submit-button').click()
await page
.getByRole('group', { name: TEXT.licenseAgreementCheckbox })
.getByText(TEXT.licenseAgreementCheckbox)
.click()
await page
.getByRole('group', { name: TEXT.privacyPolicyCheckbox })
.getByText(TEXT.privacyPolicyCheckbox)
.click()
await page.getByTestId('form-submit-button').click()
}