Skip to content

Commit

Permalink
provide basic e2e testing for desktop app
Browse files Browse the repository at this point in the history
  • Loading branch information
jon gadsden committed Feb 2, 2023
1 parent 1db2189 commit 60ba96c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 29 deletions.
8 changes: 4 additions & 4 deletions docs/development/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ A JWT is used as a refresh token because it is tamper resistant and provides use
{:.table .table-striped}
| Desktop specific variables | Description | Default |
| --- | ----------- | ------- |
| IS_TEST | Enabled during testing, required for Spectron | false |
| WEBPACK_DEV_SERVER_URL | Server load URL when in development mode | http://localhost:3000/ |
| IS_TEST | Enabled during automated testing | false |
| WEBPACK_DEV_SERVER_URL | Electron load URL when in development mode | http://localhost:3000/ |

**Note:** the desktop environment variable WEBPACK_DEV_SERVER_URL determines either development/test
mode if defined of production mode if not defined
**Note:** the desktop environment variable WEBPACK_DEV_SERVER_URL determines the mode;
either development/test mode if defined or production mode if not defined

___
## Github OAuth App Screenshot
Expand Down
1 change: 0 additions & 1 deletion td.vue/public/preload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { contextBridge, ipcRenderer } = require('electron')

if (process.env.IS_TEST === 'true') {
console.log('require wdio-electron-service/preload');
require('wdio-electron-service/preload');
}

Expand Down
23 changes: 9 additions & 14 deletions td.vue/src/desktop/desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
import menu from './menu.js';
import logger from './logger.js';
import { electronURL, isDevelopment, isTest, isMacOS, isWin } from './utils.js';

const path = require('path');

Expand All @@ -13,16 +14,10 @@ require('update-electron-app')({
logger: require('electron-log')
});

const isDevelopment = process.env.NODE_ENV !== 'production';
const isWin = (process.platform === 'win32' || process.platform === 'win64');
const isTest = process.env.IS_TEST === 'true';

if (isTest) {
console.log('require wdio-electron-service/main');
require('wdio-electron-service/main');
}


// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
Expand Down Expand Up @@ -50,14 +45,14 @@ async function createWindow () {
menu.setMainWindow(mainWindow);
});

if (process.env.WEBPACK_DEV_SERVER_URL) {
logger.log.info('Running in development mode with WEBPACK_DEV_SERVER_URL: ' + process.env.WEBPACK_DEV_SERVER_URL);
// Load the url of the dev server if in development mode
await mainWindow.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
if (!process.env.IS_TEST) mainWindow.webContents.openDevTools();
if (electronURL) {
logger.log.info('Running in development mode with WEBPACK_DEV_SERVER_URL: ' + electronURL);
// Load the url of the dev server when in development mode
await mainWindow.loadURL(electronURL);
if (!isTest) mainWindow.webContents.openDevTools();
} else {
createProtocol('app');
// Load the index.html when not in development
// Load the index.html when not in development mode
mainWindow.loadURL('app://./index.html');
}
}
Expand All @@ -66,7 +61,7 @@ async function createWindow () {
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
if (!isMacOS) {
logger.log.debug('Quit application');
app.quit();
} else {
Expand All @@ -92,7 +87,7 @@ app.on('ready', async () => {
Menu.setApplicationMenu(Menu.buildFromTemplate(template));

// Install Vue Devtools
if (isDevelopment && !process.env.IS_TEST) {
if (isDevelopment && !isTest) {
try {
await installExtension(VUEJS_DEVTOOLS);
} catch (e) {
Expand Down
7 changes: 3 additions & 4 deletions td.vue/src/desktop/logger.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';
import { isDevelopment, isMacOS, isWin, logLevel } from './utils.js';

export const log = require('electron-log');

export const consoleLogLevel = 'error';
export const fileLogLevel = process.env.LOG_LEVEL || 'debug';
const isDevelopment = process.env.NODE_ENV !== 'production';
const isMacOS = process.platform === 'darwin';
const isWin = (process.platform === 'win32' || process.platform === 'win64');
export const fileLogLevel = logLevel;

if (isDevelopment) {
if (isMacOS) {
Expand Down
3 changes: 1 addition & 2 deletions td.vue/src/desktop/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import { app, dialog } from 'electron';
import path from 'path';
import logger from './logger.js';

const isMacOS = process.platform === 'darwin';
import { isMacOS } from './utils.js';

const { shell } = require('electron');
const fs = require('fs');
Expand Down
8 changes: 8 additions & 0 deletions td.vue/src/desktop/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
electronURL: process.env.WEBPACK_DEV_SERVER_URL,
isDevelopment: process.env.NODE_ENV !== 'production',
isMacOS: process.platform === 'darwin',
isTest: process.env.IS_TEST === 'true',
isWin: (process.platform === 'win32' || process.platform === 'win64'),
logLevel: process.env.LOG_LEVEL || 'debug'
};
16 changes: 12 additions & 4 deletions td.vue/tests/e2e/desktop/desktop.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable no-undef */
// TODO: very basic tests, and there should be many more tests than this

describe('Desktop application', () => {

describe('main window', () => {
Expand All @@ -7,11 +9,17 @@ describe('Desktop application', () => {
expect(title).toEqual('OWASP Threat Dragon');
});

it('should set the window size', async () => {
let bounds = await browser.electronBrowserWindow('getBounds');
expect(bounds.width).toEqual(200);
expect(bounds.height).toEqual(300);
it.skip('should set the window size', async () => {
let rect = await browser.getWindowRect();
expect(rect.width).toEqual(1400);
expect(rect.height).toEqual(900);
});

it('should have an electron url', async () => {
let url = await browser.getUrl();
expect(url).toEqual('app://./index.html');
});

});

});
21 changes: 21 additions & 0 deletions td.vue/tests/unit/desktop/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import utils from '@/desktop/utils.js';

describe('desktop/utils.js', () => {

describe('Desktop server utilities', () => {

it('should provide the log level', () => {
expect(utils.logLevel).toMatch('debug');
});

it('should match on environment state', () => {
expect(utils.isDevelopment).toBe(true);
});

it('should not provide the url', () => {
expect(utils.electronURL).not.toBeDefined();
});

});

});

0 comments on commit 60ba96c

Please sign in to comment.