Got context and page loading

This commit is contained in:
Frank Noirot
2024-08-13 19:36:39 +02:00
parent 77f05391ee
commit 9ab64edf7d
3 changed files with 49 additions and 8 deletions

View File

@ -2,6 +2,8 @@ import { _electron as electron, test, expect } from '@playwright/test'
import { getUtils, setup, tearDown } from './test-utils' import { getUtils, setup, tearDown } from './test-utils'
import fs from 'fs/promises' import fs from 'fs/promises'
import { secrets } from './secrets' import { secrets } from './secrets'
import { join } from 'path'
import { tomlStringify } from 'lang/wasm'
test.afterEach(async ({ page }, testInfo) => { test.afterEach(async ({ page }, testInfo) => {
await tearDown(page, testInfo) await tearDown(page, testInfo)
@ -10,31 +12,60 @@ test.afterEach(async ({ page }, testInfo) => {
test( test(
'When the project folder is empty, user can create new project and open it.', 'When the project folder is empty, user can create new project and open it.',
{ tag: '@electron' }, { tag: '@electron' },
async ({ context }, testInfo) => { async ({ page: browserPage, context: browserContext }, testInfo) => {
// create or otherwise clear the folder ./electron-test-projects-dir // 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 { try {
await fs.rm(fileName, { recursive: true }) await fs.rm(projectDirName, { recursive: true })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
const projectDirectory = await fs.mkdir(fileName) await fs.mkdir(projectDirName)
// get full path for ./electron-test-projects-dir // 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({ const electronApp = await electron.launch({
args: ['.'], args: ['.'],
}) })
const context = electronApp.context()
const page = await electronApp.firstWindow() 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 // Set local storage directly using evaluate
await setup(context, page, fullPath)
const u = await getUtils(page) const u = await getUtils(page)
await page.setViewportSize({ width: 1200, height: 500 }) await page.setViewportSize({ width: 1200, height: 500 })
await page.goto('http://localhost:3000/')
page.on('console', console.log) page.on('console', console.log)

View File

@ -55,3 +55,5 @@ export const KCL_DEFAULT_CONSTANT_PREFIXES = {
} as const } as const
/** The default KCL length expression */ /** The default KCL length expression */
export const KCL_DEFAULT_LENGTH = `5` 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'

View File

@ -8,6 +8,7 @@ import { components } from './machine-api'
import { isDesktop } from './isDesktop' import { isDesktop } from './isDesktop'
import { FileEntry } from 'wasm-lib/kcl/bindings/FileEntry' import { FileEntry } from 'wasm-lib/kcl/bindings/FileEntry'
import { SaveSettingsPayload } from 'lib/settings/settingsTypes' import { SaveSettingsPayload } from 'lib/settings/settingsTypes'
import * as TOML from '@iarna/toml'
import { import {
defaultAppSettings, defaultAppSettings,
@ -15,6 +16,8 @@ import {
parseAppSettings, parseAppSettings,
parseProjectSettings, parseProjectSettings,
} from 'lang/wasm' } from 'lang/wasm'
import { TEST_SETTINGS_KEY } from '../../e2e/playwright/storageStates'
import { TEST_SETTINGS_FILE_KEY } from './constants'
export { parseProjectRoute } from 'lang/wasm' export { parseProjectRoute } from 'lang/wasm'
const DEFAULT_HOST = 'https://api.zoo.dev' const DEFAULT_HOST = 'https://api.zoo.dev'
@ -373,9 +376,14 @@ export async function writeProjectSettingsFile(
} }
const getAppSettingsFilePath = async () => { 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( const fullPath = window.electron.path.join(
appConfig, appConfig,
isPlaywright ? testDirectoryName : '',
window.electron.packageJson.name window.electron.packageJson.name
) )
try { try {