From 9ab64edf7d9765f9a8a64cc45560b6a1cf15d6ab Mon Sep 17 00:00:00 2001 From: Frank Noirot Date: Tue, 13 Aug 2024 19:36:39 +0200 Subject: [PATCH] Got context and page loading --- e2e/playwright/projects.spec.ts | 45 ++++++++++++++++++++++++++++----- src/lib/constants.ts | 2 ++ src/lib/desktop.ts | 10 +++++++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/e2e/playwright/projects.spec.ts b/e2e/playwright/projects.spec.ts index 355bfa459..981055da7 100644 --- a/e2e/playwright/projects.spec.ts +++ b/e2e/playwright/projects.spec.ts @@ -2,6 +2,8 @@ import { _electron as electron, test, expect } from '@playwright/test' import { getUtils, setup, tearDown } from './test-utils' import fs from 'fs/promises' import { secrets } from './secrets' +import { join } from 'path' +import { tomlStringify } from 'lang/wasm' test.afterEach(async ({ page }, testInfo) => { await tearDown(page, testInfo) @@ -10,31 +12,60 @@ test.afterEach(async ({ page }, testInfo) => { test( 'When the project folder is empty, user can create new project and open it.', { tag: '@electron' }, - async ({ context }, testInfo) => { + async ({ page: browserPage, context: browserContext }, testInfo) => { // create or otherwise clear the folder ./electron-test-projects-dir - const fileName = `./${testInfo.titlePath.join('-').replace(/\s/gi, '-')}-dir` + const settingsFileName = `./${testInfo.title + .replace(/\s/gi, '-') + .replace(/\W/gi, '')}` + const projectDirName = settingsFileName + '-dir' try { - await fs.rm(fileName, { recursive: true }) + await fs.rm(projectDirName, { recursive: true }) } catch (e) { console.error(e) } - const projectDirectory = await fs.mkdir(fileName) + await fs.mkdir(projectDirName) // get full path for ./electron-test-projects-dir - const fullPath = await fs.realpath(fileName) + const fullProjectPath = await fs.realpath(projectDirName) const electronApp = await electron.launch({ args: ['.'], }) - + const context = electronApp.context() const page = await electronApp.firstWindow() + const electronTempDirectory = await page.evaluate(async () => { + return await window.electron.getPath( + 'temp' + ) + }) + const tempSettingsFilePath = join(electronTempDirectory, settingsFileName) + const settingsOverrides = tomlStringify({ + app: { + projectDirectory: fullProjectPath, + }, + }) + + if (settingsOverrides instanceof Error) { + throw settingsOverrides + } + await fs.writeFile(tempSettingsFilePath + '.toml', settingsOverrides) + + console.log('from within test setup', { + settingsFileName, + fullPath: fullProjectPath, + electronApp, + page, + settingsFilePath: tempSettingsFilePath + '.toml', + }) + + await setup(context, page, fullProjectPath) // Set local storage directly using evaluate - await setup(context, page, fullPath) const u = await getUtils(page) await page.setViewportSize({ width: 1200, height: 500 }) + await page.goto('http://localhost:3000/') page.on('console', console.log) diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 092dc1582..310af6b72 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -55,3 +55,5 @@ export const KCL_DEFAULT_CONSTANT_PREFIXES = { } as const /** The default KCL length expression */ export const KCL_DEFAULT_LENGTH = `5` +/** localStorage key for the playwright test-specific app settings file */ +export const TEST_SETTINGS_FILE_KEY = 'playwright-test-settings' \ No newline at end of file diff --git a/src/lib/desktop.ts b/src/lib/desktop.ts index 61e809f00..75bdbb571 100644 --- a/src/lib/desktop.ts +++ b/src/lib/desktop.ts @@ -8,6 +8,7 @@ import { components } from './machine-api' import { isDesktop } from './isDesktop' import { FileEntry } from 'wasm-lib/kcl/bindings/FileEntry' import { SaveSettingsPayload } from 'lib/settings/settingsTypes' +import * as TOML from '@iarna/toml' import { defaultAppSettings, @@ -15,6 +16,8 @@ import { parseAppSettings, parseProjectSettings, } from 'lang/wasm' +import { TEST_SETTINGS_KEY } from '../../e2e/playwright/storageStates' +import { TEST_SETTINGS_FILE_KEY } from './constants' export { parseProjectRoute } from 'lang/wasm' const DEFAULT_HOST = 'https://api.zoo.dev' @@ -373,9 +376,14 @@ export async function writeProjectSettingsFile( } const getAppSettingsFilePath = async () => { - const appConfig = await window.electron.getPath('appData') + const isPlaywright = window.localStorage.getItem('playwright') === 'true' + const testDirectoryName = window.localStorage.getItem(TEST_SETTINGS_FILE_KEY) ?? '' + const appConfig = await window.electron.getPath( + isPlaywright ? 'temp' : 'appData' + ) const fullPath = window.electron.path.join( appConfig, + isPlaywright ? testDirectoryName : '', window.electron.packageJson.name ) try {