diff --git a/test/features/support/hooks.js b/test/features/support/hooks.js index cef1d9bf6..9769a8635 100644 --- a/test/features/support/hooks.js +++ b/test/features/support/hooks.js @@ -4,24 +4,60 @@ import { After, Before, Status, Given, When, Then } from 'cucumber' import fakeDialog from 'spectron-fake-dialog' import { expect, should, assert } from 'chai' -After({timeout: 10000}, async function (testCase) { - if (testCase.result.status === Status.FAILED) { - const imageBuffer = await this.app.browserWindow.capturePage() - await this.attach(imageBuffer, 'image/png') +async function stopAppRunning(app) { + try { + if (app && app.isRunning()) { + // console.log('Attempting to stop app...') + const result = await app.stop() + } + // this is just in case for slower OS/windows - catch any error and ignore + if (app && app.electron) { + const forceQuitResult = await this.app.electron.ipcRenderer.sendSync('forceQuit') + // console.log(`force quit?`, result3) + } + } catch (error) { + // console.log('error caught when stopping run. ignoring') } - if (this.app && this.app.isRunning()) { - await this.app.stop() +} + +After({timeout: 40000}, async function (testCase) { + try { + // console.log('Starting after hook....') + if (testCase.result.status === Status.FAILED) { + if (this.app && this.app.browserWindow) { + const imageBuffer = await this.app.browserWindow.capturePage() + await this.attach(imageBuffer, 'image/png') + // console.log('got attachment in', attachResult) + } + } + // stopAppRunning(this.app) + } catch (error) { + console.log('error in after hook', error) + } finally { + await stopAppRunning(this.app) } }) -Before({timeout: 10000}, async function () { - this.rowNumber = null - this.colNumber = null - this.latestFilePath = null - this.pageTimeout = 5000 - fakeDialog.apply(this.app) - await this.app.start() - // Auto-close message dialog : 1 = Quit (No Cancel No save) - fakeDialog.mock([{method: 'showMessageBox', value: 1}]) - fakeDialog.mock([{method: 'showOpenDialog', value: this.openFileDialogReturned}]) +Before({timeout: 20000}, async function (testCase) { + try { + // console.log('Starting before hook....') + console.log(`Starting test scenario: ${testCase.pickle.name} in: ${testCase.sourceLocation.uri}`) + // console.log(testCase.pickle.steps) + this.rowNumber = null + this.colNumber = null + this.latestFilePath = null + this.pageTimeout = 5000 + await fakeDialog.apply(this.app) + await this.app.start() + await this.app.client.waitUntilWindowLoaded() + await this.app.electron.ipcRenderer.sendSync('unlockSingleton') + await this.app.client.browserWindow.focus() + const result = await this.app.client.browserWindow.isFocused() + expect(result).to.be.true + await this.app.electron.ipcRenderer.sendSync('SPECTRON_FAKE_DIALOG/SEND', [{method: 'showMessageBox', value: 1}]) + await this.app.electron.ipcRenderer.sendSync('SPECTRON_FAKE_DIALOG/SEND', [{method: 'showOpenDialog', value: this.openFileDialogReturned}]) + } catch (error) { + console.log('error in before hook', error) + await stopAppRunning(this.app) + } }) diff --git a/test/features/support/world.js b/test/features/support/world.js index b52b4752b..3d6dc6119 100644 --- a/test/features/support/world.js +++ b/test/features/support/world.js @@ -1,7 +1,6 @@ import { setWorldConstructor } from 'cucumber' import { Application } from 'spectron' import electron from 'electron' -// import fakeDialog from 'spectron-fake-dialog' import chai from 'chai' import chaiAsPromised from 'chai-as-promised' @@ -9,21 +8,32 @@ chai.should() chai.use(chaiAsPromised) function CustomWorld ({attach, parameters}) { + // console.log('starting world...') this.attach = attach this.parameters = parameters - this.app = new Application({ + if (!this.app) { + this.app = createApp() + this.hotParentSelector = '.tab-pane.active .editor.handsontable' + this.openFileDialogReturned = ['stubbedFilenameForMenuSteps.txt'] + chaiAsPromised.transferPromiseness = this.app.transferPromiseness + } else { + console.log('WARNING: app already created...') + } +} + +function createApp() { + return new Application({ path: electron, args: ['dist/electron/main.js'], startTimeout: 10000, waitTimeout: 10000, + quitTimeout: 5000, + env: { + ELECTRON_ENABLE_LOGGING: true, + ELECTRON_ENABLE_STACK_DUMPING: true + }, webdriverLogPath: 'webdriver.log' }) - this.rowNumber = null - this.colNumber = null - this.latestFilePath = null - this.hotParentSelector = '.tab-pane.active .editor.handsontable' - this.openFileDialogReturned = ['stubbedFilenameForMenuSteps.txt'] - chaiAsPromised.transferPromiseness = this.app.transferPromiseness } setWorldConstructor(CustomWorld)