From 10f9e328631797d2ec99e38f8dd82a3172d73f8a Mon Sep 17 00:00:00 2001 From: toddtarsi Date: Sat, 28 Oct 2023 14:45:33 -0500 Subject: [PATCH] Dumping session state and logs to a file --- packages/selenium-ide/package.json | 2 +- .../controllers/Menu/menus/application.ts | 5 +++ .../session/controllers/Menu/menus/help.ts | 20 ++++++++++ .../main/session/controllers/System/index.ts | 38 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 packages/selenium-ide/src/main/session/controllers/Menu/menus/help.ts diff --git a/packages/selenium-ide/package.json b/packages/selenium-ide/package.json index 3f29c8129..bc4de516a 100644 --- a/packages/selenium-ide/package.json +++ b/packages/selenium-ide/package.json @@ -1,6 +1,6 @@ { "name": "@seleniumhq/selenium-ide", - "version": "4.0.0-alpha.53", + "version": "4.0.0-alpha.54", "private": true, "description": "Selenium IDE electron app", "author": "Todd ", diff --git a/packages/selenium-ide/src/main/session/controllers/Menu/menus/application.ts b/packages/selenium-ide/src/main/session/controllers/Menu/menus/application.ts index 4ef5ff6b4..9b41fcc86 100644 --- a/packages/selenium-ide/src/main/session/controllers/Menu/menus/application.ts +++ b/packages/selenium-ide/src/main/session/controllers/Menu/menus/application.ts @@ -5,6 +5,7 @@ import { projectEditorCommands } from './projectEditor' import { testEditorCommands } from './testEditor' import { projectViewCommands } from './projectView' import { platform } from 'os' +import helpMenu from './help' const applicationMenu = (session: Session) => async () => Menu.buildFromTemplate([ @@ -43,6 +44,10 @@ const applicationMenu = (session: Session) => async () => label: '&View', submenu: await projectViewCommands(session)(), }, + { + label: '&Help', + submenu: await helpMenu(session)(), + }, ]) export default applicationMenu diff --git a/packages/selenium-ide/src/main/session/controllers/Menu/menus/help.ts b/packages/selenium-ide/src/main/session/controllers/Menu/menus/help.ts new file mode 100644 index 000000000..da71db812 --- /dev/null +++ b/packages/selenium-ide/src/main/session/controllers/Menu/menus/help.ts @@ -0,0 +1,20 @@ +import { Menu } from 'electron' +import { MenuComponent, Session } from 'main/types' + +export const HelpCommands: MenuComponent = (session) => async () => + [ + { + accelerator: 'CommandOrControl+Shift+D', + click: async () => { + await session.system.dumpSession() + }, + label: 'Dump Session To File', + }, + ] + +const helpMenu = (session: Session) => async () => { + const menuItems = await HelpCommands(session)() + return Menu.buildFromTemplate(menuItems) +} + +export default helpMenu diff --git a/packages/selenium-ide/src/main/session/controllers/System/index.ts b/packages/selenium-ide/src/main/session/controllers/System/index.ts index d890d90c8..0d364165a 100644 --- a/packages/selenium-ide/src/main/session/controllers/System/index.ts +++ b/packages/selenium-ide/src/main/session/controllers/System/index.ts @@ -1,15 +1,49 @@ import { dialog } from 'electron' import BaseController from '../Base' import { isAutomated } from 'main/util' +import { inspect } from 'util' +import { writeFile } from 'fs/promises' let firstTime = true export default class SystemController extends BaseController { + constructor(session: any) { + super(session) + this.writeToLog = this.writeToLog.bind(this) + } isDown = true shuttingDown = false + logs = '' + + async dumpSession() { + const response = await this.session.dialogs.openSave() + if (response.canceled) return + const filePath = response.filePath as string + await writeFile( + filePath, + JSON.stringify( + { + project: this.session.projects.project, + state: this.session.state.state, + logs: this.logs, + }, + undefined, + 2 + ) + ) + } async getLogPath() { return this.session.app.getPath('logs') } + + async getLogs() { + return this.logs + } + + async writeToLog(...args: any[]) { + this.logs += args.map((arg) => inspect(arg)).join(' ') + '\n' + } + async startup() { if (this.isDown) { await this.session.windows.open('logger') @@ -23,6 +57,7 @@ export default class SystemController extends BaseController { } } await this.session.projects.select(firstTime) + await this.session.api.system.onLog.addListener(this.writeToLog) this.isDown = false firstTime = false } @@ -39,6 +74,9 @@ export default class SystemController extends BaseController { } this.shuttingDown = false } + try { + await this.session.api.system.onLog.removeListener(this.writeToLog) + } catch (e) {} } }